標簽: springmvc
本文主要介紹注解的處理器映射器和適配器相關配置
前端控制器從\org\springframework\web\servlet\DispatcherServlet.properties
件中加載處理器映射器、適配器、視圖解析器等組件,如果不在springmvc.xml中配置,則使用默認加載的
注解的處理器映射器和適配器
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"/>
或者
<!-- 使用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>
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。