备忘,免得以后项目没了,什么都得从头来。项目都是struts2+spring+jdbctemplate,并且涉及到aop,freemarker,json,quartz,log4j,urlrewrite等方面。从头来配置,光配置文件就够烦人的了。
第一个:web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>UrlRewrite</display-name> <!-- log4j --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>log4j.properties</param-value> </context-param> <listener> <listener-class> org.springframework.web.util.Log4jConfigListener </listener-class> </listener> <!-- 开启URLREWRITE监听 --> <filter> <filter-name>UrlRewriteFilter</filter-name> <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> <init-param> <param-name>logLevel</param-name> <param-value>WARN</param-value> </init-param> </filter> <filter-mapping> <filter-name>UrlRewriteFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping> <!-- 配置struts2 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping> <!-- 配置freemarker --> <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> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value> </context-param> <!-- 404错误 --> <error-page> <error-code>404</error-code> <location>/404.jsp</location> </error-page> <!-- 500错误 --> <error-page> <error-code>500</error-code> <location>/500.jsp</location> </error-page> <welcome-file-list> <welcome-file>index.action</welcome-file> </welcome-file-list> </web-app>
然后是struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.mapper.alwaysSelectFullNamespace" value="true" />
<!--spring 容器 --> <constant name="struts.objectFactory" value="spring"></constant> <package name="default" extends="struts-default"> <!-- 拦截器 --> <interceptors> <!-- 定义个人拦截器 --> <interceptor name="loginintercept" class="intercept.LoginIntercept"></interceptor> <!-- 拦截器栈 --> <interceptor-stack name="mystack"> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="loginintercept"></interceptor-ref> </interceptor-stack> </interceptors> <!-- default interceptor stack --> <!-- <default-interceptor-ref name="mystack"/> --> <default-action-ref name="index" /> <!-- 全局result --> <global-results> <result name="login">/WEB-INF/html_doc/login.jsp</result> <result name="error">/WEB-INF/html_doc/error.jsp</result> </global-results> <action name="index" class="action.IndexAction"> <result name="success">/WEB-INF/html_doc/index.html</result> </action> <action name="testinter" class="action.IndexAction"> <result name="success" type="freemarker">/WEB-INF/html_doc/user/userwelcome.html</result> <!-- 定义测试拦截器 --> <interceptor-ref name="mystack"></interceptor-ref> </action> </package> <!-- Add packages here --> <include file="struts/struts_user.xml"></include> <!-- 添加用户配置文件 --> <include file="struts/struts_auth.xml"></include><!-- 添加权限配置action --> </struts>
第三个 applicationContext.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: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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <!-- 切面类 --> <bean id="aop" class="aop.userlogin.UserLoginAop"></bean> <!-- 配置AOP --> <aop:config> <aop:aspect ref="aop" id="myregister"> <aop:pointcut id="registerpec" expression="execution(* service.user.UserService.*(..))"/> <aop:before method="doBefore" pointcut-ref="registerpec" /> <!-- <aop:after method="doAfter" pointcut-ref="registerpec" /> --> </aop:aspect> </aop:config> <!-- 数据源配置 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/test"></property> <property name="username" value="root"></property> <property name="password" value="12345"></property> </bean> <!-- 定义Spring JDBC模板类bean --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- dao --> <bean id="userDao" class="dao.impl.UserDaoImpl"> <property name="jdbcTemplate" ref="jdbcTemplate" ></property> </bean> <bean id="authDao" class="dao.impl.AuthDaoImpl"> <property name="jdbcTemplate" ref="jdbcTemplate" ></property> </bean> <!-- service --> <bean id="userService" class="service.user.UserService"> <property name="userDao" ref="userDao"></property> </bean> <bean id="authService" class="service.auth.AuthService"> <property name="authDao" ref="authDao"></property> </bean> <!-- action --> <bean id="register" class="action.user.UserAction"> <property name="userService" ref="userService"></property> </bean> <bean id="login" class="action.user.UserAction"> <property name="userService" ref="userService"></property> </bean> <bean id="authaction" class="action.auth.AuthAction"> <property name="authService" ref="authService"></property> </bean> <bean id="showuser" class="action.user.UserAction"> <property name="userService" ref="userService"></property> </bean> </beans>
第四个:log4j.properties(只在控制台输出log信息)
log4j.rootCategory=INFO,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) -%m%n
第五个:urlrewrite.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN" "\\urlrewrite3.2.dtd"> <urlrewrite> <rule> <note>将根目录下所有html资源重写成/xxx.action</note> <note>example:/index.action</note> <from>/([A-Za-z0-9]+).html</from> <to type="forward">/$1.action</to> </rule> </urlrewrite>
第六个:字符编码过滤器
package wang.util; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class EncodeFilter implements Filter { private String requestEncoding=""; private String responseEncoding=""; public void destroy() { // TODO Auto-generated method stub /* System.out.println("字符编码过滤器销毁!"); */ } public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { // TODO Auto-generated method stub /* System.out.println("编码过滤器运行中。。。"); */ request.setCharacterEncoding(requestEncoding); response.setContentType(responseEncoding); filterChain.doFilter(request, response); } public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub System.out.println("字符编码过滤器初始化!"); requestEncoding=arg0.getInitParameter("requestEncoding"); responseEncoding=arg0.getInitParameter("responseEncoding"); } }
第七个:结合上面编码过滤器的配置文件
<filter> <filter-name>filterEncoding</filter-name> <filter-class>wang.util.EncodeFilter</filter-class> <init-param> <param-name>requestEncoding</param-name> <param-value>utf-8</param-value> </init-param> <init-param> <param-name>responseEncoding</param-name> <param-value>text/html;charset=utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>filterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
还有项目中所涉及的所有包:
包的下载地址:http://pan.baidu.com/s/1pJugUAB