SpringMvc

来源:互联网 发布:付费软件怎么收费 编辑:程序博客网 时间:2024/06/04 08:20

有些类已经过时  主要写下自己的理解防止忘记有地方可查

核心类   DispathcerServlet

处理请求路径映射  handlerMapping

处理请求适配器 hanlerAdapater

viewName解释器  DefaultRequestToViewNameTranslator   

view分析者  ViewResolver


SpringMvc 处理请求步骤   

===〉DispathcerServlet 找到处理请求的handlerMapping 

===〉MultiActionController 支持不同方法处理不同业务

===〉处理请求路径映射 handlerMapping  找到处理请求的Controller

>>>>> BeanNameUrlHandlerMapping  根据配置的 Controller <bean name=‘/test*

>> /test* 可以正则匹配路径 入 http://...../test1,http://...../test2....

>> 使用beanNameUrlHanlderMapping 每个请求都要配置bean 所以这种方式不建议使用.

>>>>> SimpleUrlHandlerMapping 可以使用 urlMap 属性配置请求路径 支持正则匹配

>>>>> DefaultAnnotationHandlerMapping 使用注解配置

===〉处理请求适配器 handlerAdapter找到处理请求的controller

>>>>>handlerAdapter通过support方法注入参数找到注册的bean 最终调用实际的handle method。

===〉请求方法MethodNameResolver  方法分析器  找到处理请求的方法 返回处理结果

>>>>> InternalPathMethodNameResolver 提供去掉 前缀 和 后缀 匹配 方法名称

>>>>>PropertiesMethodNameResolver 必须设置mappings 属性 key为请求路径e.g <prop key="/requestPathFollowingTheProjectName*">methodName</prop>

>>>>>ParameterMethodNameResolver 属性 defaultMethodName 默认请求方法名称 action=? ?代表要请求的方法

===〉view分析者  ViewResolver 根据返回值 解析成需要显示的 VIEW

>>>>>ResourceBundleViewResolver 属性 basenames 值代表加载那个属性文件

                >>>> e.g  jstl.(class) = org.springframework.web.servlet.view.JstlView 

                                jstl.url = /WEB-INF/jsp/test.jsp

>>>>>BeanNameViewResolver根据配置的bean名称找到对应的view

>>>>>InternalResourceViewResolver根据前缀和后缀找到指定的jsp

===> view 视图 有很多种 一般使用JstlView 



〈〈〈〈〈〈======= 下面主要讲解下注解的配置 =======〉〉〉〉〉〉

web.xml 配置 

<!-- Spring ApplicationContext配置文件的路径,可使用通配符,用于后面的Spring Context Loader -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/applicationContext.xml
</param-value>
</context-param>
<!--Spring ApplicationContext 载入 -->
<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring MVC Servlet -->
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>



第一个配置  不使用默认过滤器  只加载 注解Controller bean  一般配置在 Dispatcher加载的配置文件  spring-servlet.xml中 代表只加载Contrller bean

<context:component-scanbase-package="com.test"use-default-filters="false">  

<context:include-filtertype="annotation"expression="org.springframework.stereotype.Controller"/>  

<context:include-filtertype="annotation"expression="org.springframework.web.bind.annotation.ControllerAdvice" />  

</context:component-scan>


第二个配置 只过滤 注解Controller bean 对其不加载 一般配置在applicationContext.xml 中 代表只不加载Controller bean 和上面配置一起使用防止重复加载

<context:component-scanbase-package="com.test">  

<context:exclude-filtertype="annotation"expression="org.springframework.stereotype.Controller"/>  

<context:exclude-filtertype="annotation"expression="org.springframework.web.bind.annotation.ControllerAdvice" 

</context:component-scan>


注解的使用 

e.g   项目路径为  http://localhost:8080/springMvc/test 请求方法post

@RequestMapping(value = "/test", method = RequestMethod.POST)


@ResponseBody   返回文本


/**

 请求路径 http://localhost:8080/springMvc/test?age=1    这里使用require = false 必须要使用包装类型来接收参数

 防止报null指针因为没有会默认传值为null

**/

@RequestMapping(value = "/test")  
    public String test(@RequestParam(value = "age", required = false)  
    Integer age)
....
}  


//请求url http://localhost:8080/springMvc/test/id/name    这种方法可以过滤掉参数攻击因为直接使用路径表达参数
@RequestMapping("/test/{id}/{name}")

test(@PathVariable String id,@PathVariable String name)


//请求url http://localhost:8080/springMvc/test?id=1&name=ni&age=10  请求路径中必须有 id name age
@RequestMapping(value="/test",params={"id","name","age"})


//请求url http://localhost:8080/springMvc/test?id=1&name=ni     请求路径中不能有age 和 sex
@RequestMapping(value="/t1",params={"id","name","!age","!sex"})








第一个配置  不使用默认过滤器  只加载 注解Controller bean
第一个配置  不使用默认过滤器  只加载 注解Controller bean
0 0