ssh框架搭建Struts2 06+spring2 5+hibernate3 2整合实例代码教程步骤

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

原创整理不易,转载请注明出处:ssh框架搭建Struts2.06+spring2.5+hibernate3.2整合实例代码教程步骤

代码下载地址:http://www.zuidaima.com/share/1760074977233920.htm

最近闲来无事可做,于是开始学习struts2。Struts2和struts1、webwork2有什么区别我也不说了,网上有很多这方面的资料。以前在项目中从未使用过struts,一直使用spring+hibernate,现在既然学习了Struts,也不能浪费,于是乎开始琢磨着怎么整合这3个框架。整合原理以spring为容器,管理hibernate的DAO和Struts2的Action。
一、 准备工作
Struts2.06+spring2.5+hibernate3.2+jdk6.0+myeclipse6.0+tomcat5.5+mysql5.0
以上是整合的原料。下面以一个注册登陆的例子来开始我们的整合过程。
这个例子很简单,下面是它的sql脚本内容:

CREATE TABLE `zuidaima_user``userid` int(11) NOT NULL AUTO_INCREMENT,  `username` varchar(20) NOT NULL`password` varchar(16) NOT NULL`email` varchar(30) NOT NULL,  PRIMARY KEY (`userid`)) ENGINE=InnoDB DEFAULT CHARSET=utf-8;


二、 开始行动
包结构可以参考下图
 
图一

 图二

图三
1) Struts部分:建立struts.xml和struts.properties
Struts.xml内容如下:

  1. <? xml version="1.0" encoding="UTF-8" ?>  
  2. <! DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd" >  
  3. <struts>  
  4.     <package  name ="user_curd"  extends ="struts-default"  >  
  5.         <global-results>  
  6.             <!--  下面定义的结果对所有的Action都有效  -->  
  7.             <result  name ="exception"> /error.jsp </result>  
  8.         </global-results>  
  9.   
  10.         <global-exception-mappings>  
  11.             <!--  指Action抛出Exception异常时,转入名为exception的结果。  -->  
  12.             <exception-mapping  exception ="java.lang.Exception"  result ="exception" />  
  13.         </global-exception-mappings>  
  14.   
  15.         <action  name ="Login"  class ="LoginAction">  
  16.             <result  name ="success"> /success.jsp </result>       
  17.             <result  name ="input"> /login.jsp </result>  
  18.         </action>  
  19.         <action  name ="Regist"  class ="RegistAction">  
  20.             <result  name ="success"> /success.jsp </result>       
  21.             <result  name ="input"> /regist.jsp </result>  
  22.         </action>  
  23.     </package>    
  24. </struts>  
 <? 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>     <package  name ="user_curd"  extends ="struts-default"  >         <global-results>             <!--  下面定义的结果对所有的Action都有效  -->             <result  name ="exception"> /error.jsp </result>         </global-results>         <global-exception-mappings>             <!--  指Action抛出Exception异常时,转入名为exception的结果。  -->             <exception-mapping  exception ="java.lang.Exception"  result ="exception" />         </global-exception-mappings>         <action  name ="Login"  class ="LoginAction">             <result  name ="success"> /success.jsp </result>                  <result  name ="input"> /login.jsp </result>         </action>         <action  name ="Regist"  class ="RegistAction">             <result  name ="success"> /success.jsp </result>                  <result  name ="input"> /regist.jsp </result>         </action>     </package>   </struts>


Struts.properties内容如下:

  1. struts.devMode = false  
  2. struts.enable.DynamicMethodInvocation = true  
  3. struts.i18n.reload = true  
  4. struts.ui.theme =xhtml  
  5.   
  6. struts.locale = zh_CN  
  7. struts.i18n.encoding = UTF- 8  
  8. struts.objectFactory = spring  
  9. struts.objectFactory.spring.autoWire = name  
  10.   
  11. struts.serve.static.browserCache = false  
  12. struts.url.includeParams = none  
struts.devMode = falsestruts.enable.DynamicMethodInvocation = truestruts.i18n.reload = truestruts.ui.theme =xhtmlstruts.locale = zh_CNstruts.i18n.encoding = UTF- 8struts.objectFactory = springstruts.objectFactory.spring.autoWire = namestruts.serve.static.browserCache = falsestruts.url.includeParams = none


2) 建立User.java和User.hbm.xml、jdbc.properties:
User.java内容如下:

/**  *  * @author <a href="mailto:flustar2008@163.com">flustar</a> * @version 1.0  * Creation date: Dec 23, 2007 1:55:28 PM */package com.firstssh.model;import java.io.Serializable;public class User implements Serializable {    private int id;    private String username;    private String password;    private String email;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }    public String getEmail() {        return email;    }    public void setEmail(String email) {        this.email = email;    }}


User.hbm.xml内容:

  1. <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC   
  3.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5. <hibernate-mapping   
  6.     package="com.firstssh.model">  
  7.   
  8.     <class name="User" table="User">  
  9.         <id name="id" column="userid">  
  10.             <generator class="identity" />  
  11.         </id>      
  12.         <property name="username"  
  13.                   column="username"  
  14.                   not-null="true"  
  15.                   length="20"  
  16.         />  
  17.         <property name="password"  
  18.                   column="password"  
  19.                   not-null="true"  
  20.                   length="16" />  
  21.         <property name="email"  
  22.                   column="email"  
  23.                   not-null="true"  
  24.                   length="30"/>  
  25.     </class>  
  26.       
  27. </hibernate-mapping>  
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping     package="com.firstssh.model">    <class name="User" table="User">        <id name="id" column="userid">            <generator class="identity" />        </id>            <property name="username"                  column="username"                  not-null="true"                  length="20"        />        <property name="password"                  column="password"                  not-null="true"                  length="16" />        <property name="email"                  column="email"                  not-null="true"                  length="30"/>    </class>    </hibernate-mapping>


jdbc.properties内容如下:

  1. datasource.type = mysql  
  2. datasource.driverClassName = com.mysql.jdbc.Driver  
  3. datasource.url = jdbc:mysql://localhost: 3306 /test?useUnicode = true&characterEncoding = UTF- 8  
  4.   
  5. datasource.username = root  
  6. datasource.password = 123456  
  7.   
  8. datasource.maxActive = 10  
  9. datasource.maxIdle = 2  
  10. datasource.maxWait = 120000  
  11. datasource.whenExhaustedAction = 1  
  12. datasource.validationQuery = select  1  from dual  
  13. datasource.testOnBorrow = true  
  14. datasource.testOnReturn = false  
  15.   
  16. c3p0.acquireIncrement = 3  
  17. c3p0.initialPoolSize = 3  
  18. c3p0.idleConnectionTestPeriod = 900  
  19. c3p0.minPoolSize = 2  
  20. c3p0.maxPoolSize = 50  
  21. c3p0.maxStatements = 100  
  22. c3p0.numHelperThreads = 10  
  23. c3p0.maxIdleTime = 600  
  24.   
  25. hibernate.dialect = org.hibernate.dialect.MySQLInnoDBDialect  
  26. #hibernate.dialect = org.hibernate.dialect.MySQLMyISAMDialect  
  27.   
  28. hibernate.jdbc.batch_size = 25  
  29. hibernate.jdbc.fetch_size = 50  
  30. hibernate.show_sql = true  
  31. hibernate.connection.release_mode = after_transaction  
datasource.type = mysqldatasource.driverClassName = com.mysql.jdbc.Driverdatasource.url = jdbc:mysql://localhost: 3306 /test?useUnicode = true&characterEncoding = UTF- 8datasource.username = rootdatasource.password = 123456datasource.maxActive = 10datasource.maxIdle = 2datasource.maxWait = 120000datasource.whenExhaustedAction = 1datasource.validationQuery = select  1  from dualdatasource.testOnBorrow = truedatasource.testOnReturn = falsec3p0.acquireIncrement = 3c3p0.initialPoolSize = 3c3p0.idleConnectionTestPeriod = 900c3p0.minPoolSize = 2c3p0.maxPoolSize = 50c3p0.maxStatements = 100c3p0.numHelperThreads = 10c3p0.maxIdleTime = 600hibernate.dialect = org.hibernate.dialect.MySQLInnoDBDialect#hibernate.dialect = org.hibernate.dialect.MySQLMyISAMDialecthibernate.jdbc.batch_size = 25hibernate.jdbc.fetch_size = 50hibernate.show_sql = truehibernate.connection.release_mode = after_transaction


3) Spirng部分:为了清晰把Spring的配置文件拆分成以下几部分applicationContext-dao.xml、appliationContext-service.xml、applicationContext-hibernate.xml、action-servlet.xml。
applicationContext-hibernate.xml内容:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">  
  3. <beans>  
  4. <bean id="propertyConfigurer"  
  5.     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  6.     <property name="locations">  
  7.         <list>  
  8.             <!--  <value>WEB-INF/mail.properties</value>-->  
  9.             <value>WEB-INF/jdbc.properties</value>  
  10.             <!--  <value>WEB-INF/oscache.properties</value>-->  
  11.         </list>  
  12.     </property>  
  13. </bean>  
  14. <!-- MailSender used by EmailAdvice -->  
  15. <!--  
  16.     <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">  
  17.     <property name="host" value="${mail.host}"/>  
  18.     </bean>  
  19. -->  
  20. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"  
  21.     destroy-method="close" dependency-check="none">  
  22.     <property name="driverClass">  
  23.         <value>${datasource.driverClassName}</value>  
  24.     </property>  
  25.     <property name="jdbcUrl">  
  26.         <value>${datasource.url}</value>  
  27.     </property>  
  28.     <property name="user">  
  29.         <value>${datasource.username}</value>  
  30.     </property>  
  31.     <property name="password">  
  32.         <value>${datasource.password}</value>  
  33.     </property>  
  34.     <property name="acquireIncrement">  
  35.         <value>${c3p0.acquireIncrement}</value>  
  36.     </property>  
  37.     <property name="initialPoolSize">  
  38.         <value>${c3p0.initialPoolSize}</value>  
  39.     </property>  
  40.     <property name="minPoolSize">  
  41.         <value>${c3p0.minPoolSize}</value>  
  42.     </property>  
  43.     <property name="maxPoolSize">  
  44.         <value>${c3p0.maxPoolSize}</value>  
  45.     </property>  
  46.     <property name="maxIdleTime">  
  47.         <value>${c3p0.maxIdleTime}</value>  
  48.     </property>  
  49.     <property name="idleConnectionTestPeriod">  
  50.         <value>${c3p0.idleConnectionTestPeriod}</value>  
  51.     </property>  
  52.     <property name="maxStatements">  
  53.         <value>${c3p0.maxStatements}</value>  
  54.     </property>  
  55.     <property name="numHelperThreads">  
  56.         <value>${c3p0.numHelperThreads}</value>  
  57.     </property>  
  58. </bean>  
  59. <bean id="sessionFactory"  
  60.     class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  61.     <property name="dataSource">  
  62.         <ref local="dataSource" />  
  63.     </property>  
  64.     <property name="mappingResources">  
  65.         <list>  
  66.             <value>com/firstssh/model/User.hbm.xml</value>  
  67.         </list>  
  68.     </property>  
  69.     <property name="hibernateProperties">  
  70.         <props>  
  71.             <prop key="hibernate.dialect">${hibernate.dialect}</prop>  
  72.             <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>  
  73.             <prop key="hibernate.jdbc.fetch_size">  
  74.                 ${hibernate.jdbc.fetch_size}  
  75.             </prop>  
  76.             <prop key="hibernate.jdbc.batch_size">  
  77.                 ${hibernate.jdbc.batch_size}  
  78.             </prop>  
  79.         </props>  
  80.     </property>  
  81. </bean>  
  82. <!-- 配置事务管理器bean,使用HibernateTransactionManager事务管理器 -->  
  83. <bean id="transactionManager"  
  84.     class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  85.         <!-- 为事务管理器注入sessionFactory" -->  
  86.         <property name="sessionFactory" ref="sessionFactory"/>  
  87. </bean>  
  88. <!-- 配置事务拦截器Bean -->  
  89. <bean id="transactionInterceptor"  
  90.     class="org.springframework.transaction.interceptor.TransactionInterceptor">  
  91.     <!-- 为事务拦截器bean注入一个事物管理器 -->  
  92.     <property name="transactionManager" ref="transactionManager"></property>  
  93.     <property name="transactionAttributes">  
  94.     <!-- 定义事务传播属性 -->  
  95.         <props>  
  96.                 <prop key="insert*">PROPAGATION_REQUIRED</prop>  
  97.                 <prop key="update*">PROPAGATION_REQUIRED</prop>  
  98.                 <prop key="save*">PROPAGATION_REQUIRED</prop>  
  99.                 <prop key="add*">PROPAGATION_REQUIRED</prop>  
  100.                 <prop key="remove*">PROPAGATION_REQUIRED</prop>  
  101.                 <prop key="delete*">PROPAGATION_REQUIRED</prop>  
  102.                 <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>  
  103.                 <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>  
  104.                 <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>  
  105.                 <prop key="change*">PROPAGATION_REQUIRED</prop>  
  106.                 <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>  
  107.         </props>  
  108.     </property>  
  109. </bean>  
  110. <!-- 定义BeanNameAutoProxyCreator -->  
  111. <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
  112.     <!-- 指定满足哪些bean自动生成业务代理 -->  
  113.     <property name="beanNames">  
  114.     <!-- 需要自动创建事务代理的bean -->  
  115.         <list>  
  116.             <value>userService</value>  
  117.         </list>  
  118.         <!-- 其它需要自动创建事务代理的bean -->  
  119.     </property>  
  120.     <property name="interceptorNames">  
  121.         <list>  
  122.             <value>transactionInterceptor</value>  
  123.             <!-- 可增加其它的interceptor -->  
  124.         </list>  
  125.     </property>  
  126. </bean>  
  127. </beans>  
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"><beans><bean id="propertyConfigurer"    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    <property name="locations">        <list>            <!--  <value>WEB-INF/mail.properties</value>-->            <value>WEB-INF/jdbc.properties</value>            <!--  <value>WEB-INF/oscache.properties</value>-->        </list>    </property></bean><!-- MailSender used by EmailAdvice --><!--    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">    <property name="host" value="${mail.host}"/>    </bean>--><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"    destroy-method="close" dependency-check="none">    <property name="driverClass">        <value>${datasource.driverClassName}</value>    </property>    <property name="jdbcUrl">        <value>${datasource.url}</value>    </property>    <property name="user">        <value>${datasource.username}</value>    </property>    <property name="password">        <value>${datasource.password}</value>    </property>    <property name="acquireIncrement">        <value>${c3p0.acquireIncrement}</value>    </property>    <property name="initialPoolSize">        <value>${c3p0.initialPoolSize}</value>    </property>    <property name="minPoolSize">        <value>${c3p0.minPoolSize}</value>    </property>    <property name="maxPoolSize">        <value>${c3p0.maxPoolSize}</value>    </property>    <property name="maxIdleTime">        <value>${c3p0.maxIdleTime}</value>    </property>    <property name="idleConnectionTestPeriod">        <value>${c3p0.idleConnectionTestPeriod}</value>    </property>    <property name="maxStatements">        <value>${c3p0.maxStatements}</value>    </property>    <property name="numHelperThreads">        <value>${c3p0.numHelperThreads}</value>    </property></bean><bean id="sessionFactory"    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">    <property name="dataSource">        <ref local="dataSource" />    </property>    <property name="mappingResources">        <list>            <value>com/firstssh/model/User.hbm.xml</value>        </list>    </property>    <property name="hibernateProperties">        <props>            <prop key="hibernate.dialect">${hibernate.dialect}</prop>            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>            <prop key="hibernate.jdbc.fetch_size">                ${hibernate.jdbc.fetch_size}            </prop>            <prop key="hibernate.jdbc.batch_size">                ${hibernate.jdbc.batch_size}            </prop>        </props>    </property></bean><!-- 配置事务管理器bean,使用HibernateTransactionManager事务管理器 --><bean id="transactionManager"    class="org.springframework.orm.hibernate3.HibernateTransactionManager">        <!-- 为事务管理器注入sessionFactory" -->        <property name="sessionFactory" ref="sessionFactory"/></bean><!-- 配置事务拦截器Bean --><bean id="transactionInterceptor"    class="org.springframework.transaction.interceptor.TransactionInterceptor">    <!-- 为事务拦截器bean注入一个事物管理器 -->    <property name="transactionManager" ref="transactionManager"></property>    <property name="transactionAttributes">    <!-- 定义事务传播属性 -->        <props>                <prop key="insert*">PROPAGATION_REQUIRED</prop>                <prop key="update*">PROPAGATION_REQUIRED</prop>                <prop key="save*">PROPAGATION_REQUIRED</prop>                <prop key="add*">PROPAGATION_REQUIRED</prop>                <prop key="remove*">PROPAGATION_REQUIRED</prop>                <prop key="delete*">PROPAGATION_REQUIRED</prop>                <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>                <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>                <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>                <prop key="change*">PROPAGATION_REQUIRED</prop>                <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>        </props>    </property></bean><!-- 定义BeanNameAutoProxyCreator --><bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">    <!-- 指定满足哪些bean自动生成业务代理 -->    <property name="beanNames">    <!-- 需要自动创建事务代理的bean -->        <list>            <value>userService</value>        </list>        <!-- 其它需要自动创建事务代理的bean -->    </property>    <property name="interceptorNames">        <list>            <value>transactionInterceptor</value>            <!-- 可增加其它的interceptor -->        </list>    </property></bean></beans>

applicationContext-dao.xml内容:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">  
  3. <beans>  
  4. <!-- 根DAO -->  
  5. <bean id="genericDao" class="com.firstssh.common.dao.GenericDao">  
  6.     <property name="sessionFactory">  
  7.         <ref bean="sessionFactory" />  
  8.     </property>  
  9. </bean>  
  10. <bean id="userDao" class="com.firstssh.dao.impl.UserDao" parent="genericDao" />  
  11. </beans>  
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"><beans><!-- 根DAO --><bean id="genericDao" class="com.firstssh.common.dao.GenericDao">    <property name="sessionFactory">        <ref bean="sessionFactory" />    </property></bean><bean id="userDao" class="com.firstssh.dao.impl.UserDao" parent="genericDao" /></beans>


applicationContext-service.xml内容:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">  
  3. <beans>  
  4. <bean id="userService" class="com.firstssh.service.impl.UserService">  
  5.     <property name="userDao">  
  6.         <ref bean="userDao"/>  
  7.     </property>  
  8. </bean>  
  9. <bean id="validateName" class="com.firstssh.common.Bean.ValidateName">  
  10.     <property name="userService">  
  11.         <ref local="userService"/>  
  12.     </property>  
  13. </bean>  
  14. </beans>  
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"><beans><bean id="userService" class="com.firstssh.service.impl.UserService">    <property name="userDao">        <ref bean="userDao"/>    </property></bean><bean id="validateName" class="com.firstssh.common.Bean.ValidateName">    <property name="userService">        <ref local="userService"/>    </property></bean></beans>


action-servlet.xml内容:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">  
  3. <beans>  
  4.   
  5.     <bean id="LoginAction" class="com.firstssh.action.LoginAction"  
  6.         scope="prototype">  
  7.         <property name="userService" ref="userService" />  
  8.     </bean>  
  9.     <bean id="RegistAction" class="com.firstssh.action.RegistAction"  
  10.         scope="prototype">  
  11.         <property name="userService" ref="userService" />  
  12.     </bean>  
  13. </beans>  
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"><beans>    <bean id="LoginAction" class="com.firstssh.action.LoginAction"        scope="prototype">        <property name="userService" ref="userService" />    </bean>    <bean id="RegistAction" class="com.firstssh.action.RegistAction"        scope="prototype">        <property name="userService" ref="userService" />    </bean></beans>


以上几个xml文件的内容暂且不要理会,继续往下看,你就自动明白的,不用我解释。
4)日志部分:log4j.properties 、commons-logging.properties
log4j.properties内容:

  1. # For JBoss: Avoid to setup Log4J outside $JBOSS_HOME/server/default/deploy/log4j.xml!  
  2. # For all other servers: Comment out the Log4J listener in web.xml to activate Log4J.  
  3. log4j.rootLogger=INFO, stdout, logfile  
  4.   
  5. log4j.appender.stdout=org.apache.log4j.ConsoleAppender  
  6. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  
  7. log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n  
  8.   
  9. log4j.appender.logfile=org.apache.log4j.RollingFileAppender  
  10. log4j.appender.logfile.File=${firstssh.root}/WEB-INF/logs/firstssh.log  
  11. log4j.appender.logfile.MaxFileSize=512KB  
  12. # Keep three backup files.  
  13. log4j.appender.logfile.MaxBackupIndex=3  
  14. # Pattern to output: date priority [category] - message  
  15. log4j.appender.logfile.layout=org.apache.log4j.PatternLayout  
  16. log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n  
  17. # OpenSymphony Stuff  
  18. log4j.logger.com.opensymphony=INFO  
  19. log4j.logger.org.apache.struts2=INFO  
  20. # Spring Stuff  
  21. log4j.logger.org.springframework=INFO  
  22. # Hibernate Stuff  
  23. log4j.logger.org.hiberante=INFO  
# For JBoss: Avoid to setup Log4J outside $JBOSS_HOME/server/default/deploy/log4j.xml!# For all other servers: Comment out the Log4J listener in web.xml to activate Log4J.log4j.rootLogger=INFO, stdout, logfilelog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%nlog4j.appender.logfile=org.apache.log4j.RollingFileAppenderlog4j.appender.logfile.File=${firstssh.root}/WEB-INF/logs/firstssh.loglog4j.appender.logfile.MaxFileSize=512KB# Keep three backup files.log4j.appender.logfile.MaxBackupIndex=3# Pattern to output: date priority [category] - messagelog4j.appender.logfile.layout=org.apache.log4j.PatternLayoutlog4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n# OpenSymphony Stufflog4j.logger.com.opensymphony=INFOlog4j.logger.org.apache.struts2=INFO# Spring Stufflog4j.logger.org.springframework=INFO# Hibernate Stufflog4j.logger.org.hiberante=INFO

commons-logging.properties
内容:

  1. org.apache.commons.logging.Log = org.apache.commons.logging.impl.Log4JLogger  
org.apache.commons.logging.Log = org.apache.commons.logging.impl.Log4JLogger


5)web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns="http://java.sun.com/xml/ns/j2ee"   
  3.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"   
  5.  version="2.4">  
  6.  <display-name>firstssh</display-name>  
  7.  <description>this is a simple example</description>  
  8.  <context-param>  
  9.   <param-name>webAppRootKey</param-name>  
  10.   <param-value>firstssh.root</param-value>  
  11.  </context-param>  
  12.    
  13.  <context-param>  
  14.   <param-name>contextConfigLocation</param-name>  
  15.   <param-value>/WEB-INF/applicationContext-*.xml,/WEB-INF/action-servlet.xml</param-value>  
  16.  </context-param>  
  17.  <context-param>  
  18.   <param-name>log4jConfigLocation</param-name>  
  19.   <param-value>/WEB-INF/log4j.properties</param-value>  
  20.  </context-param>  
  21.  <listener>  
  22.   <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
  23.  </listener>  
  24.  <!-- 用于初始化Spring容器的Listener -->  
  25.     <listener>  
  26.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  27.     </listener>  
  28.    
  29.    
  30.  <!-- 定义整合SiteMesh必须的ActionContextCleanUp Filter   
  31.  <
  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

参与评论 您还未登录,请先 登录 后发表或查看评论
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

沙漠中的车

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值