spring+springmvc+mybatis詳細運轉流程

最近在整合spring+springmvc+mybatis,網上有很多直接整合好的例子,但是下載過來之後發現對於SSM具體的運轉流程還是不太清楚,今天看到一篇關於springmvc的詳細處理流程,覺得總結的還可以,不懂springmvc運行流程的童鞋,大家一起來學習學習~~

mvc 處理流程:

1. 首先瀏覽器發送一個 url請求,比如 http://www.xxx.com/hello.html

2. 然後被 org.springframework.web.servlet.DispatcherServlet 攔截,DipatcherServlet 根據配置裏的xml 文件路徑找到相應的配置文件,比如 dispatcher-servlet.xml 文件。 DipatcherServlet web.xml 中配置。如果使用的控制器映射方式是SimpleUrlHandlerMapping(推薦使用這種,映射方式有 3 種:BeanNameUrlHandlerMapping , SimpleUrlHandlerMappin ,CommonsPathMapHandlerMapping ),默認的控制器映射方式是 BeanNameUrlHandlerMapping。在 SimpleUrlHandlerMapping 中的 mappings 中查找“ /hello*.html ”的 key 值,然後找相應的 value ,即對應 bean 的 id 名字,比如 helloController 。然後在配置文件中查找相應 bean 的 id 爲“ helloController ”的 bean 配置,查找其對應的處理類,比如“com.ctq.action.HelloController ”。

Java代碼  收藏代碼
  1. <bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">  
  2. <property name="mappings">  
  3. <props>  
  4. <prop key="/hello*.html">helloController</prop>  
  5. </props>  
  6. </property>  
  7. </bean>  
 

比如

Java代碼  收藏代碼
  1. <bean id="helloController" class="com.ctq.action.HelloController">  
 

現在由 HelloController 類來進行相應的處理操作(推薦 HelloController 繼承MultiActionController ),

3. 然後 D ipatcherServlet詢問配置文件中的視圖解析器 PropertiesMethodNameResolver(個人推薦用這種,共有 3 種: InternalResourceViewResolver ,ParameterMethodNameResolver , PropertiesMethodNameResolver 。我覺得後兩種都還可以,第一種雖然爲默認情況,但適合小型簡單的應用)。若採用這種視圖解析器,需要在第 3 步中的bean 配置中加入 methodNameResolver 屬性,以代替默認的 InternalResourceViewResolver配置。比如:

Java代碼  收藏代碼
  1. <bean id="helloController" class="com.ctq.action.HelloController">  
  2. <property name="methodNameResolver">  
  3. <ref bean="methodNameResolver"/>  
  4. </property>  
  5. </bean>  
  6.   
  7. <bean id="methodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">  
  8. <property name="mappings">  
  9. <props>  
  10. <prop key="/hello.html">hello</prop>  
  11. <prop key="/helloworld.html">helloWorld</prop>  
  12. <!-- 可以有多個 -->  
  13. </props>  
  14. </property>  
  15.   
  16. </bean>  
 

這樣就會找到相應的hello.jsp 頁面了,將頁面由服務器解析給瀏覽器用戶。如果用戶輸入的 url 地址爲: http://www.xxx.com/helloworld.html ,則會被 HelloController 的helloWorld() 方法執行。

要點:這裏要注意的是,web.xml 中的過濾路徑不能爲“ /* ”,應當改爲 *.html 或者其他的形式,在 SimpleUrlHandlerMapping 的配置中,設置爲“ /hello*.html ”這裏表示只要以 hello開頭,以“ .html ”結尾的 url 地址形式都由相對應的 helloController 來處理,然後轉向PropertiesMethodNameResolver 中,對於具體的“ /hello.html ”形式的 url 由控制器中的hello 方法來執行,對於形式爲“ /helloworld.html ”形式的 url 地址由其中的 helloWorld方法來執行。然後根據方法中定義的視圖轉向對應的處理頁面。

下面附上我的例子代碼:

web.xml

Java代碼  收藏代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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">  
  3.   <display-name>spring-mvc</display-name>  
  4.   <welcome-file-list>  
  5.     <welcome-file>index.html</welcome-file>  
  6.     <welcome-file>index.htm</welcome-file>  
  7.     <welcome-file>index.jsp</welcome-file>  
  8.   </welcome-file-list>  
  9.     <filter>  
  10.         <filter-name>CharacterEncodingFilter</filter-name>  
  11.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  12.           
  13.         <init-param>  
  14.             <param-name>encoding</param-name>  
  15.             <param-value>UTF-8</param-value>  
  16.         </init-param>  
  17.           
  18.         <init-param>  
  19.             <param-name>forceEncoding</param-name>  
  20.             <param-value>true</param-value>  
  21.         </init-param>  
  22.           
  23.     </filter>  
  24.       
  25.     <filter-mapping>  
  26.         <filter-name>CharacterEncodingFilter</filter-name>  
  27.         <url-pattern>/*</url-pattern>  
  28.     </filter-mapping>  
  29.       
  30.     <servlet>  
  31.         <servlet-name>dispatcher</servlet-name>  
  32.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  33.         <init-param>  
  34.             <param-name>contextConfigLocation</param-name>  
  35.             <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>  
  36.         </init-param>  
  37.         <load-on-startup>1</load-on-startup>  
  38.     </servlet>  
  39.       
  40.     <servlet-mapping>  
  41.         <servlet-name>dispatcher</servlet-name>  
  42.         <url-pattern>*.html</url-pattern>  
  43.     </servlet-mapping>  
  44.     
  45. </web-app>  
 這裏的contextConfigLocation可以指定相應路徑下的配置文件,多個文件可以之間用逗號隔開。
dispatcher-servlet.xml
Java代碼  收藏代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
  6.   
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans    
  8.             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
  9.             http://www.springframework.org/schema/context    
  10.             http://www.springframework.org/schema/context/spring-context-3.0.xsd    
  11.             http://www.springframework.org/schema/tx    
  12.             http://www.springframework.org/schema/tx/spring-tx-3.0.xsd    
  13.             http://www.springframework.org/schema/jdbc    
  14.             http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">  
  15.   
  16.     <bean id="simpleUrlMapping"  
  17.         class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">  
  18.         <property name="mappings">  
  19.             <props>  
  20.                 <prop key="/hello*.html">helloController</prop>  
  21.             </props>  
  22.         </property>  
  23.     </bean>  
  24.   
  25.     <bean id="helloController" class="com.ctq.action.HelloController">  
  26.         <property name="message">  
  27.             <value>Hello Relic~~~~!!</value>  
  28.         </property>  
  29.         <property name="methodNameResolver">  
  30.             <ref bean="methodNameResolver" />  
  31.         </property>  
  32.     </bean>  
  33.   
  34.     <bean id="methodNameResolver"  
  35.         class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">  
  36.         <property name="mappings">  
  37.             <props>  
  38.                 <prop key="/hello.html">hello</prop>  
  39.                 <prop key="/helloworld.html">helloWorld</prop>  
  40.                 <!-- 可以有多個 -->  
  41.             </props>  
  42.         </property>  
  43.     </bean>  
  44.   
  45. </beans>  
 HelloController.java
Java代碼  收藏代碼
  1. import javax.servlet.http.HttpServletRequest;  
  2. import javax.servlet.http.HttpServletResponse;  
  3.   
  4. import org.springframework.web.servlet.ModelAndView;  
  5. import org.springframework.web.servlet.mvc.multiaction.MultiActionController;  
  6.   
  7. public class HelloController extends MultiActionController {  
  8.     private String message;  
  9.   
  10.     public String getMessage() {  
  11.         return message;  
  12.     }  
  13.   
  14.     public void setMessage(String message) {  
  15.         this.message = message;  
  16.     }  
  17.   
  18.     public ModelAndView hello(HttpServletRequest req, HttpServletResponse res)  
  19.             throws Exception {  
  20.         System.out.println("http://www.xxx.com/hello.html調用的是hello方法");  
  21.         return new ModelAndView("/jsp/hello.jsp""message", message); //返回jsp文件夾中的hello.jsp  
  22.     }  
  23.   
  24.     public ModelAndView helloWorld(HttpServletRequest req,  
  25.             HttpServletResponse res) {  
  26.         System.out.println("http://www.xxx.com/helloworld.html調用的是helloWorld方法");  
  27.         return new ModelAndView("/jsp/hello.jsp""message", message);  
  28.     }  
  29.   
  30. }  
hello.jsp頁面
Java代碼  收藏代碼
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>hello</title>  
  8. </head>  
  9. <body>  
  10. <h1>WELCOME   Index</h1>  
  11. <h2>Hello</h2>  
  12. <h2>${message}</h2>  
  13. </body>  
  14. </html>  
 
我總結的方式是SimpleUrlHandlerMapping+ PropertiesMethodNameResolver的方式,至於這種方式的優缺點,相關的介紹有很多,也可以找相關的書籍(我是在spring in action中瞭解的)。
發佈了28 篇原創文章 · 獲贊 92 · 訪問量 58萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章