码迷,mamicode.com
首页 > 编程语言 > 详细

spring-MVC [spring in action 3.7整理]

时间:2015-04-10 17:28:59      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:

搭建springMVC

Spring MVC的核心是DispatcherServlet,这个Servlet充当前端控制器。与其他Servlet一样,必须在web应用程序的文件中进行配置。

1.web.xml:

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" 

xmlns="http://java.sun.com/xml/ns/javaee" 

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  <display-name></display-name>

  <!-- 进入页面 -->

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

  <!-- 告诉Log4jConfigListener日志配置的位置 -->

  <context-param>

    <param-name>log4jConfigLocation</param-name>

    <param-value>classpath:log4j.properties</param-value>

  </context-param>

  <!-- 表示开一条watchdog线程每6秒扫描一下配置文件的变化 -->

  <context-param>

    <param-name>log4jRefreshInterval</param-name>

    <param-value>60000</param-value>

  </context-param>

  <!-- 为了给ContextLoaderListener指定一个或多个spring配置文件。需要在servlet上下文中配置contextConfigLocation参数 -->

  <!-- contextConfigLocation参数指定一个路径列表,除非特别声明,路径是相对于应用程序根目录的。 -->

  <context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>

     classpath:xml/springContext.xml

     <!-- /WEB-INF/spitter-security.xml

     classpath:service-context.xml

     classpath:persistence-contex.xml

     classpath:dataSource-context.xml -->

    </param-value>

  </context-param>

  <!-- 日志监听器 -->

  <listener>

    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>

  </listener>

  <!-- ContextLoaderListener是一个Servlet监听器,除了DispatcherServlet创建的应用上下文以外,

  它能够加载其他的配置文件到spring应用上下文中,这里在web.xml中进行声明。  -->

  <!-- 我们必须告诉ContextLoaderListener需要加载哪些配置文件。

  如果没有指定,上下文加载器会查找/WEB-INF/applicationContext.xml这个Spring配置文件。

  但是这个文件本身并没有做到将应用上下文拆分文多个片段,因此需要重写默认实现。-->

  <listener>

    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  </listener>

  <!-- 定义request和response的编码 -->

  <filter>

    <filter-name>encodingFilter</filter-name>

    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

    <init-param>

      <param-name>encoding</param-name>

      <param-value>UTF-8</param-value>

    </init-param>

    <init-param>

      <param-name>forceEncoding</param-name>

      <param-value>true</param-value>

    </init-param>

  </filter>

  <filter-mapping>

    <filter-name>encodingFilter</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>

  <!-- Spring MVC的核心是DispatcherServlet,这个Servlet充当前端控制器。 -->

  <!-- DispatcherServlet会根据一个XML文件来加载其spring应用上下文,而这个文件的名字基于它的<servlet-name>属性来确定,这里为springmvc-servlet -->

  <!-- 关于<load-on-startup>解释:

   转自xzc.Log,原文链接http://www.blogjava.net/xzclog/archive/2011/09/29/359789.html

   贴一段英文原汁原味的解释如下:

   Servlet specification:

   The load-on-startup element indicates that this servlet should be loaded 

   (instantiated and have its init() called) on the startup of the web application. 

   The optional contents of these element must be an integer indicating the order in which 

   the servlet should be loaded. If the value is a negative integer, or the element is not present,

   the container is free to load the servlet whenever it chooses.   If the value is a positive integer 

   or 0, the container must load and initialize the servlet as the application is deployed. 

   The container must guarantee that servlets marked with lower integers are loaded before servlets 

   marked with higher integers. The container may choose the order of loading of servlets 

   with the same load-on-start-up value.

   翻译过来的意思大致如下:

   1)load-on-startup元素标记容器是否在启动的时候就加载这个servlet(实例化并调用其init()方法)。

   2)它的值必须是一个整数,表示servlet应该被载入的顺序

   2)当值为0或者大于0时,表示容器在应用启动时就加载并初始化这个servlet;

   3)当值小于0或者没有指定时,则表示容器在该servlet被选择时才会去加载。

   4)正数的值越小,该servlet的优先级越高,应用启动时就越先加载。

   5)当值相同时,容器就会自己选择顺序来加载。

   所以,<load-on-startup>x</load-on-startup>,中x的取值1,2,3,4,5代表的是优先级,而非启动延迟时间。 -->

  <servlet>

    <servlet-name>springmvc</servlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <load-on-startup>1</load-on-startup>

  </servlet>

  <servlet-mapping>

    <servlet-name>springmvc</servlet-name>

    <url-pattern>/</url-pattern>

  </servlet-mapping>

  <!-- 错误页面 -->

  <error-page>

    <!-- java中,错误类的基类是java.lang.Error,异常类的基类是java.lang.Exception,都是java.lang.Throwable的子类 -->

    <exception-type>java.lang.Throwable</exception-type>

    <location>/WEB-INF/views/commons/500.jsp</location>

  </error-page>

  <error-page>

    <error-code>500</error-code>

    <location>/WEB-INF/views/commons/500.jsp</location>

  </error-page>

  <error-page>

    <error-code>404</error-code>

    <location>/WEB-INF/views/commons/404.jsp</location>

  </error-page>

  <!-- session有效期,30分钟 -->

  <session-config>

    <session-timeout>30</session-timeout>

  </session-config>

</web-app>

 

2.springmvc-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:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

 

<!-- spring自动检测组件,即不在xml文件中配置bean,而给我们的类加上spring组件注解。但是注解注入会失去一定的灵活性 -->

<!-- 定义扫描路径,不使用默认的扫描方式 -->

<!-- @Component是所有受Spring管理组件的通用形式;而@Repository、@Service和 @Controller则是@Component的细化,

用来表示更具体的用例(例如,分别对应了持久化层、服务层和表现层)。 -->

<!-- 子标签进行过滤,use-dafault-filters=”false”的情况下:<context:exclude-filter>指定的不扫描,<context:include-filter>指定的扫描  -->

<!-- type:类别说明

 Type Examples Expression Description

    annotation org.example.SomeAnnotation 符合SomeAnnoation的target class

    assignable org.example.SomeClass 指定class或interface的全名

    aspectj org.example..*Service+ AspectJ語法

    regex org\.example\.Default.* Regelar Expression

    custom org.example.MyTypeFilter Spring3新增Type, -->

<context:component-scan base-package="cmcc.framework,com.mocha,cmcc.sample" use-default-filters="false">

<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /><!-- 只扫面@Controller注解的类 -->

<context:exclude-filter type="regex" expression="cmcc.sample.quartz..*"/>

</context:component-scan>

<!-- 默认的注解映射的支持,告知Spring,我们启用注解驱动。 -->

<!-- 这个标签注册了Spring MVC分发请求到控制器所必须的DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter实例 -->

<!-- 第一个是HandlerMapping的实现类,它会处理@RequestMapping 注解,并将其注册到请求映射表中。 -->

<!-- 第二个是HandlerAdapter的实现类,它是处理请求的适配器,说白了,就是确定调用哪个类的哪个方法,并且构造方法参数,返回值。 -->

   <mvc:annotation-driven conversion-service="conversionService"/> 

<!-- 自定义类型转换 -->

<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">    

    <property name="converters">

       <list>

           <bean class="cmcc.framework.common.date.DateConverter" />

       </list>

    </property>

</bean>

<!-- <mvc:resources>建立一个服务于静态资源的处理器。

属性mapping设置为/resource/**,表示路径必须以/resource开始,而且包括它的任意子路径。

属性location表示要提供服务的文件位置。配置表示,

所有以/resource开头的请求都会自动由应用程序根目录下的/resource目录提供服务。

因此,我们所有的图片、样式表、javascript以及其他静态资源都必须房子应用的/resource目录下 -->

<mvc:resources mapping="/resource/**" location="/resource/" />

 

 

<!-- 视图解析器 -->

<!-- DispatcherServlet会查找一个视图解析器来将控制器返回的逻辑视图名称转换成渲染结果的实际视图,

视图解析器的工作是将逻辑视图的名字与org.springframework.web.servlet.view的实现相匹配,可以认为,

视图解析器所做的就是将视图名称与JSP进行匹配。spring提供多种视图解析器选择,这里选择InternalResourceViewResolver -->

<!-- 依照约定优于配置,InternalResourceViewResolver将逻辑视图解析为View对象,而该对象将渲染的任务委托给Web应用程序

上下文中的一个模板(通常JSP),它通过为逻辑视图名称添加前缀和后缀来确定Web应用程序中模板的路径。 -->

<!-- 假设应用的所有JSP放在"/WEB-INF/views/"目录下,即有如下的配置 -->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<!-- 默认值,可不配置 <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> -->

<property name="prefix" value="/WEB-INF/views/"></property>

<property name="suffix" value=".jsp"></property>

</bean>

<!-- 文件上传 -->

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

<!-- 文件上传大小限制,单位:字节 -->

<!-- <property name="maxUploadSize" value="" /> -->

<property name="defaultEncoding" value="UTF-8" />

</bean>

<!-- 定义国际化消息 -->

<bean id="messageSource"

class="org.springframework.context.support.ResourceBundleMessageSource">

<property name="basename" value="message/mess" />

</bean>

 

<!-- Mapping exception to the handler view <bean id="exceptionResolver" 

class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> 

<property name="defaultErrorView" value="/commons/error"/> <property name="exceptionMappings"> 

<props> </props> </property> </bean> -->

 

<!-- 拦截器配置 <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/admin/**"/> 

<bean class="cmcc.example.inteceptor.MyInteceptor"></bean> </mvc:interceptor> 

</mvc:interceptors> -->

<!-- path什么都不写为拦截所有URL <mvc:mapping path=""/> -->

<!-- /admin/**为拦截admin及以admin开头的所有URL,如 /admin/user/add/test -->

<!-- /admin/*为拦截admin开头下一级URL,如 /admin/user -->

<!-- 注意:以上2种配置都不能拦截/test/admin/user这样的请求 -->

 

</beans>

 

spring-MVC [spring in action 3.7整理]

标签:

原文地址:http://www.cnblogs.com/aries1991/p/4413948.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!