本文主要介紹注解的處理器映射器和適配器相關配置
前端控制器從\org\springframework\web\servlet\DispatcherServlet.properties
件中加載處理器映射器、適配器、視圖解析器等組件,如果不在springmvc.xml中配置,則使用默認加載的
注解的處理器映射器和適配器
在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
注解適配器
<code class="language-xml hljs "><!--{cke_protected}{C}%3C!%2D%2D%20%E6%B3%A8%E8%A7%A3%E7%9A%84%E6%98%A0%E5%B0%84%E5%99%A8%20%2D%2D%3E--> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"> <!--{cke_protected}{C}%3C!%2D%2D%20%E6%B3%A8%E8%A7%A3%E7%9A%84%E9%80%82%E9%85%8D%E5%99%A8%20%2D%2D%3E--> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> </bean></bean></code>
或者
<code class="language-xml hljs "> <!--{cke_protected}{C}%3C!%2D%2D%20%E4%BD%BF%E7%94%A8mvc%3Aannotation-driven%E4%BB%A3%E6%9B%BF%E4%B8%8A%E9%9D%A2%E4%B8%A4%E4%B8%AA%E6%B3%A8%E8%A7%A3%E6%98%A0%E5%B0%84%E5%99%A8%E5%92%8C%E6%B3%A8%E8%A7%A3%E9%80%82%E9%85%8D%E7%9A%84%E9%85%8D%E7%BD%AE%0A%20%20%20%20%20mvc%3Aannotation-driven%E9%BB%98%E8%AE%A4%E5%8A%A0%E8%BD%BD%E5%BE%88%E5%A4%9A%E7%9A%84%E5%8F%82%E6%95%B0%E7%BB%91%E5%AE%9A%E6%96%B9%E6%B3%95%EF%BC%8C%0A%20%20%20%20%20%E6%AF%94%E5%A6%82json%E8%BD%AC%E6%8D%A2%E8%A7%A3%E6%9E%90%E5%99%A8%E9%BB%98%E8%AE%A4%E5%8A%A0%E8%BD%BD%E4%BA%86%EF%BC%8C%E5%A6%82%E6%9E%9C%E4%BD%BF%E7%94%A8mvc%3Aannotation-driven%E5%88%99%E4%B8%8D%E7%94%A8%E9%85%8D%E7%BD%AE%E4%B8%8A%E9%9D%A2%E7%9A%84RequestMappingHandlerMapping%E5%92%8CRequestMappingHandlerAdapter%0A%20%20%20%20%20%E5%AE%9E%E9%99%85%E5%BC%80%E5%8F%91%E6%97%B6%E4%BD%BF%E7%94%A8mvc%3Aannotation-driven%0A%20%20%20%20%20%2D%2D%3E--> </code>
使用注解的映射器和注解的適配器。(使用注解的映射器和注解的適配器必須配對使用)
//使用@Controller來標識它是一個控制器
@Controller
public class ItemsController3 {
//商品查詢列表
@RequestMapping("/queryItems")
//實現 對queryItems方法和url進行映射,一個方法對應一個url
//一般建議將url和方法寫成一樣
public ModelAndView queryItems() throws Exception{
//調用service查找數據庫,查詢商品列表,這裡使用靜態數據模擬
List itemsList = new ArrayList();
//向list中填充靜態數據
Items items_1 = new Items();
items_1.setName("聯想筆記本");
items_1.setPrice(6000f);
items_1.setDetail("ThinkPad T430 c3 聯想筆記本電腦!");
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的setAttribute方法,在jsp頁面中通過itemsList取數據
modelAndView.addObject("itemsList",itemsList);
//指定視圖
modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
return modelAndView;
}
}
<code class="language-xml hljs "><!--{cke_protected}{C}%3C!%2D%2D%20%E5%AF%B9%E4%BA%8E%E6%B3%A8%E8%A7%A3%E7%9A%84Handler%20%E5%8F%AF%E4%BB%A5%E5%8D%95%E4%B8%AA%E9%85%8D%E7%BD%AE%0A%20%20%20%20%E5%AE%9E%E9%99%85%E5%BC%80%E5%8F%91%E4%B8%AD%E5%8A%A0%E4%BD%A0%E4%BD%BF%E7%94%A8%E7%BB%84%E4%BB%B6%E6%89%AB%E6%8F%8F%0A%20%20%20%20%2D%2D%3E--> <!--{cke_protected}{C}%3C!%2D%2D%20%20%3Cbean%20%20class%3D%22com.iot.ssm.controller.ItemsController3%22%2F%3E%20%2D%2D%3E--> <!--{cke_protected}{C}%3C!%2D%2D%20%E5%8F%AF%E4%BB%A5%E6%89%AB%E6%8F%8Fcontroller%E3%80%81service%E3%80%81...%0A%20%20%20%20%E8%BF%99%E9%87%8C%E8%AE%A9%E6%89%AB%E6%8F%8Fcontroller%EF%BC%8C%E6%8C%87%E5%AE%9Acontroller%E7%9A%84%E5%8C%85%0A%20%20%20%20%20%2D%2D%3E--> <context:component-scan base-package="com.iot.ssm.controller"></context:component-scan></code>