1、流程圖html
時間流程圖前端
第一步:用戶發送請求到前端控制器(DispatcherServlet)java
第二步:前端控制器請求HandlerMapping查找 Handler 【能夠根據xml配置、註解進行查找】web
第三步:處理器映射器HandlerMapping向前端控制器返回Handlerspring
第四步:前端控制器請求處理器適配器去執行Handlerexpress
第五步:處理器適配器去執行Handlerspring-mvc
第六步:處理器適配器執行完成後,Controller返回ModelAndViewmvc
第七步:處理器適配器向前端控制器返回ModelAndViewapp
ModelAndView是springmvc框架的一個底層對象,包括Model和view框架
第八步:前端控制器請求視圖解析器去進行視圖解析【根據邏輯視圖名解析成真正的視圖(jsp)】
第九步:視圖解析器向前端控制器返回View
第十步:前端控制器進行視圖渲染【視圖渲染即:模型數據(在ModelAndView對象中)填充到request域】
第十一步:前端控制器向用戶響應結果
2、簡單例子
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)測試