spring mvc和rest风格小例子

之前在新一个项目中用了spring3 的mvc开发,用得很爽,不过当时想找些入门的小例子时,找了好久也没找到,

现在写个简单的小例子出来给初学者学习下。
srping3也支持rest,所以例子也包括这部分内容。
先看web.xml配置
<!-- 像js,css,gif等静态文件,需要配置为默认的servlet -->

  < servlet-mapping >  
 
< servlet-name > default </ servlet-name >  
 
< url-pattern > *.js </ url-pattern >  
 
</ servlet-mapping >  
 
< servlet-mapping >  
 
< servlet-name > default </ servlet-name >  
 
< url-pattern > *.css </ url-pattern >  
 
</ servlet-mapping >  

 
< servlet >  
 
< servlet-name > spring </ servlet-name >  
 
< servlet-class > org.springframework.web.servlet.DispatcherServlet </ servlet-class >  
 
< load-on-startup > 1 </ load-on-startup >  
 
</ servlet >  
 
< servlet-mapping >  
 
< servlet-name > spring </ servlet-name >  
 
< url-pattern > / </ url-pattern >  
<!--  url配置为/,不带文件后缀,会造成其它静态文件(js,css等)不能访问。如配为*.do,则不影响静态文件的

访问 
-->  
 
</ servlet-mapping >

spring-servlet.xml文件配置,注:XXXX-servlet.xml的XXXX就是上边<servlet-name>spring</servlet-name>中

的名称,如果上边为<servlet-name>mvc</servlet-name>则这个文件名为mvc-servelt.xml。

< beans  xmlns ="http://www.springframework.org/schema/beans"  
 xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"  

xmlns:p
="http://www.springframework.org/schema/p"  
 xmlns:context
="http://www.springframework.org/schema/context"  
 xsi:schemaLocation
="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
 http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context-3.0.xsd"
>  
 
 
<!--  自动扫描bean,把作了注解的类转换为bean  -->  
 
< context:component-scan  base-package ="com.mvc.rest"   />  
 
 
<!--  启动Spring MVC的注解功能,完成请求和注解POJO的映射  -->  
 
< bean class ="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"   />  

 
 
 
<!--  对模型视图名称的解析,在请求时模型视图名称添加前后缀  -->  
 
< bean  class ="org.springframework.web.servlet.view.InternalResourceViewResolver"  
 p:prefix
="/WEB-INF/view/"  p:suffix =".jsp"   />  
 
 
< bean  id ="multipartResolver"  
 class
="org.springframework.web.multipart.commons.CommonsMultipartResolver"  
 p:defaultEncoding
="utf-8"   />  
 
 
</ beans >

controller类
package  com.mvc.rest;

import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletResponse;

import  org.springframework.stereotype.Controller;
import  org.springframework.ui.ModelMap;
import  org.springframework.web.bind.annotation.PathVariable;
import  org.springframework.web.bind.annotation.RequestMapping;
import  org.springframework.web.bind.annotation.RequestMethod;
import  org.springframework.web.servlet.ModelAndView;

@Controller
public   class  RestController  {
 
 
public  RestController() {
 
 }

 
 
@RequestMapping(value = "/login/{user}", method = RequestMethod.GET) 
 
public  ModelAndView myMethod(HttpServletRequest request, HttpServletResponse response, 
 @PathVariable(
"user" ) String user, ModelMap modelMap)  throws  Exception 
 modelMap.put(
" loginUser " , user);
 
return   new  ModelAndView( " /login/hello " , modelMap);
 }
 
 
 
@RequestMapping(value = "/welcome", method = RequestMethod.GET) 
 
public  String registPost() 
 
return   " /welcome " ;
 }
 
}

两个jsp页面
\WEB-INF\viewwelcome.jsp
< html >
< head >
< meta http - equiv = " Content-Type "  content = " text/html; charset=GBK " >
< title > Insert title here </ title >
</ head >
< body >
欢迎
</ body >
</ html >


\WEB-INF\view\login\hello.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Insert title here</title>
</head>
<body>
你好:<%=request.getAttribute("loginUser") %>

// ${loginUser }


</body>
</html>

运行效果
访问http://localhost:8089/mvc/welcome

http://www.blogjava.net/pengo/

访问http://localhost:8089/mvc/login/pengo

http://www.blogjava.net/pengo/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值