您的位置:首页 > 其它

SSH框架集成详细解读(配源码)

2013-03-24 21:35 211 查看
最近开始做毕业设计,想利用三大框架进行集成开发,自己先做了三大框架集成的测试程序,以便于后续的开发不出现错误。先做个总结,为了更好的性能,DaoSupport(Daosupport只是一种开发的常用的一种通用经验)后续补上。(代码,我会放到资源里面,大家想要的自己去下载,里面的checkLogin()方法实际上是向数据库添加数据,我懒得改了,呵呵)

在三大框架集成中需要几个配置文件,包括如下:
web.xml配置文件:
(1)配置struts2的过滤器 (2)spring框架的配置文件 (3)会话监听器 (4)编码的配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<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>/*</url-pattern>
</filter-mapping>

<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>
applicationContext.xml配置文件:

(1)读取dbcp数据源

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">

(2)Hibernate的常用属性配置(注意到一定要配置方言dialect,否者会报错的)

(3)事务管理的配置

(4)bean的配置

<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
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/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <context:property-placeholder location="classpath:dbcpconfig.properties" />

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>${driverClassName}</value>
</property>

<property name="url">
<value>${url}</value>
</property>

<property name="username">
<value>${username}</value>
</property>

<property name="password">
<value>${password}</value>
</property>

<property name="initialSize">
<value>${initialSize}</value>
</property>

<property name="maxActive">
<value>${maxActive}</value>
</property>

<property name="maxIdle">
<value>${maxIdle}</value>
</property>

<property name="minIdle">
<value>${minIdle}</value>
</property>

<property name="maxWait">
<value>${maxWait}</value>
</property>
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />

<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>

<property name="mappingResources">
<list>
<value>cn/csu/edu/graduateDesign/domain/TestUser.hbm.xml</value>
</list>
</property>
</bean>

<!-- hibernate的事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- 事务传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="*" propagation="NOT_SUPPORTED" />
</tx:attributes>
</tx:advice>

<aop:config>
<aop:pointcut expression="execution (* cn.csu.edu.graduateDesign.service.*.*(..))"
id="AllMethod" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="AllMethod" />
</aop:config>

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="TestUserAction" class="cn.csu.edu.graduateDesign.web.struts.TestUserAction">
<property name="testUserService" ref="testUserService"/>
</bean>

<bean id="testUserService" class="cn.csu.edu.graduateDesign.service.impl.TestUserServiceImpl">
<property name="testUserDao" ref="testUserDao"/>
</bean>

<bean id="testUserDao" class="cn.csu.edu.graduateDesign.dao.impl.TestUserDaoHibernateImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"/>
</bean>
</beans>
(注:配置文件的bean里面引用了hibernateTemplate,这个是spring价包里面的,其他的bean,就是我自己

定义的一些类,在这里面利用set注入的方式进行实例化了,这里面的实例互相引用,我相信大家应该看得

懂)

struts.xml配置文件:

(1)在这个配置文件里面有这样一句配置的情况

<constant name="struts.objectFactory" value="spring"/>

这句话的意思是Struts2的action由Spring来负责进行实例化

<!-- 指定Struts 2默认的ObjectFactory Bean,该属性默认值是spring -->

(2)这个配置文件的主要作用就是进行action的管理,一般先设定包

<package name="user" extends="struts-default">

<action name="user" class="TestUserAction">

<result>/user_list.jsp</result>

<result name="login" type="redirect">/index.jsp</result>

</action>

</package>

/*注意这里面的class没有写完全的路径,因为这里面的这个class的值只需要

和application.xml的id要相同,就是如下这个配置

<bean id="TestUserAction" class="cn.csu.edu.graduateDesign.web.struts.TestUserAction">
<property name="testUserService" ref="testUserService"/>
</bean>
这样这个action的实例就交给了spring进行实例化

*/

dbcp的properties文件:进行数据源的配置(我是连接的oracle数据库)

#连接设置
driverClassName=oracle.jdbc.driver.OracleDriver
url=jdbc\:oracle\:thin\:@localhost\:1521\:orcl
username=liuxiaoming
password=liuxiaoming

#<!-- 初始化连接 -->
initialSize=10

#最大连接数量
maxActive=50

#<!-- 最大空闲连接 -->
maxIdle=20

#<!-- 最小空闲连接 -->
minIdle=5

#<!-- 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 -->
maxWait=60000

#JDBC驱动建立连接时附带的连接属性属性的格式必须为这样:[属性名=property;]
#注意:"user" 与 "password" 两个属性会被明确地传递,因此这里不需要包含他们。
connectionProperties=useUnicode\=true;characterEncoding\=utf-8

#指定由连接池所创建的连接的自动提交(auto-commit)状态。
defaultAutoCommit=true

#driver default 指定由连接池所创建的连接的只读(read-only)状态。
#如果没有设置该值,则“setReadOnly”方法将不被调用。(某些驱动并不支持只读模式,如:Informix)
defaultReadOnly=

#driver default 指定由连接池所创建的连接的事务级别(TransactionIsolation)。
#可用值为下列之一:(详情可见javadoc。)NONE,READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE
defaultTransactionIsolation=READ_UNCOMMITTED


我们发现在集成之后,3大框架的配置文件互相关联,hibernate的配置文件被集成到了application.xml里面了

spring里面封装了hibernate模板,struts里面action的实例化也交给了spring,在整个数据操作的时候的事务也

被进行AOP设置,这些都给我们开发带来了很大的方便

(注:源码我上传到我的资源里面,名字叫SSH集成代码,还有数据库建立语句,我用的是oracle,主键
的自动增长,我是用触发器还有序列实现的,如果大家是用的mysql或者是sqlServer,自动增长是可以直接设置

的,大家有兴趣的可以去下载)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: