第一 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_2_5.xsd"
version="2.5">
<!-- 定义Spring配置文件位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<!-- 编码处理过滤器 -->
<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>
<filter-name>jCaptchaFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<!-- 页面缓存过滤器 -->
<filter>
<filter-name>cacheFilter</filter-name>
<filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
<init-param>
<param-name>time</param-name>
<param-value>3600</param-value>
</init-param>
<init-param>
<param-name>scope</param-name>
<param-value>application</param-value>
</init-param>
</filter>
<!-- 解决Hibernate延迟加载问题过滤器,需放在struts2过滤器之前 -->
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>excludeSuffixs</param-name>
<param-value>js,css,jpg,gif,png,bmp,jpeg</param-value>
</init-param>
</filter>
<!-- Struts2过滤器 -->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 验证码图片 -->
<filter-mapping>
<filter-name>jCaptchaFilter</filter-name>
<url-pattern>/captcha.jpg</url-pattern>
</filter-mapping><!-- 注意:需放在struts2过滤器filter-mapping之前 -->
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- JSPSupportServlet配置,以便Freemarker中使用Struts2标签 -->
<servlet>
<servlet-name>JSPSupportServlet</servlet-name>
<servlet-class>org.apache.struts2.views.JspSupportServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 对Spring容器进行实例化 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring刷新Introspector防止内存泄露 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- Session超时定义,单位为分钟 -->
<session-config>
<session-timeout>30</session-timeout>
</session-config><!-- 定义默认访问页 -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!-- 出错页面定义 -->
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/html/404.html</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/html/404.html</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/html/404.html</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/html/404.html</location>
</error-page>
</web-app>
第二 struts2.xml 文件配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts><!-- 是否显示详细错误信息 -->
<constant name="struts.devMode" value="false" />
<!-- 国际化资源文件名称 -->
<constant name="struts.custom.i18n.resources" value="i18n" />
<!-- 是否自动加载国际化资源文件 -->
<constant name="struts.i18n.reload" value="false" />
<!-- convention类重新加载 -->
<constant name="struts.convention.classes.reload" value="false" />
<!-- 浏览器是否缓存静态内容 -->
<constant name="struts.serve.static.browserCache" value="true" />
<!-- 配置使用Spring管理Action -->
<constant name="struts.objectFactory" value="spring" />
<!-- 上传文件大小限制设置 -->
<constant name="struts.multipart.maxSize" value="-1" />
<!-- 主题 -->
<constant name="struts.ui.theme" value="simple" />
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<!-- 编码 -->
<constant name="struts.i18n.encoding" value="UTF-8" />
<!-- 后缀 -->
<constant name="struts.action.extension" value="action,,xml,json" />
<!-- 结果资源的路径 -->
<constant name="struts.convention.result.path" value="/WEB-INF/template/" />
<!-- URL资源分隔符 -->
<constant name="struts.convention.action.name.separator" value="_" />
<package name="basePackage" extends="struts-default">
<interceptors>
<interceptor name="trimInterceptor" class="com.common.TrimInterceptor" /><!-- trim拦截器 -->
<interceptor name="loginInterceptor" class="com.common.LoginInterceptor" /><!-- 登录验证过滤器 -->
<interceptor name="resourceInterceptor" class="com.common.ResourceInterceptor" /><!-- 配置资源权限过滤滤器 -->
<!-- 后台管理拦截器栈 -->
<interceptor-stack name="adminStack">
<interceptor-ref name="loginInterceptor" />
<interceptor-ref name="resourceInterceptor" />
<interceptor-ref name="trimInterceptor" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
<!-- 系统登录拦截器栈 -->
<interceptor-stack name="loginStack">
<!-- trim拦截器 -->
<interceptor-ref name="trimInterceptor" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors><!-- 配置默认拦截器栈 -->
<default-interceptor-ref name="defaultStack" />
<!-- 未到找Action指向页面 -->
<default-action-ref name="errorPage" />
<global-results>
<result name="error" type="freemarker">/WEB-INF/template/common/error.ftl</result>
<result name="success" type="freemarker">/WEB-INF/template/common/success.ftl</result>
<result name="noresource" type="freemarker">/WEB-INF/template/common/noresource.ftl</result>
<result name="nologin" type="redirect">/index.html</result>
<result name="excel" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="contentDisposition">attachment;filename="${excelFileName}"</param>
<param name="bufferSize">1024</param>
<param name="inputName">excelFile</param>
</result>
</global-results><action name="errorPage">
<result type="redirect">/html/404.html</result>
</action>
<!-- 文件下载时候判断文件是否存在,并跳转到download -->
<action name="downloadFile" class="com.action.file.DownloadAction">
<result type="chain">download</result>
</action>
<action name="download" class="com.action.file.UpDownloadAction">
<result type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">downloadFile</param>
<param name="contentDisposition">attachment;fileName="${fileName}"</param>
<param name="bufferSize">1024</param>
</result>
</action>
</package><package name="admin" extends="basePackage" namespace="/">
<!-- 配置默认拦截器栈 -->
<default-interceptor-ref name="adminStack" />
</package>
<package name="login" extends="basePackage" >
<!-- 配置默认拦截器栈 -->
<default-interceptor-ref name="loginStack" />
</package>
</struts>
第三步 application.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">
<!-- 设置需要进行Spring注解扫描的类包 -->
<context:component-scan base-package="com.eshop" />
<!-- JDBC参数配置 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" lazy-init="true">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath:/jdbc.properties</value>
</list>
</property>
</bean><!-- 数据源配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- 设置JDBC驱动名称 -->
<property name="driverClassName" value="${jdbc.driver}" />
<!-- 设置JDBC连接URL -->
<property name="url" value="${jdbc.url}" />
<!-- 设置数据库用户名 -->
<property name="username" value="${jdbc.username}" />
<!-- 设置数据库密码 -->
<property name="password" value="${jdbc.password}" /><!-- 设置连接池初始值 -->
<property name="initialSize" value="3" />
<!-- 设置连接池最大值 -->
<property name="maxActive" value="60" />
<!-- 设置连接池最小空闲值 -->
<property name="minIdle" value="2" />
<!-- 设置连接池最大空闲值 -->
<property name="maxIdle" value="5" />
<!-- 设置验证SQL -->
<property name="validationQuery" value="${jdbc.validationQuery}" />
<!-- 设置取出连接时验证其是否有效 -->
<property name="testOnBorrow" value="${jdbc.testOnBorrow}" />
</bean><bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<!-- 配置Hibernate拦截器,自动填充数据的插入、更新时间 -->
<property name="entityInterceptor" ref="entityInterceptor" />
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<!-- 设置数据库方言 -->
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<!-- 输出SQL语句到控制台 -->
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<!-- 格式化输出到控制台的SQL语句 -->
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<!-- 是否开启二级缓存 -->
<prop key="hibernate.cache.use_second_level_cache">false</prop>
<!-- 配置二级缓存产品 -->
<prop key="hibernate.cache.provider_class">org.hibernate.cache.OSCacheProvider</prop>
<!-- 是否开启查询缓存 -->
<prop key="hibernate.cache.use_query_cache">false</prop>
<!-- 数据库批量查询数 -->
<!-- <prop key="hibernate.jdbc.fetch_size">50</prop> -->
<!-- 数据库批量更新数 -->
<prop key="hibernate.jdbc.batch_size">30</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com/eshop/entity</value>
</list>
</property>
</bean>
<!-- 缓存配置 -->
<bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation">
<value>classpath:/ehcache.xml</value>
</property>
</bean>
<!-- 资源验证 -->
<bean id="resourceCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager" ref="cacheManagerFactory" />
<property name="cacheName" value="RESOURCE_CACHE" />
</bean>
<!-- 使用AspectJ方式配置AOP -->
<aop:aspectj-autoproxy proxy-target-class="true" />
<aop:config proxy-target-class="true" /><!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置事务传播特性 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="get*" read-only="true"/>
<tx:method name="load*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice><!-- 配置哪些类的哪些方法参与事务 -->
<aop:config>
<aop:advisor pointcut="execution(* com.eshop.service.*..*.*(..))" advice-ref="transactionAdvice" />
</aop:config>
<!-- 配置JCaptcha验证码功能 -->
<bean id="captchaService" class="com.octo.captcha.service.image.DefaultManageableImageCaptchaService">
<property name="captchaEngine">
<bean class="com.eshop.common.JCaptchaEngine" />
</property>
<!-- 验证码过期时间 -->
<property name="minGuarantedStorageDelayInSeconds" value="600" />
</bean><!-- 配置freemarkerManager -->
<bean id="freemarkerManager" class="org.apache.struts2.views.freemarker.FreemarkerManager" /><!-- 配置freeMarkerConfigurer -->
<bean id="freeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPaths" value="/WEB-INF/template/admin/" />
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">1800</prop><!-- 模板更新延时 -->
<prop key="default_encoding">UTF-8</prop>
<prop key="locale">zh_CN</prop>
</props>
</property>
</bean>
</beans>