一、架构信息
Struts2 - jar
commons-fileupload.jar
Commons-io.jar
Freemarker.jar
Ognl.jar
Struts2-core.jar
Xwork-core.jar
servlet-api.jar
Spring3 - jar
spring.jar
commons-logging.jar,
log4j.jar
aspectJjrt.jar,
aspectJweaver.jar,
cglib-nodep.jar
struts2-spring-plugin.jar
Hibernate3 - jar
Hibernate3.jar
Required:
antlr.jar
Commons-collections.jar
Dom4j.jar
Javassist.jar
Jta.jar
Slf4j-api.jar
Slf4j-log4j.jar
mysql-connector-java-bin.jar
Optional:
c3p0.jar
Jpa:
Hibernate-jpa.jar
mysql - jar
mysql-connector-java-5.1.24.jar
二、配置文件
各部分的配置及文件分别是
struts.xml;
Hibernate.cfg.xml;
*.hbm.xml;
jdbc.properties;
Log4j.properties;
applicationContext.xml;
因为Spring集成了Hibernate,所有Hibernate.cfg.xml中的配置就可以直接配置到applicationContext.xml中了.这样一来对于我们这样的Spring爱好者来说就方便多了.有了这些,后面在web.xml中再进行一些配置就可以了.一般我们总是先整合Spring和Hibernate,再整合Struts,不为别的,方便调错.
1. applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!-- 自动扫描与装配bean --> <context:component-scan base-package="com.ziry"></context:component-scan> <!-- 导入外部的properties文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置sessionFactory,将Hibernate中的事务交给spring管理 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 配置数据库连接池,指定sessionFactory中的数据源 --> <property name="dataSource"> <bean class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 数据库连接信息 --> <property name="jdbcUrl" value="${jdbcUrl}"></property> <property name="driverClass" value="${driverClass}"></property> <property name="user" value="${user}"></property> <property name="password" value="${password}"></property> <!-- 其他配置 --> <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 --> <property name="initialPoolSize" value="3"></property> <!--连接池中保留的最小连接数。Default: 3 --> <property name="minPoolSize" value="3"></property> <!--连接池中保留的最大连接数。Default: 15 --> <property name="maxPoolSize" value="5"></property> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --> <property name="acquireIncrement" value="3"></property> <!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 --> <property name="maxStatements" value="8"></property> <!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 --> <property name="maxStatementsPerConnection" value="5"></property> <!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --> <property name="maxIdleTime" value="1800"></property> </bean> </property> <!-- 指定hibernate的属性 --> <property name="hibernateProperties"> <value> <!-- hibernate使用的 方言 --> hibernate.dialect=org.hibernate.dialect.Oracle10gDialect <!-- 根据实体的映射文件生成表结构 --> hibernate.hbm2ddl.auto=update <!-- 是否打印出sql语句 --> hibernate.show_sql=false </value> </property> <!-- Hibernate的映射文件 --> <property name="mappingResources"> <list> <value>com/tgb/oa/domain/User.hbm.xml</value> <value>com/tgb/oa/domain/Role.hbm.xml</value> </list> </property> </bean> <!-- 配置使用基于Hibernate的事务管理器 --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <!-- 通过sessionfactory开启事务 --> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!--声明式事务管理(采用注解的方式) --> <tx:annotation-driven transaction-manager="txManager"/> </beans>
2. jdbc.properties
jdbcUrl=jdbc:mysql://127.0.0.1:3306/ziry driverClass=com.mysql.jdbc.Driver user=root password=123456
3. struts.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" /> <!-- 把扩展名配置为action --> <constant name="struts.action.extension" value="action" /> <!-- 把主题配置为simple --> <constant name="struts.ui.theme" value="simple" /> <package name="default" namespace="/" extends="struts-default"> <!--当struts2与spring整合后,class属性可以写bean的名称 --> <action name="test" class="testAction"> <result name="success">/test.jsp</result> </action> <action name="role_*" class="roleAction" method="{1}"> <result name="list" >/WEB-INF/jsp/roleAction/list.jsp</result> <result name="toList" type="redirectAction">role_list</result> <result name="saveUI">/WEB-INF/jsp/roleAction/saveUI.jsp</result> </action> </package> </struts>
4.web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <!-- 配置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> <!-- 配置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>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
三、完整SSH实例
链接:http://pan.baidu.com/s/1mhUSqyk 密码:u2uy
注意:本文归作者所有,未经作者允许,不得转载