spring mvc流程
springmvc框架模型:
1. HTTP请求
2. DispatcherServlet接收请求,根据请求信息按照某种机制找到请求对应的处理器handler
3. DispatcherServlet将请求的处理工作委托给具体的处理器(Handler)来处理请求,行相应的业务逻辑。
4.handler处理后返回一个ModelAndView给DispatcherServlet,ModelAndView包含了视图逻辑名和渲染视图时需要用到的模型数据对象
5. DispatcherServlet通过调用ViewResolver来完成视图解析的工作,得到视图
6.给视图中渲染model数据
7.返回响应
HelloWorld
1.在web.xml中配置DispatcherServlet
web.xml
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:applicationContext.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <servlet>
- <servlet-name>dispatcherServlet</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/dispatcherServlet-servlet.xml</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>dispatcherServlet</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
从上面可以看出 DispatcherServlet是默认的servlet,在容器启动时候就实例化了
DispatcherServlet对应的配置文件是/WEB-INF/dispatcherServlet-servlet.xml
2.写DispatcherServlet所使用的配置文件>/WEB-INF/dispatcherServlet-servlet.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-2.5.xsd">
- <context:component-scan base-package="com.auscend.controller" />
- <bean
- class="org.springframework.web.servlet.view.InternalResourceViewResolver"
- p:prefix="/"
- p:suffix=".jsp" />
- </beans>
前缀和后缀是用来加载视图的名字前后的
2.编写处理请求的控制器handler,控制器返回一个ModelAndView对象
- package com.auscend.controller;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.servlet.ModelAndView;
- import com.auscend.domain.User;
- @Controller
- @RequestMapping("/user")
- public class UserController {
- @RequestMapping(value = "/add")
- public ModelAndView add(User user) {
- user.setId(1L);
- ModelAndView mav = new ModelAndView();
- mav.setViewName("success");
- mav.addObject("user", user);
- return mav;
- }
- }
4.写返回给客户端的页面
/success.jsp
- <h3>add success!</h3>
- id:${user.id}<br/>
- username:${user.userName }<br/>
- password:${user.password }<br/>
5.写发出请求的页面
/input.jsp
- <form action="${pageContext.request.contextPath }/user/add" method="post">
- username:<input type="text" name="userName"/><br/>
- password:<input type="password" name="password"/><br/>
- <input type="submit" value="submit"/>
- </form>
DispatcherServlet使用的配置文件的位置以及内容
假如dispatcherServlet对应的servlet-name是hello
如果不配置这个文件的位置,也就是不配置上面的contextConfigLocation,默认会在WEB-INF下找hello-servlet.xml
如果在dispatcherServlet中配置了namespace属性(例如叫 namesp ),默认会在WEB-INF下找namesp.xml
如果配置了contextConfigLocation,就加载contextConfigLocation写的xml,contextConfigLocation的值可以是多个如:classpath:a.xml,classpath:b.xml
配置文件中写什么东西,看看DispatcherServlet源代码就知道了
- /**
- * Initialize the strategy objects that this servlet uses.
- * <p>May be overridden in subclasses in order to initialize further strategy objects.
- */
- protected void initStrategies(ApplicationContext context) {
- initMultipartResolver(context);
- initLocaleResolver(context);
- initThemeResolver(context);
- initHandlerMappings(context);
- initHandlerAdapters(context);
- initHandlerExceptionResolvers(context);
- initRequestToViewNameTranslator(context);
- initViewResolvers(context);
- initFlashMapManager(context);
- }
initMultipartResolver(context); init多部分请求解释器也就是上传文件的解析器
initLocaleResolver(context); 初始化本地化解析器
initThemeResolver(context)
;initHandlerMappings(context);
initHandlerAdapters(context);i
nitHandlerExceptionResolvers(context);
initRequestToViewNameTranslator(context);
initViewResolvers(context);
initFlashMapManager(context);
上面这些spring已经提供了默认值,在org.springframework.web.servlet类路径下有一个DispatcherServlet.properties文件,文件中定义了默认的组件,如果我们配置了自己选择的组件,就不使用这些组件了
initMultipartResolver会查找bean name为multipartResolver 类型为 MultipartResolver 的bean 作为上传文件的解析器,没有默认的配置,如果需要我们可以自己配一个,不能配置多个,如下:
- <!-- 文件上传 -->
- <bean id="multipartResolver"
- class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
- p:defaultEncoding="UTF-8"
- p:maxUploadSize="5000000"
- p:uploadTempDir="upload/temp"/>
单位是字节,在控制层中只需在处理请求的方法里的参数中注解即可 public String upload(@RequestParam("picture") MultipartFile picture) 那么在方法里处理这个picture就ok,其中picture对应<input type="file" name="picture"/>
initLocaleResolver 本地化解析器,查找名为localeResolver类型为LocaleResolver的bean,找不到就使用默认的实现类(AcceptHeaderLocaleResolver) ,只能配置一个,下面介绍:
1.AcceptHeaderLocaleResolver依据请求中的accept-language请求头信息进行解析处理,这个是默认处理器
2.CookieLocaleResolver语言信息保存在cookie中
<!-- 本地化解析 -->
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver"
p:cookieName="clientLanguage"
p:cookieMaxAge="100000"
p:cookiePath="/"
p:defaultLocale="zh_CN"/>
3.SessionLocaleResolver语言信息保存在session中
保存语言的属性名是SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME属性,值是Locale对象
4.LocaleChangeInterceptor通过url的参数指定语言
用户可以通过paramName指定一个参数,比如locale,那么访问时 http://xxx/xxx/xx?locale=en 就表示英文
initThemeResolver,查找名为themeResolver类型为LocaleResolver的Bean,找不到就使用默认的实现类(FixedThemeResolver),只能配置一个
参考:
http://viralpatel.net/blogs/spring-3-mvc-themes-tutorial-example/
http://starscream.iteye.com/blog/1075855
http://www.jpalace.org/docs/tutorials/spring/mvc_22.html
initHandlerMappings 处理器映射器 可以配置多个 bean 名字无需指定
1)如果detectAllHandlerMappings属性为true(默认为true),根据类型匹配(HandlerMapping)机制查找上下文及父Spring容器中所有匹配的Bean,将它们作为该类型组件;
2)如果detectAllHandlerMappings属性为false,查找名为handlerMapping类型为HandlerMapping的Bean作为该类型组件;
3)如果通过以上方式都找不到,使用BeanNameUrlHandlerMapping实现类创建创建该类型的组件。
补充:待续
initHandlerAdapters处理器适配器 可以配置多个 bean 名字无需指定
1)如果detectAllHandlerAdapters属性为true(默认为true),根据类型匹配(HandlerAdapter)机制查找上下文及父Spring容器中所有匹配的Bean,将它们作为该类型组件;
2)如果detectAllHandlerAdapters属性为false,查找名为handlerAdapter类型为HandlerAdapter的Bean作为该类型组件;
3)如果通过以上方式都找不到,使用DispatcherServlet.properties配置文件中指定的三个实现类分别创建一个适配器,添加到适配器列表中。
补充:待续
initHandlerExceptionResolvers处理器异常解析器 可以配置多个 bean 名字无需指定
1)如果detectAllHandlerExceptionResolvers属性为true(默认为true),根据类型匹配(HandlerExceptionResolver)机制查找上下文及父Spring容器中所有匹配的Bean作为该类型组件;
2)如果detectAllHandlerExceptionResolvers属性为false,查找名为handlerExceptionResolver类型为HandlerExceptionResolver的Bean作为该类型组件;
3)如果通过以上方式都找不到,查找DispatcherServlet.properties中定义的默认实现类,不过该文件中没有对应处理器异常解析器的默认实现类(你可以更改属性文件)。
补充:待续
initRequestToViewNameTranslator视图名翻译器 只能配置一个
1)查找名为viewNameTranslator类型为RequestToViewNameTranslator的Bean作为该类型组件;
2)如果1)找不到,使用默认的实现类(DefaultRequestToViewNameTranslator)创建该类型的组件。
补充:待续
initViewResolvers视图解析器 可以配置多个 bean 名字无需指定
1)如果detectAllViewResolvers属性为true(默认为true),根据类型匹配(ViewResolver)机制查找上下文及父Spring容器中所有匹配的Bean作为该类型组件;
2)如果detectAllViewResolvers属性为false,查找名为viewResolver类型为ViewResolver的Bean作为该类型组件;
3)如果通过以上方式都找不到,通过DispatcherServlet.properties中定义的默认实现类(InternalResourceViewResolver)创建该类型的组件。