您的当前位置:首页正文

SpringMVC工作流程及常用注解

2024-11-24 来源:个人技术集锦

1.SpringMVC组件

|
  |-基本组件-|-HandlerMapping
  |          |-HandlerAdapter
  |          |-HandlerExceptionResolver
  |          |-ViewResolver
  |          |-LocaleResolver & LocaleContextResolver
  |          |-ThemeResolver
  |          |-MultipartResolver
  |          |-FlashMapManager
  |
  |-HandlerMapping-|-BeanNameUrlHandlerMapping
  |                |-SimpleUrlHandlerMapping
  |                |-RequestMappingHandlerMapping
  |
  |-(I)HandlerInterceptor-|-preHandle()
  |                       |-postHandle() 
  |                       |-afterCompletion() 
  |
  |-Handler-|-ResourceHttpRequestHandler
  |         |-HandlerMethod
  |
  |-HandlerAdaptor-|-RequestMappingHandlerAdapter
  |                |-HttpRequestHandlerAdapter	
  |                |-SimpleControllerHandlerAdapter

2.组件搭配模型图(大概流程)

     _____________________________________
	|					 |                |
	|					 |---Resquest     |
    |                    |                |  1.到HandlerMapping
    |             1.DispatcheServlet      |    进行查询
    |                    |                |
    |                   \|/               |  2.返回处理器执行链路   
    |    _________2.HandlerMapping___     |    HandlerExecutionChain
	|   |                |           |    |
    |  \|/              \|/         \|/   |  3.根据方法入参及注解         
    |Controller1     3.Controller2   ...  |    进行参数映射
	|   |                |           |    |    (HandlerAdapter)
    |  \|/              \|/         \|/   |   
    |   |___________4.ModelAndView___|    |
    |                    |                |
    |                   \|/               |
    |               5.ViewReslover        |
	|			         |                |
    |                   \|/               |
    |_________________ 6....______________|

3.常用注解

  |-@RequestMapping :用于请求url映射;有六大属性: -|-value   :绑定的url地址
  |											     |-method  :指定请求的method类型, GET、POST、PUT、DELETE等
  |											     |-consumes:指定处理请求的提交内容类型(Content-Type)
  |											     |-produces:指定返回的内容类型
  |											     |-params  :指定request中必须包含某些参数值
  |											     |-headers :指定request中必须包含某些指定的header值
  |-@RequestBody    :注解实现接收http请求的json数据,将json转换为java对象
  |-@ResponseBody   :注解实现将conreoller方法返回对象转化为json对象、xml等响应给客户
  |
  |-@Autowired : 默认按类型装配;所依赖的对象必须存在,如果要允许null值,可以设置它的required属性为false
  |-@Resource  : 默认按名称进行装配,通过name属性进行指定
  |
  |-@PathVariable :用于将请求URL中的模板变量映射到功能处理方法的参数上,即取出uri模板中的变量作为参数
  |-@requestParam :主要用于在SpringMVC后台控制层获取参数,类似一种是request.getParameter("name")
  |
  |-@Controller
  |-@Service
  |-@Repository
显示全文