天天看點

【SpringMVC架構】注解的處理器映射器和擴充卡配置

下面我們來探讨注解的處理器映射器和擴充卡

1.注解的處理器映射器和擴充卡

在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注解擴充卡。

2.配置注解映射器和擴充卡。

在springmvc.xml中寫:

<!-- 注解映射器 -->
<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>-->
           

下面的開發可能就與非注解的産生了翻天覆地的變化了

3.開發注解Handler

使用注解的映射器和注解的擴充卡。(注解的映射器和注解的擴充卡必須配對使用)

ItemsController3:

package cn.edu.hpu.ssm.controller;

import java.util.ArrayList;
import java.util.List;


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

import cn.edu.hpu.ssn.po.Items;

//注解的Handler類
//使用@Controller來辨別它是一個控制器
@Controller
public class ItemsController3 {
	
	//商品查詢清單
	//@RequestMapping實作 對queryItems方法和url進行映射,一個方法對應一個url
	//一般建議将url和方法寫成一樣
	@RequestMapping("/queryItems")
	public ModelAndView queryItems()throws Exception{
		
		//調用Service查找資料庫,查詢商品清單,這裡使用靜态資料模拟
		List<Items> itemsList=new ArrayList<Items>();
		//向List中填充靜态資料
		Items items_1 = new Items();
		items_1.setName("聯想筆記本22222222");
		items_1.setPrice(6000f);
		items_1.setDetail("ThinkPad T430 聯想筆記本電腦!");
		
		Items items_2 = new Items();
		items_2.setName("蘋果手機22222222");
		items_2.setPrice(5000f);
		items_2.setDetail("iphone6蘋果手機!");
		
		itemsList.add(items_1);
		itemsList.add(items_2);
		
		//傳回ModelAndView
		ModelAndView modelAndView=new ModelAndView();
		//相當于request的setAttribut,在jsp頁面中通過這個來取資料
		modelAndView.addObject("itemsList",itemsList);
		
		//指定視圖
		modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");


		return modelAndView;
	}
	
	//定義其它方法
	//商品添加
	//商品删除
}
           

這個注解的Handler的好處還有可以在類中在添加其他的方法,如商品添加、商品删除等。

5.3在spring容器中加載Handler

<!-- 對于注解的Handler可以單個配置,
	實際開發中建議使用掃描元件,因為一個一個配bean太麻煩 -->
	<!--  <bean class="cn.edu.hpu.ssm.controller.ItemsController3"></bean>-->


	<!-- 可以掃描controller、service、... 
	這裡讓掃描controller,指定controller的包-->
	<context:component-scan base-package="cn.edu.hpu.ssm.controller"></context:component-scan>
           

看一下最終的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太麻煩 -->
	<!--  <bean class="cn.edu.hpu.ssm.controller.ItemsController3"></bean>-->


	<!-- 可以掃描controller、service、... 
	這裡讓掃描controller,指定controller的包-->
	<context:component-scan base-package="cn.edu.hpu.ssm.controller"></context:component-scan>
	
	<!-- 注解映射器 -->
	<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>-->
	
	<!-- 視圖解析器 
	解析jsp解析,預設使用jstl标簽,classpath下的得有jstl的包
	 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	</bean>
</beans>
           

5.4部署調試

通路:http://localhost:8080/springmvcfirst1208/queryItems.action

通路結果如圖

【SpringMVC架構】注解的處理器映射器和擴充卡配置

下一篇總結我們來分析一下springmvc中一些源碼的分析

轉載請注明出處:http://blog.csdn.net/acmman/article/details/46980427

繼續閱讀