Spring MVC框架的环境搭建与配置

来源:互联网 发布:淘宝主图psd模版 编辑:程序博客网 时间:2024/06/16 12:49

本文是Spring MVC框架的环境搭建与配置笔记,目前包括部署描述符文件模版与Spring MVC配置文件模版,Async、Security、Decorator、Interceptor、Repository等配置待更新。

部署描述符文件模版

//web.xml<?xml version="1.0" encoding="UTF-8"?><web-app 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_3_0.xsd"    version="3.1">    <!--定义应用名称-->    <display-name>myapp</display-name>    <!-- web.xml的加载过程是context-param >> listener  >> fileter  >> servlet -->    <!-- 配置Spring MVC加载的应用上下文配置文件,设定web应用的context环境参数,WEB项目所有部分都将共享这个上下文 -->    <!-- param-name在JSP网页中使用${initParam.param_name}获得,在Servlet则使用String param_name=getServletContext().getInitParamter("param_name"); -->    <context-param>        <param-name>contextConfigLocation</param-name>        <!-- 该xml文件定义了根应用上下文,会被ContextLoaderListener加载 -->        <!-- <param-value>/WEB-INF/spring/root-context.xml</param-value> -->        <param-value>classpath:/spring/*.xml</param-value>    </context-param>    <context-param>        <param-name>log4jConfigLocation</param-name>        <param-value>/WEB-INF/log4j.xml</param-value>    </context-param>    <!-- 注册ContextLoaderListener -->    <listener>        <listener-class>com.zero.myapp.web.context.ContextLoaderListener</listener-class>    </listener>    <!-- 注册Log4jConfigListener -->    <listener>        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>    </listener>    <!--Encoding filter -->    <filter>        <!--当JSP页面和Java代码中使用了不同的字符集进行编码时会出现表单提交的数据或者上传/下载中文名称文件出现乱码的问题,该类可出场 -->        <filter-name>encodingFilter</filter-name>        <filter-class>com.zero.commons.web.filter.CharacterEncodingIPFilter</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-mapping 元素的两个主要子元素filter-name和url-pattern.用来定义Filter所对应的URL -->        <filter-name>encodingFilter</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>    <!--GzipFilter -->    <filter>        <!--GZIPFilter可以用gzip的方式压缩HTTP Response的内容,从而在Server端加快了响应回复的速度,在Client端缩短了页面刷新时间,在网络内减少了数据流量-->        <filter-name>gzipFilter</filter-name>        <filter-class>            net.sf.ehcache.constructs.web.filter.GzipFilter        </filter-class>    </filter>    <filter-mapping>        <filter-name>gzipFilter</filter-name>        <url-pattern>*.css</url-pattern>    </filter-mapping>    <filter-mapping>        <filter-name>gzipFilter</filter-name>        <url-pattern>*.png</url-pattern>    </filter-mapping>    <filter-mapping>        <filter-name>gzipFilter</filter-name>        <url-pattern>*.gif</url-pattern>    </filter-mapping>    <filter-mapping>        <filter-name>gzipFilter</filter-name>        <url-pattern>*.jpeg</url-pattern>    </filter-mapping>    <filter-mapping>        <filter-name>gzipFilter</filter-name>        <url-pattern>*.html</url-pattern>    </filter-mapping>    <filter-mapping>        <filter-name>gzipFilter</filter-name>        <url-pattern>*.jpg</url-pattern>    </filter-mapping>    <filter-mapping>        <filter-name>gzipFilter</filter-name>        <url-pattern>*.js</url-pattern>    </filter-mapping>    <!-- springSecurityFilterChain filter-->    <filter>        <!-- 在Spring Security配置在web安全性中时,DelegatingFilterProxy会将过滤处理逻辑委托给这个filter  -->        <filter-name>springSecurityFilterChain</filter-name>        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>    </filter>    <filter-mapping>        <filter-name>springSecurityFilterChain</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>    <!-- sitemesh filter -->    <filter>        <!-- Sitemesh是一个网页布局和修饰的框架,基于Servlet中的Filter -->        <filter-name>sitemesh</filter-name>        <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>    </filter>    <filter-mapping>        <filter-name>sitemesh</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>    <!-- 注册DispatcherServlet -->    <servlet>        <!-- 配置DispatcherServlet类型的servlet,自动启动并mapping到所有请求,Spring MVC的入口 -->        <servlet-name>appServlet</servlet-name>        <!-- DispatcherServlet即前端控制器,会根据servlet名字找到/WEB-INF/appServlet-context.xml文件并加载其应用上下文,接受request进行response -->        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <!-- <load-on-startup>标记容器在启动时就装载这个servlet并调用其init方法,若不存在则在该servlet第一个请求时加载 -->        <load-on-startup>1</load-on-startup>        <!-- 给servlet声明赋值,使其变成multipartConfig servlet,文件上传配置之一 -->        <multipart-config>                     <!-- 设置上传文件容量最大字节,这里限制为50MB -->            <max-file-size>52428800</max-file-size>        </multipart-config>    </servlet>    <servlet-mapping>        <servlet-name>appServlet</servlet-name>        <!-- url模式设置为“/”表明包括静态资源的所有请求都被映射到dispatcher servlet -->        <url-pattern>/</url-pattern>    </servlet-mapping>      <session-config>        <!-- 会话超时配置,此处120分钟后session.getAttribute()获取的值为空 -->        <session-timeout>120</session-timeout>    </session-config>    <error-page>        <error-code>404</error-code>        <location>/static/html/404.html</location>    </error-page>    <error-page>        <error-code>500</error-code>        <location>/static/html/500.html</location>    </error-page>    <welcome-file-list>        <welcome-file>/index</welcome-file>    </welcome-file-list>    </web-app>

Spring MVC配置文件模版

//appServlet-context.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/beans        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd       http://www.springframework.org/schema/context       http://www.springframework.org/schema/context/spring-context.xsd        http://www.springframework.org/schema/mvc        http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">    <!--<import resource="security.xml"/>-->    <!--annotation configuration,隐式的向容器注册bean,以识别注解 -->    <context:annotation-config/>    <!-- 组建扫描,扫描依赖基本包,保证Spring MVC在声明的base-package包中扫描及注册用于支持基于注解的控制器的请求处理方法的bean对象 -->    <context:component-scan base-package="com.zero.myapp.web"/>    <context:component-scan base-package="com.zero.commons.web.validation"/>    <!--static resource,指定Spring MVC那些静态资源需单独处理,不通过dispatcher servlet -->    <mvc:resources mapping="/static/**" location="/static/"/>    <!-- viewResolver, 视图解析器,解析jsp默认使用jstl标签,classpath得有jstl的包-->    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>        <property name="prefix" value="/WEB-INF/view/"/>        <property name="suffix" value=".jsp"/>    </bean>    <!-- localeResolver,区域解析器,用于支持国际化 -->    <bean id="localeResolver" class="corg.springframewrok.web.servlet.i18n.SessionLocaleResolver"/>    <!-- multipartResolver,文件上传需要的多部分解析器 -->    <bean id="multipartResolver"        class="org.springframework.web.multipart.support.StandardServletMultipartResolver">    </bean></beans>
原创粉丝点击