之前在新一个项目中用了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 >
< 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 >
<!-- 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" />
<!-- 启动SpringMVC的注解功能,完成请求和注解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 >
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" />
<!-- 启动SpringMVC的注解功能,完成请求和注解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类
- packagecom.mvc.rest;
- importjavax.servlet.http.HttpServletRequest;
- importjavax.servlet.http.HttpServletResponse;
- importorg.springframework.stereotype.Controller;
- importorg.springframework.ui.ModelMap;
- importorg.springframework.web.bind.annotation.PathVariable;
- importorg.springframework.web.bind.annotation.RequestMapping;
- importorg.springframework.web.bind.annotation.RequestMethod;
- importorg.springframework.web.servlet.ModelAndView;
- @Controller
- publicclassRestController{
- publicRestController(){
- }
- @RequestMapping(value="/login/{user}",method=RequestMethod.GET)
- publicModelAndViewmyMethod(HttpServletRequestrequest,HttpServletResponseresponse,
- @PathVariable("user")Stringuser,ModelMapmodelMap)throwsException{
- modelMap.put("loginUser",user);
- returnnewModelAndView("/login/hello",modelMap);
- }
- @RequestMapping(value="/welcome",method=RequestMethod.GET)
- publicStringregistPost(){
- return"/welcome";
- }
- }
两个jsp页面
\WEB-INF\viewwelcome.jsp
<
html
>
< head >
< metahttp - equiv = " Content-Type " content = " text/html;charset=GBK " >
< title > Inserttitlehere </ title >
</ head >
< body >
欢迎
</ body >
</ html >
< head >
< metahttp - equiv = " Content-Type " content = " text/html;charset=GBK " >
< title > Inserttitlehere </ 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") %>
</body>
</html>
运行效果
访问http://localhost:8089/mvc/welcome
访问http://localhost:8089/mvc/login/pengo