SpringMVC使用非注解方式和注解方式

Spring:SpringMVC 使用非注解方式和注解方式

SpringMVC配置的组件众多,如果单纯的在xml文件中进行配置,需要配置处理器映射器,处理器适配器,处理器,处理器解析器.尤其是处理器的配置,并且还得指定处理器的name,才能完成请求地址到handler之间的映射.相比较于注解方式,注解方式实在是太麻烦了.

但是,我们还是先来看看两种配置方式的具体做法吧!

1.1 使用非注解方式

1.1.1 处理器映射器

使用BeanNameUrlHandlerMapping:

它能够完成Handler的bean的name和url之间的映射

1
2
3
4
5
6
7
8
<!--配置处理器映射器-->
<!--根据url查找对应name值的Handler-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

<!--配置我们自定义的Handler(处理器)-->
<!--它会被BeanNameUrlHandlerMapping类型的处理器映射器映射匹配,根据它的name值-->
<bean name="/user.action" class="com.lee.ssm.controller.UserController"/>
<bean name="/book.action" class="com.lee.ssm.controller.BookController"/>

使用SimpleUrlHandlerMapping:

它可以批量完成Handler和url之间的映射

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!--通过Handler的ID值完成url和Handler的映射-->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<!--key是地址-->
<!--值为Handler的ID-->
<prop key="/user.action">userController</prop>
<prop key="/book.action">bookController</prop>
</props>
</property>
</bean>

<!--配置我们自定义的Handler(处理器)-->
<!--它会被BeanNameUrlHandlerMapping类型的处理器映射器映射匹配,根据它的name值-->
<bean name="/user.action" id="userController" class="com.lee.ssm.controller.UserController"/>
<bean name="/book.action" id="bookController" class="com.lee.ssm.controller.BookController"/>

上述两种方式都能完成url到handler的映射

1.1.2 处理器适配器

  • 使用:SimpleControllerHandlerAdapter,它能够调用的Handler必须实现org.springframework.web.servlet.mvc包下的Controller接口

    UserController处理器:

1
2
3
4
5
6
7
8
9
10
11
12
public class UserController implements Controller {

@Override
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ModelAndView modelAndView = new ModelAndView();
// 设置模型数据,通过键值对的形式设置,在页面中就像从response中取数据一样取出来
modelAndView.addObject("msg", "Hello World!");
// 设置视图,这里设置了一个链接,相当于转发
modelAndView.setViewName("jsp/helloworld.jsp");
return modelAndView;
}
}

SimpleControllerHanderAdapter的配置:

1
2
3
<!--配置处理器适配器-->
<!--他只能适配Controller的实现类型的Controller-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
  • 使用HttpRequestHandlerAdapter,他只能适配HttpRequestHandler实现类型的Controller

    OrdersController处理器:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    public class OrdersController implements HttpRequestHandler {
    @Override
    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // 接下来的开发像极了使用Servlet的原生开发
    response.getWriter().print(getClass().getSimpleName() + ",hello!");

    request.getRequestDispatcher("jsp/helloworld.jsp").forward(request, response);
    }
    }

    HttpRequestHandlerAdapter配置:

    1
    2
    <!--他只能适配HttpRequestHandler实现类型的Controller-->
    <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>

    上面两种非注解的处理器适配器能够适配不同类型的Handler(也称作Controller)

使用注解的方式

applicationContext.xml中配置:

1
2
3
4
5
<!--使用注解的方式配置Handler以后,需要将处理器映射器和处理器适配器配置为以下的方式-->
<!--配置处理器映射器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<!--配置处理器适配器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

或者使用:

1
<mvc:annotation-driven/>

上面这一句就可以在Spring容器中自动的帮我们注册RequestMappingHandlerMappingRequestMappingHandlerAdapter.

编写Controller类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Controller
public class FirstAnnoController {
// 可以放回很多中类型的值,这里选择返回ModelAndView,他将返回的结果交给视图解析器进行解析,然后返回给前端控制器
@RequestMapping("/firstanno.action")
public ModelAndView request1() {
ModelAndView modelAndView = new ModelAndView();
// 设置模型数据,通过键值对的形式设置,在页面中就像从response中取数据一样取出来
modelAndView.addObject("msg", "Hello World!");
// 设置视图,这里设置了一个链接,相当于转发
modelAndView.setViewName("jsp/helloworld.jsp");

return modelAndView;
}

@RequestMapping("/secondanno.action")
public String request2() {
// redirect:表示重定向
return "redirect:/index.jsp";
}

RequestMappingHandlerMapping能够完成url到该方法的映射.另外,FirstAnnoController中也可以配置多个方法,只要他们的@RequestMapping注解中value不一样就可以,这样一个Controller中可以实现多种功能.

另外:在applicationContext.xml中开启组件扫描,@Controller就能被自动扫描到并被Spring容器管理

测试:在浏览器中输入地址:http://localhost:8080/ssm/firstanno.action就能自动跳转到jsp/helloworld.jsp页面