SpringMVC (二)非注解开发和注解开发

非注解的映射器

所有的映射器都实现了HandlerMapping接口。

原始的映射器:org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping
Handler:<bean name="/queryItems.action" 


class="cn.itcast.ssm.controller.ItemsController1"></bean>

映射器和Handler合体配置:SimpleUrlHandlerMappering

需要在Controller的bean内增加id="",把Controller的bean增加到简单url映射器内。

简单url映射器:
<!-- 配置Handler -->
<bean id="itemsController1" name="/queryItems.action" 


class="cn.itcast.ssm.controller.ItemsController1" />


<!-- 处理器映射器
将Bean的Name作为url进行查找,需要在配置Handler时指定beanname(就是url) -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />


<!-- 简单url映射 -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
<property name="mappings">
<props>
<!-- 对itemsController进行url映射:url是/queryItems1.action -->
<prop key="/queryItems1.action">itemsController1</prop>
<prop key="/queryItems2.action">itemsController1</prop>
</props>
</property>
</bean>


多个映射器可以并存,前端控制器会判断url能让哪些映射器映射,就让正确的映射器处理。
====================================================================================
非注解的适配器
SimpleControllerHandlerAdapter要求编写Handler实现Controller接口。
<!-- 处理器适配器
所有处理器适配器都实现HandlerAdapter接口 -->

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

HttpRequestHandlerAdapter要求编写的Handler实现HttpRequestHandler

<!-- 另一个非注解的适配器 -->
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter" />


配置Controller2

public class ItemsController2 implements HttpRequestHandler {


@Override
public void handleRequest(HttpServletRequest request, 
HttpServletResponse response) throws ServletException,IOException {
//调用service查找数据库,查询商品,这里先使用静态模拟
List<Items> itemsList = new ArrayList<Items>();
Items items_1 = new Items();
items_1.setName("联想笔记本");
items_1.setPrice(6000f);
items_1.setDetail("ThinkPad 笔记本");


Items items_2 = new Items();
items_2.setName("苹果笔记本");
items_2.setPrice(5000f);
items_2.setDetail("iphone6苹果手机");


itemsList.add(items_1);
itemsList.add(items_2);


request.setAttribute("itemsList",itemsList);
request.getRequestDispatcher("/WEB-INF/jsp/items/itemsList.jsp").forward(request,response);


}
}


配置另外一个Handler
<!-- 配置另外一个Handler -->
<bean id="itemsController3" class="cn.itcast.ssm.controller.ItemsController2" />


所有的


后面可以随便选择一个映射器
<prop key="/queryItems3.action">itemsController3</prop>


通过这种方式没有返回ModelAndView,可以通过response修改定义响应内容,比如返回json数据:
response.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
response.getWriter().write("json串");
多个适配器也可以并存
======================================================================================
注解的处理器和映射器

DispatcherSerlvet.properties

删除全部的适配器、映射器、视图解析器等组件,springmvc也能正常运行。

前端控制器会从文件中加载处理器映射器等组件,如果不在springmvc.xml中配置,使用默认加载的。但是有


分3.1之前和之后。


3.1之前使用org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping注解映


射器。
3.1之后使用org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping注


解的映射器




3.1之前使用org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter注解适


配器。
3.1之后使用org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter注


解适配器。


配置注解的映射器和适配器


<!-- 注解映射器 -->
<bean 


class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
<!-- 注解适配器 -->
<bean 


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


转换解析器就默认加载了,如果使用mvc:annotation-driven不用配置上边的


RequestMappingHandlerMapping和Adapter
实际开发使用:mvc:annotation-driven
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">


<!-- 配置Handler -->
<bean id="itemsController1" name="/queryItems_test.action" 


class="cn.itcast.ssm.controller.ItemsController1" />


<!-- 对于注解的Handler可以单个配置-->
<bean class="cn.itcast.ssm.controller.ItemsController3" /> 
<!-- 但是在实际开发中建议使用组件扫描 ,可以扫描controller、service
<context:component-scan base-package="cn.itcast.ssm.controller"></context:component-scan>-->


<!-- 处理器映射器
将Bean的Name作为url进行查找,需要在配置Handler时指定beanname(就是url) -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />


<!-- 简单url映射 -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
<property name="mappings">
<props>
<!-- 对itemsController进行url映射:url是/queryItems1.action -->
<prop key="/queryItems1.action">itemsController1</prop>
<prop key="/queryItems2.action">itemsController1</prop>
</props>
</property>
</bean>


<!-- 注解映射器 -->
<bean 


class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
<!-- 注解适配器 -->
<bean 


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


<!-- 使用mvc:annotation-driven 代替上边的注解映射器和注解适配器
mvc:annotation-driven默认加载很多的参数绑定方法,比如json转换解析器就默认加载了,如果使用mvc:


annotation-driven不用配置上边的RequestMappingHandlerMapping和RequestMappingHandlerAdapter
实际开发使用的也是这个mvc:annotation-driven 
<mvc:annotation-driven></mvc:annotation-driven>-->


<!-- 处理器适配器
所有处理器适配器都实现HandlerAdapter接口 -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
<!-- 另一个非注解的适配器 -->
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter" />
<!-- 视图解析器 
解析jsp解析,默认使用jstl标签,classpath下面得有jstl的包-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>


</beans>


可以开发注解的Handler
ItemsController3.java
//使用Controller标识他是一个控制器
@Controller
public class ItemsController3 {


//商品查询列表
//一般建议将url和方法名写成一样,方便queryItems和url进行映射,一个方法对应一个url
@RequestMapping("/queryItems")
public ModelAndView queryItems()throws Exception{
//调用service查找数据库,查询商品,这里先使用静态模拟
List<Items> itemsList = new ArrayList<Items>();
Items items_1 = new Items();
items_1.setName("联想笔记本");
items_1.setPrice(6000f);
items_1.setDetail("ThinkPad 笔记本");

Items items_2 = new Items();
items_2.setName("苹果笔记本");
items_2.setPrice(5000f);
items_2.setDetail("iphone6苹果手机");

itemsList.add(items_1);
itemsList.add(items_2);

//返回ModelAndView
ModelAndView modelAndView = new ModelAndView();
//相当于request的setAttribut,在jsp页面中通过itemsList取数据
modelAndView.addObject("itemsList",itemsList);

//指定视图
modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
return modelAndView;
}
注解的映射器和适配器要配对使用


需要在spring容器中加载Handler
<!-- 对于注解的Handler可以单个配置-->
<bean class="cn.itcast.ssm.controller.ItemsController3" /> 
<!-- 但是在实际开发中建议使用组件扫描 ,可以扫描controller、service
<context:component-scan base-package="cn.itcast.ssm.controller"></context:component-scan>-->


===============================================================================================
源码分析:
1.前端控制器接收请求,调用doDispatch
2.前端控制器调用处理器映射器查找Handler
3.调用处理器适配器执行Handler,得到执行的结果ModelAndVeiw
4.视图渲染,将model数据填充到request域
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值