1 非注解的處理器映射器和處理器擴充卡
非注解的處理器映射器
<!-- 處理器映射器
将Bean的Name作為url進行查找,需要在配置Handler時指定beanname(就是url)
-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!-- 另一種處理器映射器 -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/queryItem3.action">ItemsController1</prop>
<prop key="/queryItem4.action">ItemsController2</prop>
</props>
</property>
</bean>
多個處理器映射器可以并存,前端控制器判斷url能讓哪些處理器映射器映射,就讓正确的映射器處理。 非注解的處理器擴充卡
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter
要求編寫的Handler實作 Controller接口。
org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter
要求編寫的Handler實作 HttpRequestHandler接口。
package cn.minyan.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.HttpRequestHandler;
import cn.minyan.bean.Items;
public class ItemsController2 implements HttpRequestHandler{
@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//調用service查找資料庫,查詢商品清單,這裡使用靜态資料模拟
List<Items> itemList = new ArrayList<Items>();
Items item1 = new Items();
item1.setName("聯想筆記本");
item1.setPrice(3000f);
item1.setDetail("ThinkPad T430聯想筆記本");
Items item2 = new Items();
item2.setName("蘋果手機");
item2.setPrice(5200f);
item2.setDetail("Iphone5S");
itemList.add(item1);
itemList.add(item2);
request.setAttribute("itemList", itemList);
request.getRequestDispatcher("/WEB-INF/jsp/items/items.jsp").forward(request, response);
}
}
//使用此方法可以通過修改response,設定響應的資料格式,比如響應json資料
response.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
response.getWriter().write("json串");
-------------------------------------------------------------------------------------------------------------------------------------------------------------
以下的注解的處理器映射器和處理器擴充卡才是開中常用的:
-------------------------------------------------------------------------------------------------------------------------------------------------------------
2 注解的處理器映射器和處理器擴充卡
在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注解擴充卡。
配置注解映射器和擴充卡:
<!-- 注解處理器映射器 -->
<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> -->
開發注解的處理器映射器和處理器擴充卡:
注解的映射器和注解的擴充卡必須配對使用
//使用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("聯想筆記本");
items_1.setPrice(6000f);
items_1.setDetail("ThinkPad T430 聯想筆記本電腦!");
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.minyan.controller.ItemsController3" /> -->
<!-- 可以掃描controller、service、...
這裡讓掃描controller,指定controller的包
-->
<context:component-scan base-package="cn.minyan.controller"></context:component-scan>