在spring3.1之前使用DefaultAnnotationHandlerMapping映射器,AnnotationMethodHandlerAdapter注解适配器
在spring3.1之后使用org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping 映射器,
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter 注解适配器
建议使用3.1之后的
使用<mvc:annotation-driven></mvc:annotation-driven>这个标签可以代替上面注解映射器和注解适配器 的配置,而且默认加载很多参数绑定方法,比如json转换解析器就默认加载了。实际开发一般使用<mvc:annotation-driven></mvc:annotation-driven>。
开发注解Handler
使用@org.springframework.stereotype.Controller表明他是一个控制器。
注解的映射器,适配器
对于注解的Handler可以单个配置
<bean class="com.ssm.controller.ItemsHandler3" ></bean>
在实际开发中建议使用组件扫描:(可以扫描controller,service。。。)
<context:component-scan base-package="com.ssm.controller"></context:component-scan>
声明Controller
@org.springframework.stereotype.Controller
public class ItemsHandler3 {
//@RequestMapping实现对方法和url进行映射,一个方法对应一个url
//一般建议将url与方法名写成一样
@RequestMapping("/queryItems")
public ModelAndView queryItems() throws Exception{
//调用service 查询商品列表,这里使用静态数据模拟
ArrayList<Items>list=new ArrayList<Items>();
list.add(new Items("小米",1244,"性价比最高"));
list.add(new Items("华为",3999,"华为荣耀,你值得拥有"));
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("list", list);
modelAndView.setViewName("/items.jsp");
return modelAndView;
}
}
这里的RequestMapping里的参数,即指url的请求参数。访问链接:http://localhost:8080/SpringStudy/queryItems.action
第一步:前端控制器接受请求
调用doDispatcher(HttpServletRequest request , HttpServletResponse response){...};
第二步:前端控制器查找Handler
HandlerExecutionChain getHandler(HttpServletRequest request){...}
第三步:执行Handler,得到执行的结果ModelAndView。
handler.handle(processedRequest,request, mappedHandler.getHandler());
第四步:视图渲染,将model数据填充到 request 域
试图解析得到view,然后调用view.render() 即view的渲染方法。
void exposeModelAsRequestAttributes(Map<String,Object> model ,HttpServletRequest request){...} , 然后便是把model中的属性设置到request中。