標籤: springmvc前端
[TOC]java
本文主要介紹註解的處理器映射器和適配器相關配置git
前端控制器從\org\springframework\web\servlet\DispatcherServlet.properties
件中加載處理器映射器、適配器、視圖解析器等組件,若是不在springmvc.xml中配置,則使用默認加載的github
註解的處理器映射器和適配器web
org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
註解映射器。org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
註解映射器。org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
註解適配器。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"/>
或者spring
<!-- 使用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") //實現 對queryItems方法和url進行映射,一個方法對應一個url //通常建議將url和方法寫成同樣 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 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; } }
<!-- 對於註解的Handler 能夠單個配置 實際開發中加你使用組件掃描 --> <!-- <bean class="com.iot.ssm.controller.ItemsController3"/> --> <!-- 能夠掃描controller、service、... 這裏讓掃描controller,指定controller的包 --> <context:component-scan base-package="com.iot.ssm.controller"></context:component-scan>