spring+springmvc+mybatis詳細運轉流程
spring+springmvc+mybatis詳細運轉流程
2016-04-14 23:38 1892人閱讀 評論(0) 收藏 舉報 分類:最近在整合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 ”。
- class= </bean>
比如
Java代碼- class="com.ctq.action.HelloController">
現在由 HelloController 類來進行相應的處理操作(推薦 HelloController 繼承MultiActionController ),
3. 然後 D ipatcherServlet詢問配置文件中的視圖解析器 PropertiesMethodNameResolver(個人推薦用這種,共有 3 種: InternalResourceViewResolver ,ParameterMethodNameResolver , PropertiesMethodNameResolver 。我覺得後兩種都還可以,第一種雖然為默認情況,但適合小型簡單的應用)。若采用這種視圖解析器,需要在第 3 步中的bean 配置中加入 methodNameResolver 屬性,以代替默認的 InternalResourceViewResolver配置。比如:
- class= class= </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代碼- id= class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
- true</param-value>
- class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- </web-app>
- //www.springframework.org/schema/beans
- //www.springframework.org/schema/beans/spring-beans-3.0.xsd
- //www.springframework.org/schema/context
- //www.springframework.org/schema/context/spring-context-3.0.xsd
- //www.springframework.org/schema/tx
- //www.springframework.org/schema/tx/spring-tx-3.0.xsd
- //www.springframework.org/schema/jdbc
- //www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">
- class= class= class= </beans>
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.springframework.web.servlet.ModelAndView;
- import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
- public class HelloController extends MultiActionController {
- private String message;
- public String getMessage() {
- return message;
- public void setMessage(String message) {
- this.message = message;
- public ModelAndView hello(HttpServletRequest req, HttpServletResponse res)
- throws Exception {
- return new ModelAndView(//返回jsp文件夾中的hello.jsp
- public ModelAndView helloWorld(HttpServletRequest req,
- return new ModelAndView( }
- >
- </html>
spring+springmvc+mybatis詳細運轉流程