spring2 +hibernate 3 + struts 配置
1. web.xml
1. <?xml version="1.0" encoding="UTF-8"?>
2. <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/javaee"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
5. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
6.
7. <!-- 用于初始化Spring容器的Listener -->
8. <listener>
9. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
10. </listener>
11.
12. <!-- 载入spring配置文件 -->
13. <context-param>
14. <param-name>contextConfigLocation</param-name>
15. <param-value>classpath:spring/*.xml</param-value>
16. </context-param>
17.
18. <!-- struts2本质上是一个filter -->
19. <filter>
20. <filter-name>struts2</filter-name>
21. <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
22. </filter>
23.
24. <filter-mapping>
25. <filter-name>struts2</filter-name>
26. <url-pattern>/*</url-pattern>
27. </filter-mapping>
28.
29. <filter>
30. <filter-name> struts-cleanup </filter-name>
31. <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
32. </filter>
33.
34. <filter-mapping>
35. <filter-name>struts-cleanup</filter-name>
36. <url-pattern>/*</url-pattern>
37. </filter-mapping>
38.
39. <!-- 在freemarker中使用struts2标签 -->
40. <servlet>
41. <servlet-name>JspSupportServlet</servlet-name>
42. <servlet-class>org.apache.struts2.views.JspSupportServlet</servlet-class>
43. <load-on-startup>1</load-on-startup>
44. </servlet>
45.
46. <filter>
47. <filter-name>CharacterEncodingFilter</filter-name>
48. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
49. <init-param>
50. <param-name>encoding</param-name>
51. <param-value>utf-8</param-value>
52. </init-param>
53. </filter>
54.
55. <filter-mapping>
56. <filter-name>CharacterEncodingFilter</filter-name>
57. <url-pattern>/*</url-pattern>
58. </filter-mapping>
59.
60. <welcome-file-list>
61. <welcome-file>index.html</welcome-file>
62. </welcome-file-list>
63.
64. </web-app>
2. struts.xml
1. <struts>
2. <constant name="struts.custom.i18n.resources" value="messageResource" />
3. <constant name="struts.i18n.encoding" value="UTF-8" />
4. <constant name="struts.ui.theme" value="simple" />
5. <constant name="struts.multipart.maxSize" value="1000000000" />
6. <include file="struts/demo-default.xml" />
7. </struts>
3. demo-default.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="demo-default" extends="struts-default" namespace="/">
5. <!-- 使用freemarker只为页面模板 -->
6. <result-types>
7. <result-type name="freemarke" default="true"
8. class="org.apache.struts2.views.freemarker.FreemarkerResult" />
9. </result-types>
10.
11. <!-- 自定义权限拦截器 -->
12. <interceptors>
13. <interceptor name="authority" class="org.demo.common.AuthorityInterceptor" />
14. <interceptor-stack name="demoDefaultStack">
15. <interceptor-ref name="authority" />
16. <interceptor-ref name="defaultStack" />
17. </interceptor-stack>
18. </interceptors>
19. <default-interceptor-ref name="demoDefaultStack" />
20.
21. <global-results>
22. <result name="executeResult">/common/execute_result.ftl</result>
23. <result name="exception">/common/exception.ftl</result>
24. </global-results>
25.
26. <global-exception-mappings>
27. <exception-mapping result="exception" exception="org.cms.common.ExceptionHandle" />
28. </global-exception-mappings>
29. </package>
30.
31. <include file="struts/demo-home.xml" />
32. </struts>
4. demo-home.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="demo-home" extends="demo-default" namespace="/home">
5. <action name="index" class="org.demo.action.home.UserAction"
6. method="index">
7. <result name="index">homepage.ftl</result>
8. </action>
9. </package>
10. </struts>
5. applicationContext.xml - Spring
1. <?xml version="1.0" encoding="GBK"?>
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xsi:schemaLocation="http://www.springframework.org/schema/beans http:/www.springframework.org/schema/beans/spring-beans.xsd">
5.
6. <!-- 定义数据源bean,使用Apache DBCP数据源实现 -->
7. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
8. destroy-method="close">
9. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
10. <property name="url" value="jdbc:mysql://localhost:3306/demo?characterEncoding=utf-8" />
11. <property name="username" value="root" />
12. <property name="password" value="123456" />
13. <!-- 可以同时闲职在连接池中的连接的最大数目 -->
14. <property name="maxIdle" value="10" />
15. <!-- 最大激活连接数 -->
16. <property name="maxActive" value="100" />
17. <!-- 请求连接最常等待时间 -->
18. <property name="maxWait" value="30" />
19. <!-- 是否回收被遗弃的数据库连接到连接池中 -->
20. <property name="removeAbandoned" value="true" />
21. </bean>
22.
23. <!--定义了Hibernate的SessionFactory -->
24. <bean id="sessionFactory"
25. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
26. <!-- 为SessionFactory注入DataSource实例 -->
27. <property name="dataSource" ref="dataSource" />
28. <!-- 指定Hibernate的实体映射文件 -->
29. <property name="mappingResources">
30. <list>
31. <value>classpath:org/demo/domain/</value>
32. </list>
33. </property>
34.
35. <!-- 指定Hibernate连接的系列属性 -->
36. <property name="hibernateProperties">
37. <props>
38. <!-- 指定Hibernate连接数据库的连接方言 -->
39. <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
40. <!-- 是否显示Hibernate产生的SQL语句 -->
41. <prop key="show_sql">true</prop>
42. <!-- 启动应用时,是否根据hbm文件创建数据表 -->
43. <prop key="hibernate.hbm2ddl.auto">update</prop>
44. <!-- 指定Hibernate持久化操作的批操作大小 -->
45. <prop key="hibernate.jdbc.batch_size">20</prop>
46. </props>
47. </property>
48. </bean>
49.
50. <!-- 配置事务管理器,使用Hibernate局部事务管理器策略 -->
51. <bean id="transactionManager"
52. class="org.springframework.orm.hibernate3.HibernateTransactionManager">
53. <property name="sessionFactory" ref="sessionFactory" />
54. </bean>
55.
56. <!-- 配置事务拦截器 -->
57. <bean id="transactionInterceptor"
58. class="org.springframework.transaction.interceptor.TransactionInterceptor">
59. <!-- 事务拦截器bean需要依赖注入一个事务管理器 -->
60. <property name="transactionManager" ref="transactionManager" />
61. <property name="transactionAttributes">
62. <!-- 下面定义事务传播属性 -->
63. <props>
64. <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
65. <prop key="*">PROPAGATION_REQUIRED</prop>
66. </props>
67. </property>
68. </bean>
69.
70. <!-- 定义BeanNameAutoProxyCreator -->
71. <bean
72. class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
73. <!-- 指定对满足哪些bean name的bean自动生成业务代理 -->
74. <property name="beanNames">
75. <!-- 下面是所有需要自动创建事务代理的bean,此处可增加其他需要自动创建事务代理的bean -->
76. <list>
77. <value>demoService</value>
78. </list>
79. </property>
80. <!-- 下面定义BeanNameAutoProxyCreator所需的事务拦截器 -->
81. <property name="interceptorNames">
82. <list>
83. <!-- 此处可增加其他新的Interceptor -->
84. <value>transactionInterceptor</value>
85. </list>
86. </property>
87. </bean>
88.
89. <!-- 定义MailSender Bean,用于发送邮件 -->
90. <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
91. <property name="host" value="smtp.163.com" />
92. <property name="javaMailProperties">
93. <props>
94. <prop key="mail.smtp.auth">true</prop>
95. <prop key="mail.smtp.timeout">25000</prop>
96. </props>
97. </property>
98. <property name="username" value="username" />
99. <property name="password" value="password" />
100. </bean>
101.
102. <!-- 配置MailMessage Bean -->
103. <bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
104. <property name="from" value="username@163.com" />
105. <property name="subject" value="Demo" />
106. </bean>
107.</beans
6. applicationContext-dao.xml
1. <?xml version="1.0" encoding="GBK"?>
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xsi:schemaLocation="http://www.springframework.org/schema/beans http:/www.springframework.org/schema/beans/spring-beans.xsd">
5.
6. <bean id="daoTemplate" abstract="true">
7. <property name="sessionFactory" ref="sessionFactory" />
8. </bean>
9.
10. <bean id="demoDao" class="org.demo.dao.impl.DemoDaoImpl" parent="daoTemplate" />
11.
12. <!-- Dao工厂类 -->
13. <bean id="daoManager" class="org.demo.dao.DaoManager">
14. <property name="demoDao">
15. <ref bean="demoDao" />
16. </property>
17. </bean>
18.
19. </beans>
. applicationContext-service.xml
1. <?xml version="1.0" encoding="GBK"?>
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xsi:schemaLocation="http://www.springframework.org/schema/beans http:/www.springframework.org/schema/beans/spring-beans.xsd">
5.
6. <bean id="demoService" class="org.demo.service.impl.DemoServiceImpl" />
7.
8. <bean id="serviceManager" class="org.demo.service.ServiceManager">
9. <property name="demoService">
10. <ref bean="demoService" />
11. </property>
12. </bean>
13.
14. </beans>
8. freemarker.properties
1. default_encoding=UTF-8
2. number_format=#
3. date_format=yyyy-MM-dd
4. time_format=HH:mm:ss
5. datetime_format=yyyy-MM-dd HH:mm:ss
6. auto_import=/common/page_macro.ftl as p
9. log4j.properties
1. log4j.rootLogger=INFO, CONSOLE, FILE
2.
3. log4j.addivity.org.apache=true
4. log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
5. log4j.appender.Threshold=DEBUG
6. log4j.appender.CONSOLE.Target=System.out
7. log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
8. log4j.appender.CONSOLE.layout.ConversionPattern= %c %x - %m%n
9.
10. log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
11. log4j.appender.FILE.File=webapps/demo/WEB-INF/logs/file.log
12. log4j.appender.FILE.DatePattern=.yyyy-MM-dd
13. log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
14. log4j.appender.FILE.layout.ConversionPattern=[DEMO] %d - %c -%-4r [%t] %-5p %c %x - %m%n