- 在eclipse中新建项目springmvc,使用dynamic web project的方式。
- 导入相关的jar包
-
- 在WEB-INF目录下创建 web.xml(默认不做指定spring-servlet.xml,即默认在WEB-INF下。如果是自定义指定那需要在servlet下<init-param>下指定)
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
- 在WEB-INF目录下创建 springmvc-servlet.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/index">indexController</prop> </props> </property> </bean> <bean id="indexController" class="controller.IndexController"></bean> </beans>
- 控制类 IndexController
public class IndexController implements Controller { public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView mav = new ModelAndView("index.jsp"); mav.addObject("message", "Hello Spring MVC"); return mav; } }
- 准备index.jsp,在WebContent目录下创建index.jsp <%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8" isELIgnored="false"%><h1>${message}</h1>
- 总结:
1. 用户访问 /index
2. 根据web.xml中的配置 所有的访问都会经过DispatcherServlet
3. 根据 根据配置文件springmvc-servlet.xml ,访问路径/index
会进入IndexController类
4. 在IndexController中指定跳转到页面index.jsp,并传递message数据
5. 在index.jsp中显示message信息
2. 根据web.xml中的配置 所有的访问都会经过DispatcherServlet
3. 根据 根据配置文件springmvc-servlet.xml ,访问路径/index
会进入IndexController类
4. 在IndexController中指定跳转到页面index.jsp,并传递message数据
5. 在index.jsp中显示message信息
将上面的第5,6做修改,进行视图定位:
修改springmvc-servlet.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/page/" /> <property name="suffix" value=".jsp" /> </bean> <bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/index">indexController</prop> </props> </property> </bean> <bean id="indexController" class="controller.IndexController"></bean> </beans>修改IndexController
public class IndexController implements Controller { public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView mav = new ModelAndView("index"); mav.addObject("message", "Hello Spring MVC"); return mav; } }
使用注解的方式:
在类前面加上@Controller 表示该类是一个控制器
在方法handleRequest 前面加上 @RequestMapping("/index") 表示路径/index会映射到该方法上
注意:不再让IndexController实现Controller接口
在方法handleRequest 前面加上 @RequestMapping("/index") 表示路径/index会映射到该方法上
注意:不再让IndexController实现Controller接口
@Controller public class IndexController { @RequestMapping("/index") public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView mav = new ModelAndView("index"); mav.addObject("message", "Hello Spring MVC"); return mav; } }修改springmvc-servlet.xml
<?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: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"> <context:component-scan base-package="controller" /> <bean id="irViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/page/" /> <property name="suffix" value=".jsp" /> </bean> <!-- <bean id="simpleUrlHandlerMapping" --> <!-- class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> --> <!-- <property name="mappings"> --> <!-- <props> --> <!-- <prop key="/index">indexController</prop> --> <!-- </props> --> <!-- </property> --> <!-- </bean> --> <!-- <bean id="indexController" class="controller.IndexController"></bean> --> </beans>
Session在用户登录,一些特殊场合在页面间传递数据的时候会经常用到
SpringMVC组件:
DispatcherServlet:负责流程的控制
- 文件上传解析,如果请求类型是multipart将通过MultipartResolver进行文件解析
- 通过HandlerMapping,将请求映射到处理器(返回一个HandlerExecutionChain,它包括一个处理器,多个HandlerInterceptor***)
- 通过HandlerAdapter支持多种类型的处理器(HandlerExecrutionChain)
- 通过ViewResolver解析逻辑视图名到具体的视图实现
- 本地化解析
- 渲染具体的视图等
- 如果执行过程中遇到异常将交给HandlerExceptionResolver来解析