springmvc(三)註解的處理器對映器和介面卡
阿新 • • 發佈:2019-01-08
在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
配置註解對映器和介面卡
使用 mvc:annotation-driven代替上邊註解對映器和註解介面卡配置,mvc:annotation-driven預設載入很多的引數繫結方法,比如json轉換解析器就預設載入了,如果使用mvc:annotation-driven不用配置上邊的RequestMappingHandlerMapping和RequestMappingHandlerAdapter。實際開發時使用mvc:annotation-driven。
開發註解Handler
使用註解的對映器和註解的介面卡。(註解的對映器和註解的介面卡必須配對使用)
@Controller public class ItemsController3 { @RequestMapping("/queryItems") public ModelAndView queryItems() throws Exception { System.out.println("===================ItemsController3======================"); List<Items> itemsList = new ArrayList<Items>(); 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 = new ModelAndView(); modelAndView.addObject("itemsList", itemsList); modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp"); return modelAndView; } }
在spring容器中載入Handler
部署除錯
訪問:http://localhost:8080/springmvc/queryItems.action