Spring MVC入门教程(一)
来源:互联网 发布:算法基础怎么样 编辑:程序博客网 时间:2024/04/29 04:07
我们先来看下Spring Web MVC的DispatcherServlet处理请求的工作流,有个整体的印象。
DispatcherServlet继承自HttpServlet,就像它的名字一样,是一个“调度员”。一个HTTP请求到来时,DispatcherServlet根据URL分配到注册的handlers处理。
配置DispatcherServlet
web.xml
<web-app> <servlet> <servlet-name>golfing</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>golfing</servlet-name> <url-pattern>/golfing/*</url-pattern> </servlet-mapping></web-app>
所有URL以/golfing开头的请求都会被名字为golfing的DispatcherServlet处理。
配置其他Bean
有两种方式,继承context方式和根context方法。
继承context方法
每个DispatcherServlet有自己的上下文对象WebApplicationContext(继承了根WebApplicationContext对象中定义的所有bean)。继承的bean可以在具体的Servlet实例中被重载。
Spring MVC会查找WEB-INF目录下查找一个名为[servlet-name]-servlet.xml的配置文件。
看一个例子。
<web-app> <servlet> <servlet-name>golfing</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>golfing</servlet-name> <url-pattern>/golfing/*</url-pattern> </servlet-mapping></web-app>
需要在/WEB-INF/路径下创建一个golfing-servlet.xml文件,如果需要多个Servlet,可以配置多个。
根context方法
只有一个WebApplicationContext对象,包含了所以的组件(components)。
通过在servlet初始化参数中配置一个空的contextConfigLocation来实现,看一个例子。
<web-app> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/root-context.xml</param-value> </context-param> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value></param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener></web-app>
其中/WEB-INF/root-context.xml可以自定义名字和路径。
WebApplicationContext中特殊类型bean
DispatcherServlet配置
DispatcherServlet包含有默认配,默认配置在org.springframework.web.servlet下的DispatcherServlet.properties文件中。
# Default implementation classes for DispatcherServlet's strategy interfaces.# Used as fallback when no matching beans are found in the DispatcherServlet context.# Not meant to be customized by application developers.org.springframework.web.servlet.LocaleResolver=org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolverorg.springframework.web.servlet.ThemeResolver=org.springframework.web.servlet.theme.FixedThemeResolverorg.springframework.web.servlet.HandlerMapping=org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,\ org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMappingorg.springframework.web.servlet.HandlerAdapter=org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,\ org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,\ org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapterorg.springframework.web.servlet.HandlerExceptionResolver=org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver,\ org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver,\ org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolverorg.springframework.web.servlet.RequestToViewNameTranslator=org.springframework.web.servlet.view.DefaultRequestToViewNameTranslatororg.springframework.web.servlet.ViewResolver=org.springframework.web.servlet.view.InternalResourceViewResolverorg.springframework.web.servlet.FlashMapManager=org.springframework.web.servlet.support.SessionFlashMapManager
用户也可以自己配置,覆盖默认配置。
看一个例子。
<!-- 定义跳转的文件的前后缀 ,视图模式配置--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 --> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean>
- Spring MVC入门教程(一)
- Spring MVC入门教程(一)
- Spring MVC 入门教程一
- spring mvc入门教程(一)概念介绍
- Spring MVC入门教程(二)
- Spring MVC入门教程(二)
- Spring MVC 入门教程
- spring MVC入门教程
- Spring MVC快速入门教程
- Spring MVC快速入门教程
- Spring mvc(一)
- Spring MVC(一)
- Spring MVC(一)
- Spring MVC(一)
- Spring入门教程(一)IOC 基本概念
- spring cloud快速入门教程(一)总章
- Spring MVC学习(一)
- Spring MVC学习(一)
- (五)使用DOM解析XML文档
- Centreon+Nagios实战第七篇——安装NRPE
- finally遇到的坑
- 打造国内一流全球知名的装备制造业龙头企业
- Hbase数据迁移
- Spring MVC入门教程(一)
- C运算符
- (六)使用SAX解析xml文档
- MAC下安装与配置MySQL
- eclipse修改编码类型的缺省值
- phpstorm10安装Plugin
- 常用的正则表达式规则整理
- LA3942Remember the Word[Trie树]
- (七)XML Schema总结