案例流程总结
二入门中涉及到的组件 2.3.1 DispatcherServlet:前端控制器 用户请求到达前端控制器,它就相当于mvc模式中的, dispatcherServlet是整个流程控制的中心,由 它调用其它组件处理用户的请求,dispatcherServlet的存在降低了组件之间的耦合性。 2.3.2 HandlerMapping:处理器映射器 HandlerMapping负责根据用户请求找到Handler即处理器,SpirngMVC 提供了不同的映射器实现不同的映射方式, 例如:配置文件方式,实现接口方式,注解方式等 2.3.3 Handler:处理器 它就是我们开发中要编写的具体业务控制器,由 DispatcherServlet把用户请求转发到Handler.由Handler对具体的用户请求进行处理 2.3.4 HandlAdapter:处理器适配器
2.3.5 View Resolver:视图解析器 View Resolver 负责将处理结果生成View视图,View Resolver首先根据逻辑图名解析成物理视图名,即具体的页面地质, 再生成View视图对象,最后对View进行渲染将处理结果通过页面展示给用户。 2.3.6 View:视图 SpringMVC框架提供了很多的View视图类型的支持,包括jstlView, freemarkerView, pdfView等。我们最常见的视图是JSP 一般情况下需要通过页面标签或页面模板技术将模型数据通过页面展示给用户,需要由程序员根据业务需求开发具体的页面。 2.3.7 <mvc:annotation-driven/> 在SpringMVC的各个组件中,处理器映射器,处理器适配器,视图解析器称为SpringMVC的三大组件。 室用<mvc:annotation-driven>自动加载RequestMappingHandlerMapping(处理映射器)和RequestMappingHandlerAdapter(处理适配器) ,可用在SpringMVC.xml配置文件中使用<mvc:annotation-driven>替代注解处理器和适配器的配置。 它就相当于在xml中配置了: <!-- 上面的标签相当于如下配置--> <!-- Begin --> <!-- HandlerMapping--> <bean 一堆bean />bean 注意:一般开发中,我们都需要写上此标签(随着课程的深入,该标签还有具体的使用场景) 明确:我们只需要编写处理具体业务的控制器以及视图 2.4 RequestMapping注解的作用 2.4.1使用说明 源码: @Target({ElementType.METHOD, ElementType.Type})---->1Method:作用在方法上, Type作用在类和接口上,放在类上分模块做开发 @Retention(RetentionPolicy.RUNTIME) @Documented @Mapping public @interface RequestMapping{ } 作用: 用于建立请求URL和处理请求方法之间的对应关系 你写一个方法,别人请求一个方法,访问URL就可以了。 例子代码 @Controller @RequestMapping(path="/user") <-----------------/user public class HelloController { //为什么一请求,就会执行,需要加入注解@RequestMapping 其中request表示请求,Mapping表示映射,里面需要提供路径path="hello" @RequestMapping(path="hello") public String sayHello(){ System.out.println("hello StringMVC"); return "success"; //一点击会跳转到一个页面,如何跳转呢? } @RequestMapping(path="/testRequestMapping") <---------------- public String testRequestMapping(){ System.out.println("测试RequestMapping注解..."); return "success"; } } <%@ page contentType="text/html;charset=UTF-8" language="java" %> <testRequestMapping <html> <head> <title>Title</title> </head> <body> <h3>入门程序</h3> <%-- <a href="hello">入门程序</a> --%> <a href ="user/testRequestMapping">RequestMapping注解</a> <------------- </body> </html> 前两个加拼接为最后一个xml文件引用 user+testRequestMapping=user/testRequestMapping 2.52.4 RequestMapping注解的属性 @RequestMapping(path="/testRequestMapping") public String testRequestMapping(){ 在RequestMapping源码中,起了个别名,value=path,都是指映射的路径 例如@RequestMapping(path="/testRequestMapping") 等同于@RequestMapping(value="/testRequestMapping") 如果【自由一个属性】, Value可以省略不写 即 (1) @RequestMapping(path="/testRequestMapping") 等同于 (2) @RequestMapping(value="/testRequestMapping")等同于 (3)@RequestMapping("/testRequestMapping") value:用于指定请求的URL。 它和path属性的作用是一样的 method:用于指定请求的方式。(指定方法,get, post,)可以传多个数组 params:用于指定限制请求参数的条件。 它支持简单的表达式。要求请求从参数的key和value必须和配置的一抹一样 例如: params = {"accountName"},表示请求参数必须有accountName params = {"moeny!100"},表示请求参数中money不能是100. headers:用于指定限制请求消息头的条件: 以上四个属性只要出现2个或以上时,他们的关系是与的关系。 1下面表示请求方式必须是POST方式才可以访问 @RequestMapping(value="/testRequestMapping", method={RequestMethod.POST}) 2下面表示请求方式必须是GET方式才可以访问 @RequestMapping(value="/testRequestMapping", method={RequestMethod.GET}) 3 数组类型,字符串。请求testRequestMapping,必须传给一个username的属性,必须传,不传,不会执行 @RequestMapping(value="/testRequestMapping", params={"username"}) 需要在index.jsp访问的时候传递一个数值,在index.jsp中 <a href ="user/testRequestMapping?username=hehe">RequestMapping注解</a> 访问方式 http://localhost:8080/springmvc_day01_01_start/user/testRequestMapping?username=hehe 3RequestMapping的属性 1path:指定请求路径的URL 2value:value属性和path属性是一样的 3method:指定该方法的请求方式 4params 指定限制请求参数的条件 5headers:发送的请求中必须包含的请求头 @RequestMapping(value="/testRequestMapping", params={"username=heihei"},headers = {"Accept"}) 以后发送请求,必须包含Accept,否则也不执行 <----------------------> 想看需要看头信息按F12,浏览器都有调试工具 <---------------------->