关于struts、hibernate、spring
2012-10-12 14:05
399 查看
原本遇到了很多的问题,现在想写下来却又懒得写了,简要记录下吧。
1、关于jar
struts2的jar:(我用的是struts2.0.11.2)
struts2-core-x.x.x.x.jar
xwork-x.x.x.jar
ognl-x.x.x.jar
freemarker-x.x.x.jar
commons-logging-x.x.x.jar
hibernate用到的jar:(我用的是hibernate3)
hibernate3.jar
以及required文件夹下所有的jar:
antlr-x.x.x.jar
commons-collections-x.x.jar
dom4j-x.x.x.jar
javassist-x.x.GA.jar
jta-x.x.jar
slf4j-api-x.x.x.jar
还要加上slf4j-log4j12-1.5.2.jar这个包,估计是记日志什么的吧,没仔细看过,网上很好找到这个jar
还要有个相关的数据库驱动jar
最后还需要有log4j-x.x.x.jar这个jar,记日志总会有好处,反正struts是用log4j级日志的,谁让都是apache的呢,再说apeche实在是很强大,关于log4j的配置网上很多,查查就行
spring用到的jar:(3.1.1)
org.springframework.asm-3.1.1.RELEASE.jar
org.springframework.beans-3.1.1.RELEASE.jar
org.springframework.context.support-3.1.1.RELEASE.jar
org.springframework.context-3.1.1.RELEASE.jar
org.springframework.core-3.1.1.RELEASE.jar
org.springframework.expression-3.1.1.RELEASE.jar
org.springframework.web-3.1.1.RELEASE.jar
以及struts2结合spring用到的jar:struts2-spring-plugin-2.0.11.2.jar
以及spring结合hibernate用到jar:org.springframework.orm-3.1.1.RELEASE.jar、org.springframework.transaction-3.1.1.RELEASE.jar
因为用到了spring的aop来管理事务啥的,现在还不太了解,不过用到了jar包括org.springframework.aop-3.1.1.RELEASE.jar、aopalliance-1.0.jar、aspectjweaver.jar、org.springframework.jdbc-3.1.1.RELEASE.jar
其中如果用aopalliance-alpha1.jar仍然有缺失的类,要用aopalliance-1.0.jar。
不得不说的是jar问题是随着你搭建不断出现的问题,往往后台报错找不到class的时候都去搜索下看看是少了什么jar,通常都能解决。
2、具体代码
先说web.xml
要增加struts2的过滤器配置;spring的参数和监听;因为用spring结合hibernate,还需要增加spring的整合hibernate的监听,代码如下:
看到网上的例子都用spring来管理strurs的action,这让我很惊讶,我相信大项目这样做是有其优势的,解耦啥的,我这种层次的开发直接忽略吧,仍然用struts管理,下面写下struts的配置文件:
struts.xml
但是相关的业务处理类还是用spring来管理会比较好,但是因为后面用到了aop,所以必须实现个接口,这让我很不爽,难道说是因为注入都要通过接口实现,将来有时间做深入研究。
struts.properties文件其中需要写struts.locale、encoding以及和spring整合自动装配模式等
此处做简单的单表维护测试
baseinfo.line.xml:
LineAction:
LineBusiness为接口,因为测试只写了一个save方法
具体业务实现类LineBusinessManager还要继承HibernateDaoSupport类来管理事务
那么就需要配置spring配置文件来控制业务接口注入
applicationContext-business.xml
hibernate整合交由spring管理事务
applicationContext-common.xml
需要说明的是相关的通配符说明参照了下面说明
http://blog.sina.com.cn/s/blog_667528fd0100rcmr.html
其中execution(* cn.com.sgcc.fuda..*Business.*(..))表示了在包cn.com.sgcc.fuda和其子包下的任意实现了后缀是Business接口的实现类。具体参照http://www.blogjava.net/freeman1984/archive/2010/02/25/313886.html
再剩下的就是hibernate的配置文件hibernate.cfg.xml
需要注意的是<property name="current_session_context_class">thread</property>需要去掉。
就写这么多吧,剩下的都是枝梢末节,懒得写了,最后写下参考的网页
http://www.cnitblog.com/intrl/archive/2009/04/13/56322.html
http://hbiao68.iteye.com/blog/1454192
http://blog.csdn.net/kkdelta/article/details/5587659
http://www.blogjava.net/cmzy/archive/2008/07/20/216176.html
http://woaiwofengkuang.iteye.com/blog/165926
http://www.2cto.com/kf/201205/133479.html
http://blog.sina.com.cn/s/blog_667528fd0100rcmr.html
1、关于jar
struts2的jar:(我用的是struts2.0.11.2)
struts2-core-x.x.x.x.jar
xwork-x.x.x.jar
ognl-x.x.x.jar
freemarker-x.x.x.jar
commons-logging-x.x.x.jar
hibernate用到的jar:(我用的是hibernate3)
hibernate3.jar
以及required文件夹下所有的jar:
antlr-x.x.x.jar
commons-collections-x.x.jar
dom4j-x.x.x.jar
javassist-x.x.GA.jar
jta-x.x.jar
slf4j-api-x.x.x.jar
还要加上slf4j-log4j12-1.5.2.jar这个包,估计是记日志什么的吧,没仔细看过,网上很好找到这个jar
还要有个相关的数据库驱动jar
最后还需要有log4j-x.x.x.jar这个jar,记日志总会有好处,反正struts是用log4j级日志的,谁让都是apache的呢,再说apeche实在是很强大,关于log4j的配置网上很多,查查就行
spring用到的jar:(3.1.1)
org.springframework.asm-3.1.1.RELEASE.jar
org.springframework.beans-3.1.1.RELEASE.jar
org.springframework.context.support-3.1.1.RELEASE.jar
org.springframework.context-3.1.1.RELEASE.jar
org.springframework.core-3.1.1.RELEASE.jar
org.springframework.expression-3.1.1.RELEASE.jar
org.springframework.web-3.1.1.RELEASE.jar
以及struts2结合spring用到的jar:struts2-spring-plugin-2.0.11.2.jar
以及spring结合hibernate用到jar:org.springframework.orm-3.1.1.RELEASE.jar、org.springframework.transaction-3.1.1.RELEASE.jar
因为用到了spring的aop来管理事务啥的,现在还不太了解,不过用到了jar包括org.springframework.aop-3.1.1.RELEASE.jar、aopalliance-1.0.jar、aspectjweaver.jar、org.springframework.jdbc-3.1.1.RELEASE.jar
其中如果用aopalliance-alpha1.jar仍然有缺失的类,要用aopalliance-1.0.jar。
不得不说的是jar问题是随着你搭建不断出现的问题,往往后台报错找不到class的时候都去搜索下看看是少了什么jar,通常都能解决。
2、具体代码
先说web.xml
要增加struts2的过滤器配置;spring的参数和监听;因为用spring结合hibernate,还需要增加spring的整合hibernate的监听,代码如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app 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" version="2.5"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</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>hibernateFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>hibernateFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
看到网上的例子都用spring来管理strurs的action,这让我很惊讶,我相信大项目这样做是有其优势的,解耦啥的,我这种层次的开发直接忽略吧,仍然用struts管理,下面写下struts的配置文件:
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> <include file="cn/com/sgcc/fuda/baseinfo/line/baseinfo.line.xml" /> </struts>
但是相关的业务处理类还是用spring来管理会比较好,但是因为后面用到了aop,所以必须实现个接口,这让我很不爽,难道说是因为注入都要通过接口实现,将来有时间做深入研究。
struts.properties文件其中需要写struts.locale、encoding以及和spring整合自动装配模式等
struts.locale=zh_CN struts.i18n.encoding=UTF-8 #\u5728\u751f\u4ea7\u73af\u5883\u4e2d\uff0c\u5e94\u8be5\u8bbe\u7f6e\u4e3afalse struts.configuration.xml.reload=false struts.multipart.saveDir=\temp struts.multipart.maxSize=10240 struts.xslt.nocache=false struts.ui.templateDir = template struts.objectFactory = spring struts.objectFactory.spring.autoWire = auto #\u5728\u5f00\u53d1\u73af\u5883\u4e2d\uff0c\u8bbe\u7f6e\u4e3atrue struts.devMode = true
此处做简单的单表维护测试
baseinfo.line.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> <package name="baseinfo.line.xml" extends="struts-default" namespace="/baseinfo/line"> <action name="getList" class="cn.com.sgcc.fuda.baseinfo.line.LineAction"> <result name="success" type="dispatcher"> <param name="location">line_list.jsp</param> </result> </action> <action name="new"> <result>line_detail.jsp</result> </action> <action name="save" class="cn.com.sgcc.fuda.baseinfo.line.LineAction" method="save"> <result name="success" type="dispatcher"> <param name="location">line_list.jsp</param> </result> </action> </package> </struts>
LineAction:
package cn.com.sgcc.fuda.baseinfo.line; import com.opensymphony.xwork2.ActionSupport; public class LineAction extends ActionSupport{ private Line line; private LineBusiness lineBusiness; public Line getLine() { return line; } public void setLine(Line line) { this.line = line; } public LineBusiness getLineBusiness() { return lineBusiness; } public void setLineBusiness(LineBusiness lineBusiness) { this.lineBusiness = lineBusiness; } public String save(){ getLineBusiness().save(getLine()); return SUCCESS; } }
LineBusiness为接口,因为测试只写了一个save方法
package cn.com.sgcc.fuda.baseinfo.line; public interface LineBusiness { public void save(Line line); }
具体业务实现类LineBusinessManager还要继承HibernateDaoSupport类来管理事务
package cn.com.sgcc.fuda.baseinfo.line; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; public class LineBusinessManager extends HibernateDaoSupport implements LineBusiness{ /** * 保存或者更新线路信息 * @param line */ public void save(Line line){ this.getHibernateTemplate().saveOrUpdate(line); } }
那么就需要配置spring配置文件来控制业务接口注入
applicationContext-business.xml
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> <bean id="lineBusiness" class="cn.com.sgcc.fuda.baseinfo.line.LineBusinessManager" > <property name="sessionFactory" ref="sessionFactory"></property> </bean> </beans>
hibernate整合交由spring管理事务
applicationContext-common.xml
<?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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <!-- 配置sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"><ref bean="sessionFactory" /></property> </bean> <!-- 那些类的哪些方法参与事务 --> <aop:config> <aop:advisor pointcut="execution(* cn.com.sgcc.fuda..*Business.*(..))" advice-ref="txAdvice" /> </aop:config> <!-- 配置事务的传播特性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="save*" propagation="REQUIRED" /> <!-- <tx:method name="*" read-only="true" /> --> </tx:attributes> </tx:advice> </beans>
需要说明的是相关的通配符说明参照了下面说明
http://blog.sina.com.cn/s/blog_667528fd0100rcmr.html
其中execution(* cn.com.sgcc.fuda..*Business.*(..))表示了在包cn.com.sgcc.fuda和其子包下的任意实现了后缀是Business接口的实现类。具体参照http://www.blogjava.net/freeman1984/archive/2010/02/25/313886.html
再剩下的就是hibernate的配置文件hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:frozsoil</property> <property name="connection.username">frozensoil</property> <property name="connection.password">frozensoil</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">5</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <!-- Enable Hibernate's automatic session context management--> <!-- <property name="current_session_context_class">thread</property> --> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <property name="hibernate.format_sql">true</property> <!-- Drop and re-create the database schema on startup --> <!-- <property name="hbm2ddl.auto">create</property> --> <mapping resource="cn/com/sgcc/fuda/baseinfo/line/baseinfo.line.hbm.xml" /> </session-factory> </hibernate-configuration>
需要注意的是<property name="current_session_context_class">thread</property>需要去掉。
就写这么多吧,剩下的都是枝梢末节,懒得写了,最后写下参考的网页
http://www.cnitblog.com/intrl/archive/2009/04/13/56322.html
http://hbiao68.iteye.com/blog/1454192
http://blog.csdn.net/kkdelta/article/details/5587659
http://www.blogjava.net/cmzy/archive/2008/07/20/216176.html
http://woaiwofengkuang.iteye.com/blog/165926
http://www.2cto.com/kf/201205/133479.html
http://blog.sina.com.cn/s/blog_667528fd0100rcmr.html
相关文章推荐
- ssh中关于spring,struts,hibernate的工作机制与原理详解
- Java高级工程师面试题目汇集(关于Struts,Spring,Hibernate三大框架的面试)
- 关于Struts、Spring和Hibernate整合的问题
- Java高级工程师面试题目汇集(关于Struts,Spring,Hibernate三大框架的面试)
- 关于MYECLIPSE开发Spring+Struts+Hibernate时用SQL2000出现驱动不匹配的问题
- 关于struts、spring 和 hibernate的说明
- 关于Struts, Spring, Hibernate的一点文字
- 关于Struts、Hibernate、Spring的简单理解
- Java高级工程师面试题目汇集(关于Struts,Spring,Hibernate三大框架的面试)
- 关于Struts+Spring+Hibernate的理解
- 关于Struts Spring Hibernate 的优缺点
- Java高级工程师面试题目汇集(关于Struts,Spring,Hibernate三大框架的面试)
- SSH学习笔记之关于struts-spring-hibernate整合配置问
- Java高级工程师面试题目汇集(关于Struts,Spring,Hibernate三大框架的面试)
- 关于SSH整合, Struts2.3,Hibernate3,Spring3.2
- 关于SSH框架配置文件 Hibernate.cfg.xml 、struts.xml、spring.xml
- Java高级工程师面试题目汇集(关于Struts,Spring,Hibernate三大框架的面试)
- 关于Hibernate,Spring,Struts一些问题
- 关于struts+spring+hibernate的相关总结(一)
- 关于spring+hibernate+struts架构service层的作用