spring MVC工作流程

一、流程图

spring MVC工作流程

 时间流程图

spring MVC工作流程

第一步:用户发送请求到前端控制器(DispatcherServlet)

第二步:前端控制器请求HandlerMapping查找 Handler 【可以根据xml配置、注解进行查找】

第三步:处理器映射器HandlerMapping向前端控制器返回Handler

第四步:前端控制器请求处理器适配器去执行Handler

第五步:处理器适配器去执行Handler

第六步:处理器适配器执行完成后,Controller返回ModelAndView

第七步:处理器适配器向前端控制器返回ModelAndView

ModelAndView是springmvc框架的一个底层对象,包括Model和view

第八步:前端控制器请求视图解析器去进行视图解析【根据逻辑视图名解析成真正的视图(jsp)】    

第九步:视图解析器向前端控制器返回View

第十步:前端控制器进行视图渲染【视图渲染即:模型数据(在ModelAndView对象中)填充到request域】

第十一步:前端控制器向用户响应结果

 

二、简单例子

1)引jar包

//spring_core
spring3.2.9core\commons-logging-1.2.jar
spring3.2.9core\spring-beans-3.2.9.RELEASE.jar
spring3.2.9core\spring-context-3.2.9.RELEASE.jar
spring3.2.9core\spring-core-3.2.9.RELEASE.jar
spring3.2.9core\spring-expression-3.2.9.RELEASE.jar

//spring mvc
springMVC\spring-web-3.2.9.RELEASE.jar
springMVC\spring-webmvc-3.2.9.RELEASE.jar

2)配置web.xml文件,spring mvc核心servlet:dispatcherServlet

<?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>springmvc2</display-name>
  
  <servlet>
      <servlet-name>DispatcherServlet</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>DispatcherServlet</servlet-name>
      <url-pattern>*.action</url-pattern>
  </servlet-mapping>
  
  <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>
</web-app>

3)写Action类

public ModelAndView handleRequest(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        
        modelAndView.addObject("word", "成功加载");
        modelAndView.setViewName("index");
        
        System.out.println("点击成功");
        
        return modelAndView;
    }

4)该Action的配置springmvc.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:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
        
        <!-- 1 action -->
        <bean id="userAction" class="com.huitong.action.EmpAction"></bean>
        <!-- 2 mapping -->
        <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="mappings">
            
                <props>
                    <prop key="/add.action">userAction</prop>
                    <prop key="/update.action">userAction</prop>
                    <prop key="/delete.action">userAction</prop>
                    <prop key="/get.action">userAction</prop>
                </props>
            
            </property>
        
        </bean>
        
        <!-- 3 adapter -->
        
        <!-- 4 internalresourceviewresolver -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/jsp/"></property>
            <property name="suffix" value=".jsp"></property>
        
        </bean>
</beans>

5)如果该文件不是在默认位置,需要在web.xml文件中进行配置。说明配置文件在哪。

6)编写视图文件,jsp

7)测试