s2sh整合之xml方式
说明:本文档所采用的框架版本为:Struts 2.1.8,Sping2.5.5,Hibernate 3.5.6
1.需要的jar包:
------------Strut2-----------:
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-logging.jar
freemarker-2.3.15.jar
ognl-2.7.3.jar
struts2-core-2.1.8.1.jar
struts2-spring-plugin-2.1.8.1.jar
xwork-core-2.1.6.jar
-------------Spring---------------:
aspectjrt.jar
aspectjweaver.jar
cglib-nodep-2.1_3.jar
common-annotations.jar
commons-lang.jar
commons-logging.jar
spring.jar
-----------Hibernate------------:
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
hibernate3.jar
javassist-3.9.0.GA.jar
jta-1.1.jar
slf4j-api-1.5.8.jar
slf4j-log4j12.jar
------------other-----------------
c3p0-0.9.1.2.jar
json-lib-2.1.jar
jstl.jar
junit.jar
log4j-1.2.15.jar
MysqL-connector-java-5.1.10-bin.jar
standard.jar
2.web.xml
<?xmlversion=@H_301_89@"1.0"encoding=@H_301_89@"UTF-8"?>
<web-appxmlns:xsi=@H_301_89@"http://www.w3.org/2001/XMLSchema-instance"
xmlns=@H_301_89@"http://java.sun.com/xml/ns/javaee"
xmlns:web=@H_301_89@"http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation=@H_301_89@"http://java.sun.com/xml/ns/javaee
@H_301_89@ http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"version=@H_301_89@"2.5">
<!-- spring config begin -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- spring config end-->
<filter>
<filter-name>Spring character encoding filter</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>
</filter>
<filter-mapping>
<filter-name>Spring character encoding filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- struts config begin-->
<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>/*</url-pattern>
</filter-mapping>
<!-- struts config end-->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
3.applicationContext.xml
<?xmlversion=@H_301_89@"1.0"encoding=@H_301_89@"UTF-8"?>
<beansxmlns=@H_301_89@"http://www.springframework.org/schema/beans"
xmlns:xsi=@H_301_89@"http://www.w3.org/2001/XMLSchema-instance"
xmlns:context=@H_301_89@"http://www.springframework.org/schema/context"
xmlns:aop=@H_301_89@"http://www.springframework.org/schema/aop"
xmlns:tx=@H_301_89@"http://www.springframework.org/schema/tx"
xsi:schemaLocation=@H_301_89@"http://www.springframework.org/schema/beans
@H_301_89@ http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
@H_301_89@http://www.springframework.org/schema/context
@H_301_89@http://www.springframework.org/schema/context/spring-context-2.5.xsd
@H_301_89@http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
@H_301_89@http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<import resource=@H_301_89@"applicationContext-db.xml"/>
<!-- dao service 默认使用单例 -->
<beanid=@H_301_89@"userDao"class=@H_301_89@"cn.s2sh.demo.dao.impl.UserDaoImpl">
<propertyname=@H_301_89@"sessionFactory"ref=@H_301_89@"sessionFactory"></property>
</bean>
<beanid=@H_301_89@"userService"class=@H_301_89@"cn.s2sh.demo.service.impl.UserServiceImpl">
<propertyname=@H_301_89@"userDao"ref=@H_301_89@"userDao"/>
</bean>
<!-- action 不能使用单例,设置scope="prototype" -->
<beanid=@H_301_89@"userAction"class=@H_301_89@"cn.s2sh.demo.struts2.action.UserAction"scope=@H_301_89@"prototype">
<propertyname=@H_301_89@"userService"ref=@H_301_89@"userService"></property>
</bean>
</beans>
4.applicationContext-db.xml
<?xmlversion=@H_301_89@"1.0"encoding=@H_301_89@"UTF-8"?>
<beansxmlns=@H_301_89@"http://www.springframework.org/schema/beans"
xmlns:xsi=@H_301_89@"http://www.w3.org/2001/XMLSchema-instance"
xmlns:context=@H_301_89@"http://www.springframework.org/schema/context"
xmlns:aop=@H_301_89@"http://www.springframework.org/schema/aop"
xmlns:tx=@H_301_89@"http://www.springframework.org/schema/tx"
xsi:schemaLocation=@H_301_89@"http://www.springframework.org/schema/beans
@H_301_89@http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
@H_301_89@http://www.springframework.org/schema/context
@H_301_89@ http://www.springframework.org/schema/context/spring-context-2.5.xsd
@H_301_89@http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd
@H_301_89@http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<beanid=@H_301_89@"sessionFactory"class=@H_301_89@"org.springframework.orm.hibernate3.LocalSessionfactorybean">
<propertyname=@H_301_89@"configLocation">
<value>classpath:hibernate/hibernate.cfg.xml</value>
</property>
</bean>
<beanid=@H_301_89@"transactionManager"class=@H_301_89@"org.springframework.orm.hibernate3.HibernateTransactionManager">
<propertyname=@H_301_89@"sessionFactory">
<refbean=@H_301_89@"sessionFactory"/>
</property>
</bean>
<tx:advicetransaction-manager=@H_301_89@"transactionManager"id=@H_301_89@"tx">
<tx:attributes>
<tx:methodname=@H_301_89@"save*"read-only=@H_301_89@"false"/>
<tx:methodname=@H_301_89@"delete*"read-only=@H_301_89@"false"/>
<tx:methodname=@H_301_89@"update*"read-only=@H_301_89@"false"/>
<tx:methodname=@H_301_89@"*"read-only=@H_301_89@"true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcutexpression=@H_301_89@"execution(*cn.s2sh.demo.service.impl.*.*(..))"id=@H_301_89@"perform"/>
<aop:advisoradvice-ref=@H_301_89@"tx"pointcut-ref=@H_301_89@"perform"/>
</aop:config>
</beans>
5.hibernate.cfg.xml
<?xmlversion=@H_301_89@'1.0'encoding=@H_301_89@'UTF-8'?>
<!DOCTYPEhibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse HibernateTools. -->
<hibernate-configuration>
<session-factory>
<propertyname=@H_301_89@"dialect">
org.hibernate.dialect.MysqLDialect
</property>
<propertyname=@H_301_89@"connection.url">jdbc:MysqL://localhost:3306/s2shdemo</property>
<propertyname=@H_301_89@"connection.username">root</property>
<propertyname=@H_301_89@"connection.password">root</property>
<propertyname=@H_301_89@"connection.driver_class">
com.MysqL.jdbc.Driver
</property>
<propertyname=@H_301_89@"myeclipse.connection.profile">MysqL</property>
<propertyname=@H_301_89@"hbm2ddl.auto">update</property>
<propertyname=@H_301_89@"show_sql">true</property>
<mappingresource=@H_301_89@"cn/s2sh/demo/domain/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
6.struts.xml
<?xmlversion=@H_301_89@"1.0"encoding=@H_301_89@"UTF-8"?>
<!DOCTYPEstruts PUBLIC
"-//Apache Software Foundation//DTD StrutsConfiguration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<!-- 开发模式下使用,这样可以打印出更详细的错误信息,默认值为false -->
<constantname=@H_301_89@"struts.devMode"value=@H_301_89@"true"/>
<!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->
<constantname=@H_301_89@"struts.serve.static.browserCache"value=@H_301_89@"false"/>
<!-- 默认的视图主题 simple模式减少多余代码的生成 -->
<constantname=@H_301_89@"struts.ui.theme"value=@H_301_89@"simple"/>
<!-- 指定Web应用的默认编码集,相当于调用setCharacterEncoding方法 -->
<constantname=@H_301_89@"struts.i18n.encoding"value=@H_301_89@"UTF-8"/>
<constantname=@H_301_89@"struts.custom.i18n.resources"
value=@H_301_89@"cn.s2sh.action.token"></constant>
<!-- 把struts的请求委托给spring管理,
作用:创建Action实例的过程由spring处理,其他的还是由struts2自己处理 -->
<constantname=@H_301_89@"struts.objectFactory"value=@H_301_89@"spring"/>
<packagename=@H_301_89@"user"namespace=@H_301_89@"/"extends=@H_301_89@"struts-default">
<actionname=@H_301_89@"userAction_*"class=@H_301_89@"userAction"method=@H_301_89@"{1}">
<resultname=@H_301_89@"login">login.jsp</result>
<resultname=@H_301_89@"index">index.jsp</result>
<resultname=@H_301_89@"listAction">WEB-INF/jsp/user/list.jsp</result>
<resultname=@H_301_89@"action2action"type=@H_301_89@"redirectAction">userAction_getAllUser</result>
<resultname=@H_301_89@"addUI">WEB-INF/jsp/user/saveUI.jsp</result>
<resultname=@H_301_89@"updateUI">WEB-INF/jsp/user/updateUI.jsp</result>
</action>
</package>
</struts>
下载完整demo: http://download.csdn.net/detail/rchm8519/7513457