0053 用注解方式配置Spring MVC

按照0052中的办法,如果一个站点设计有1000个请求,那就得写1000个controller,还得配置1000个<bean id="helloController" class="net.sonng.mvcdemo.controller.HelloController" ></bean>,这个工作量无疑没有必要。

实际上,还可以通过注解实现,用@Controller注解将一个类指定为Controller,就不用实现Controller接口,也不用一定是ModelAndView handleRequest(HttpServletRequest,HttpServletResponse)这个方法签名了;用@RequstMapping将请求映射到方法上,也就不用那1000条配置了

将0052中的HelloController改成下面这样:

package net.sonng.mvcdemo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller //该注解表明这是个Controller,这个类也就不用实现Controller接口了
public class HelloController {

    @RequestMapping(value="/hello")  //这个注解替代了xml中SimpleUrlHandlerMapping的配置,将请求映射到处理方法上
    public ModelAndView hello(){  //方法签名完全不用遵循handleRequest,返回值和形参也都可以不同
        ModelAndView mav=new ModelAndView();
        mav.getModel().put("msg", "这是Model中的数据,这是hello方法");
        mav.setViewName("hello");
        return mav;
    }
    @RequestMapping(value="/world") //采用注解配置,一个Controller内可以包含多个方法,对应多个不同的请求
    public ModelAndView world(){
        ModelAndView mav=new ModelAndView();
        mav.getModel().put("msg", "这是Model中的数据,这是world方法");
        mav.setViewName("hello");
        return mav;
    }
}

xml配置改为这样

    <!-- 开启组件注解扫描,扫描base-package值下面的类或子包下的类,包含了特定注解的类就注解为Spring的Bean 
         比如,上面的Controller,就将其注册为Controller组件;除此之外还有@Service、@Conponent、@Repository等-->
    <context:component-scan base-package="net.sonng.mvcdemo" />
    <!-- 这是annotation类型的处理映射器,根据请求查找映射 -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
    <!-- 这是annotation类型的处理器适配器,完成对hello和world方法的调用 -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />
    <!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="prefix" value="/WEB-INF/content/" />
        <property name="suffix" value=".jsp" />
    </bean>

部署以上代码后,到浏览器中访问:http://localhost:8080/mvcdemo/hello和http://localhost:8080/mvcdemo/world

Spring MVC的执行流程

  1. 用户向服务器发出请求,被DispatcherServlet截获
  2. DispatcherServlet对请求的url进行解析,调用handlerMapping(RequestMappingHandlerMapping)获得相关的对象,比如Handler对象、对应的拦截器对象,封装到HandlerExecutionChain对象中返回
  3. DispatcherServlet选择一个HandlerAdapter,用于处理上面返回Handler对象,调用Handler处理请求的方法,比如hello和world方法
  4. 执行handler(Controller),之前还有些预处理,比如参数类型转换,传上来的json/xml数据转成对象,或者将传上来的数据封装成一个实体对象等
  5. handler执行完毕,向DispatcherServlet返回一个ModelAndView对象(或者其他对象)
  6. 根据返回的对象,找个合适的ViewResolver视图解析器,返回给DispatcherServlet
  7. 视图解析器根据Model和View生成结果
  8. 将结果返回给客户端

其他:
上面xml配置中的这两行:

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

在Spring 4.0以后,可以用下面一行配置替代,当然注意不要少了mvc的命名空间

<mvc:annotation-driven />
原文地址:https://www.cnblogs.com/sonng/p/6635223.html