引入jar包:
spring- web- 4.1 .7 . RELEASE. jar
spring- webmvc- 4.1 .7 . RELEASE. jar
新建动态工程
jsps文件夹放在WEB- INF下面
< ? xml version= "1.0" encoding= "UTF-8" ? >
< web- app xmlns: xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns= "http://java.sun.com/xml/ns/javaee" xsi: schemaLocation= "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id= "WebApp_ID" version= "2.5" >
< display- name> springmvc< / display- name>
< welcome- file- list>
< welcome- file> index. html< / welcome- file>
< welcome- file> index. htm< / welcome- file>
< welcome- file> index. jsp< / welcome- file>
< welcome- file> default . html< / welcome- file>
< welcome- file> default . htm< / welcome- file>
< welcome- file> default . jsp< / welcome- file>
< / welcome- file- list>
< servlet>
< servlet- name> springDispatcherServlet< / servlet- name>
< servlet- class > org. springframework. web. servlet. DispatcherServlet< / servlet- class >
< init- param>
< param- name> contextConfigLocation< / param- name>
< param- value> classpath: springmvc. xml< / param- value>
< / init- param>
< / servlet>
< servlet- mapping>
< servlet- name> springDispatcherServlet< / servlet- name>
< url- pattern> * . do < / url- pattern>
< / servlet- mapping>
< / web- app>
< ? xml version= "1.0" encoding= "UTF-8" ? >
< beans xmlns= "http://www.springframework.org/schema/beans"
xmlns: xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns: aop= "http://www.springframework.org/schema/aop"
xmlns: context= "http://www.springframework.org/schema/context"
xmlns: jee= "http://www.springframework.org/schema/jee" xmlns: lang= "http://www.springframework.org/schema/lang"
xmlns: util= "http://www.springframework.org/schema/util" xmlns: tx= "http://www.springframework.org/schema/tx"
xmlns: mvc= "http://www.springframework.org/schema/mvc"
xsi: schemaLocation= "http: / / www. springframework. org/ schema/ beans
http: / / www. springframework. org/ schema/ beans/ spring- beans. xsd
http: / / www. springframework. org/ schema/ aop
http: / / www. springframework. org/ schema/ aop/ spring- aop. xsd
http: / / www. springframework. org/ schema/ jee
http: / / www. springframework. org/ schema/ jee/ spring- jee. xsd
http: / / www. springframework. org/ schema/ lang
http: / / www. springframework. org/ schema/ lang/ spring- lang. xsd
http: / / www. springframework. org/ schema/ context
http: / / www. springframework. org/ schema/ context/ spring- context. xsd
http: / / www. springframework. org/ schema/ tx
http: / / www. springframework. org/ schema/ tx/ spring- tx. xsd
http: / / www. springframework. org/ schema/ util
http: / / www. springframework. org/ schema/ util/ spring- util. xsd
http: / / www. springframework. org/ schema/ mvc
http: / / www. springframework. org/ schema/ mvc/ spring- mvc. xsd">
< ! -- 配置处理器映射器 第一种-- >
< ! -- < bean class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" > < / bean> -- >
< ! -- 配置集中管理的映射器 第二种 集中声明 -- >
< ! -- < bean class = "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
< property name= "mappings" >
< props>
< prop key= "abc.do" > userController< / prop>
< prop key= "sf.do" > userController< / prop>
< prop key= "test.do" > userController< / prop>
< / props>
< / property>
< / bean> -- >
< ! -- 根据类名访问 第三种 -- >
< ! -- < bean class = "org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" > < / bean> -- >
< ! -- 配置处理器适配器 -- >
< ! -- SimpleControllerHandlerAdapter: controller要实现controller接口 -- >
< ! -- < bean class = "org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" > < / bean> -- >
< ! -- HttpRequestHandlerAdapter要实现HttpRequestHandler -- >
< ! -- < bean class = "org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter" > < / bean>
< bean id= "userController" name= "/hello.do" class = "com.springmvc.controller.UserController" > < / bean> -- >
< ! -- * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -- >
< ! -- 使@controller 注解生效 -- >
< ! -- < context: component- scan base- package = "com.springmvc.controller" > < / context: component- scan> -- >
< ! -- 配置注解处理器映射器 -- >
< ! -- < bean class = "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" > < / bean> -- >
< ! -- 配置注解处理器适配器 -- >
< ! -- < bean class = "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" > < / bean> -- >
< ! -- * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * -- >
< ! -- 使@controller 注解生效 -- >
< context: component- scan base- package = "com.springmvc.controller" > < / context: component- scan>
< mvc: annotation- driven> < / mvc: annotation- driven> < ! -- 开启注解模式 -- >
< ! -- 配置视图解析器 -- >
< bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" >
< property name= "prefix" value= "/WEB-INF/jsps/" > < / property>
< property name= "suffix" value= ".jsp" > < / property>
< / bean>
< / beans>
< % @ page language= "java" contentType= "text/html; charset=utf-8"
pageEncoding= "utf-8" % >
< ! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html>
< head>
< meta http- equiv= "Content-Type" content= "text/html; charset=utf-8" >
< title> Insert title here< / title>
< / head>
< body>
< % -- ${ hello} -- % >
hello world test
< / body>
< / html>
package com. springmvc. controller;
import org. springframework. stereotype. Controller;
import org. springframework. web. bind. annotation. RequestMapping;
import org. springframework. web. bind. annotation. RequestMethod;
@Controller
@RequestMapping ( "/userController" )
public class UserController {
@RequestMapping ( value= "hello" , method= { RequestMethod. POST, RequestMethod. GET} )
public String testHello ( ) {
System. out. println ( "you are beautiful" ) ;
return "hello" ;
}
}
package com. springmvc. controller;
import java. io. IOException;
import javax. servlet. ServletException;
import javax. servlet. http. HttpServletRequest;
import javax. servlet. http. HttpServletResponse;
import org. springframework. web. HttpRequestHandler;
import org. springframework. web. servlet. ModelAndView;
public class HttpController implements HttpRequestHandler {
public void handleRequest ( HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
}
}