SpringMVC框架(三):注解的处理器映射器和适配器;小结;

注解的处理器映射器和适配器

在spring3.1之前使用org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping注解映射器
在spring3.1之后使用org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping注解映射器
在spring3.1之前使用org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter注解适配器
在spring3.1之后使用org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter注解适配器
1.配置注解映射器和适配器

eg:
1.1准备jsp页面
1.2准备web.xml(配置了前端控制器和映射路径)
1.3.配置容器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"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

	<!-- 注解映射器 -->
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
	
	<!-- 注解适配器 -->
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
	
	<!-- 扫描包 -->
	<context:component-scan base-package="cn.hp.ssm.controller"></context:component-scan>
		
</beans>

1.4…新建Controller基于注解

@Controller//使用标识控制器,被标记的话也是受spring管理的一个Bean,只要配置一个扫描包就能把标记的类就能装配Bean
public class ItemsController3 {
	//注解方式 查询商品列表
	@RequestMapping("/queryItems.action")//@RequestMapping对url处理方法进行映射,作为请求的处理方法,一般的话一个方法对应一个url
	public ModelAndView queryItems() throws Exception{
		
		List<Items> itemsList = new ArrayList<Items>();
		
		Items items1 = new Items();
		items1.setName("thinkpad");
		items1.setPrice(1000f);
		items1.setDetail("thinkpad T400。。。");
		
		Items items2 = new Items();
		items2.setName("dell");
		items2.setPrice(1000f);
		items2.setDetail("Dell vostro。。。");
		
		itemsList.add(items1);
		itemsList.add(items2);
		System.out.println("ItemsController3……");
		
		//返回modelAndView
		ModelAndView modelAndView = new ModelAndView();
		//相当于request的setAttribute
		modelAndView.addObject("itemsList", itemsList);
		
		//指定视图
		modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
		
		return modelAndView;
	}
}
2.可以不配置注解映射器和注解适配器

eg:
2.1准备jsp页面
2.2准备web.xml(配置了前端控制器和映射路径)
2.3.配置Controller(注解方式)
2.4…配置容器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"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

	<!-- 使用mvc:annotation-driven 可以不写注解映射器和注解适配器 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	
	<!-- 扫描包 -->
	<context:component-scan base-package="cn.hp.ssm.controller"></context:component-scan>
	
</beans>
基于配置好处:修改配置比较集中
基于注解好处:xml配置少,但修改比较分散
3.源码分析
通过前端控制器源码分析springmvc的执行过程
1.前端控制器接收请求(调用doDiapatch请求分发)
2.前端控制器调用处理器映射器查找Handler
3.调用处理器适配器执行Handler,得到执行结果ModelAndView
4.视图渲染,讲Model数据填充到request域(视图解析,得到view)
	调用view的渲染方法,将model数据填充到request域
	渲染方法view.render
4.小结
1.通过入门程序理解springmvc前端控制器,处理器映射器,处理器适配器,视图解析器用法
2.前端控制器配置
	1. [ *.action ]访问以.action结尾,由DispatchServlet进行解析
	2. [ / ] 所以访问的地址都由DispatchServlet进行解析,对于静态文件的解析需要配置不让DispatchServlet进行解析
		使用此种方法可以实现RESTful风格的url
3.处理器映射器
	1.非注解处理器映射器(了解)
	2.注解处理器映射器(掌握)
		对标记@Controller类中标识有@RequestMapping的方法进行映射。在@RequestMapping里边定义映射的url。使用注解的映射器不用在xml中配置url和Handler的映射关系
4.处理器适配器
	1.非注解处理器适配器(了解)
	2.注解处理器适配器(掌握)	
		注解处理器适配器和注解的处理器映射器是配对使用。理解为不能使用非注解映射器进行映射

5.<mvc:annotation-driven></mvc:annotation-driven>可以替代以下配置
	<!-- 注解映射器 -->
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>

	<!-- 注解适配器 -->
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
	实际开发使用:mvc:annotation-driven

6.视图解析器前缀和后缀
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	<!-- jsp路径的前缀prefix -->
	<property name="prefix" value="/WEB-INF/jsp/"></property>
	<!-- jsp路径的后缀suffix -->
	<property name="suffix" value=".jsp"></property>
</bean>	

跳转的时候在Controller里简写
	//指定视图
	//modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
	modelAndView.setViewName("items/itemsList");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值