zoukankan      html  css  js  c++  java
  • spring+springmvc+mybatis详细运转流程

    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. class= </bean>  
     

    比如

    Java代码  收藏代码
    1. class="com.ctq.action.HelloController">  
     

    现在由 HelloController 类来进行相应的处理操作(推荐 HelloController 继承MultiActionController ),

    3. 然后 ipatcherServlet询问配置文件中的视图解析器 PropertiesMethodNameResolver(个人推荐用这种,共有 3 种: InternalResourceViewResolver ,ParameterMethodNameResolver , PropertiesMethodNameResolver 。我觉得后两种都还可以,第一种虽然为默认情况,但适合小型简单的应用)。若采用这种视图解析器,需要在第 3 步中的bean 配置中加入 methodNameResolver 属性,以代替默认的 InternalResourceViewResolver配置。比如:

    Java代码  收藏代码
    1. 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代码  收藏代码
    1.  id= class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
    2. true</param-value>  
    3. class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    4. </web-app>  
     这里的contextConfigLocation可以指定相应路径下的配置文件,多个文件可以之间用逗号隔开。
    dispatcher-servlet.xml
    Java代码  收藏代码
    1. //www.springframework.org/schema/beans    
    2. //www.springframework.org/schema/beans/spring-beans-3.0.xsd    
    3. //www.springframework.org/schema/context    
    4. //www.springframework.org/schema/context/spring-context-3.0.xsd    
    5. //www.springframework.org/schema/tx    
    6. //www.springframework.org/schema/tx/spring-tx-3.0.xsd    
    7. //www.springframework.org/schema/jdbc    
    8. //www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">  
    9. class= class= class= </beans>  
     HelloController.java
    Java代码  收藏代码
    1. import javax.servlet.http.HttpServletRequest;  
    2. import javax.servlet.http.HttpServletResponse;  
    3. import org.springframework.web.servlet.ModelAndView;  
    4. import org.springframework.web.servlet.mvc.multiaction.MultiActionController;  
    5. public class HelloController extends MultiActionController {  
    6. private String message;  
    7. public String getMessage() {  
    8. return message;  
    9. public void setMessage(String message) {  
    10. this.message = message;  
    11. public ModelAndView hello(HttpServletRequest req, HttpServletResponse res)  
    12. throws Exception {  
    13. return new ModelAndView(//返回jsp文件夹中的hello.jsp  
    14. public ModelAndView helloWorld(HttpServletRequest req,  
    15. return new ModelAndView( }  
    hello.jsp页面
    Java代码  收藏代码
    1.   
    2.   >  
    3. </html>  
     
    我总结的方式是SimpleUrlHandlerMappingPropertiesMethodNameResolver的方式,至于这种方式的优缺点,相关的介绍有很多,也可以找相关的书籍(我是在spring in action中了解的)。
  • 相关阅读:
    hdu5360 Hiking(水题)
    hdu5348 MZL's endless loop(欧拉回路)
    hdu5351 MZL's Border(规律题,java)
    hdu5347 MZL's chemistry(打表)
    hdu5344 MZL's xor(水题)
    hdu5338 ZZX and Permutations(贪心、线段树)
    hdu 5325 Crazy Bobo (树形dp)
    hdu5323 Solve this interesting problem(爆搜)
    hdu5322 Hope(dp)
    Lightoj1009 Back to Underworld(带权并查集)
  • 原文地址:https://www.cnblogs.com/zftxdh/p/7081687.html
Copyright © 2011-2022 走看看