SpringMVC总结

一、基本原理:

 

1、用户发送请求至DispatcherServlet;

2、DispatcherServlet请求HandlerMapping查找Handler;

3、HandlerMapping向DispatcherServlet返回Handler;

4、DispatcherServlet请求HandlerAdapter执行Handler;

5、HandlerAdapter执行Handler;

6、Handler返回一个ModelAndView对象给HandlerAdapter,ModelAndView中包含模型数据和逻辑视图名;

7、HandlerAdapter返回ModelAndView给DispatcherServlet;

8、DispatcherServlet请求ViewResolver将逻辑视图名解析为具体的View;

9、ViewResolver将解析出的具体的View返回给DispatcherServlet;

10、     View根据传过来的Model数据进行渲染,Model实际上是一个Map;

11、     视图渲染完毕后最终由DispatcherServlet响应给用户;

 

二、编写业务逻辑处理Handler类:

public class ItemsControllerA implements Controller

{

    @Override

    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)

    {

              ModelAndView mv = new ModelAndView(); 

             //添加模型数据 可以是任意的POJO对象 

            mv.addObject("message", "Hello World!"); 

            //设置逻辑视图名,视图解析器会根据该名字解析到具体的视图页面 

            mv.setViewName("hello"); 

            return mv; 

       }

}

 

三、配置(非注解方式):

1、在web.xml文件中配置:

<servlet>

    <servlet-name>DispatchServlet</servlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <init-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>classpath:spring-mvc.xml</param-value>

    </init-param>

</servlet>

 

<servlet-mapping>

    <servlet-name>DispatchServlet</servlet-name>

    <url-pattern>*.action</url-pattern>

</servlet-mapping>

 

2、spring-mvc.xml文件配置:

<!-- Handler -->

<bean id="ItemsControllerA" name="/queryItems.action" class="com.dstudiow.ssm.controller.ItemsControllerA"/>

 

<!-- BeanNameUrlHandlerMapping: 将bean的name作为url查找,需在配置Handler指定bean name,即url -->

<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

 

<!-- SimpleControllerHandlerAdapter: 要求Handler实现Controller接口 -->

<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

 

<!-- ViewResolver -->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/WEB-INF/jsp/"/>

<property name="suffix" value=".jsp"/>

   </bean>

 

四、配置(注解方式):

  1. 在web.xml文件中配置:

<servlet>

    <servlet-name>DispatchServlet</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <init-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>classpath:spring-mvc.xml</param-value>

    </init-param>

</servlet>

 

<servlet-mapping>

    <servlet-name>DispatchServlet</servlet-name>

    <url-pattern>*.action</url-pattern>

</servlet-mapping>

 

      2.spring-mvc.xml配置:

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> 

 

<context:component-scan base-package="com.demo2do" /> 

 

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 

    <property name="prefix" value="/" /> 

    <property name="suffix" value=".jsp" /> 

</bean>

 

或者:

<!-- 支持mvc注解驱动 -->

<mvc:annotation-driven /> 

 

<context:component-scan base-package="com.demo2do" /> 

 

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 

    <property name="prefix" value="/" /> 

    <property name="suffix" value=".jsp" /> 

</bean> 

 

五、参考资料:

http://downpour.iteye.com/category/196182

 

posted on 2018-03-19 16:14  小夏coding  阅读(117)  评论(0编辑  收藏  举报

导航