SSH集成
Struts和Spring的集成, 配置文件比较麻烦, 这里采用插件形式
步骤:
一、 新建工程: 为工程添加Spring和Struts支持, 添加Spring时,应该注意将几个勾打上AOP , WEB
二、新建DAO类, 接口(如BaseDao 接口和它的实现类 UserDao)
public interface BaseDao {
public String queryUser();
}
public class UserDao implements BaseDao {
public String queryUser(){
System.out.println("调用真实DAO的方法");
return "我是真的逗你玩";
}
}
三、写Struts中的Action(使用setter方法注入)
public class HelloAction extends Action{
private BaseDao base;//使用接口标识类型
public BaseDao getBase() {
return base;
}
public void setBase(BaseDao base) {
this.base = base;
}
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
String name=request.getParameter("userName");
System.out.println(name);
String str = base.queryUser();
return mapping.findForward("success");
}
}
四、配置struts-config.xml文件
<action-mappings>
<action path="/queryuser" type="org.springframework.web.struts.DelegatingActionProxy">
<forward name="success" path="/main.jsp"></forward>
</action>
</action-mappings>
<!—以插件的形式装载struts-->
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/classes/applicationContext.xml"/>
</plug-in>
五、配置applicationContext.xml文件, 使用它将对象注入到Action中
<bean id="userdao" class="com.doin.struts.dao.UserDao" />
//此处名称要和action中的path属性值相同
<bean name="/queryuser" class="com.doin.struts.action.HelloAction">
<property name="base" ref="userdao"></property>
</bean>
需要更改的地方:
在structs-config.xml文件的
action配置中 type=”org.springframework.web.struts.DelegatingActionProxy”
另外以插件的形式集成到spring
<plug-in className=”org.springframework.web.struts.ContextLoaderPlugIn”>
<set-property
property=”contextConfigLocation”
value=”/WEB-INF/classes/applicationContext.xml”>spring配置文件路径
</plug-in>
然后在applicationContext.xml文件中
配置实现类对象,以及Action
<bean id=”baseDao” class=””/>
<bean name=”此值为你在Action中配置的path值”>
Spring与Hibernate的集成
导入包commons.pool.jar, commons.dbcp.jar …log.jar,dom4j,commons-collections.jar,jta.jar.
第一种配置方案:
导入spring时要加上hibernate3,AOP,core 库文件
1、 生成hibernate.cfg.xml和映射文件以及VO
2、 修改applicationContext.xml文件:如下所示
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="file:src/hibernate.cfg.xml"/>
</bean>
然后配置DAO,这个DAO是访问数据库所用
<bean id="customerDao" class="com.dao.CustomerDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
最后写DAO继承HibernateDaoSupport
public Customers queryCustomer() {
Customers c = (Customers)this.getHibernateTemplate().get(Customers.class,"Tom");
Set set = c.getOrderses();
}
第二种方案, 使用纯Spring配置
主要还是配置applicationContext.xml
第一步: 创建数据源
必须导入三个包commons-dbcp.jar/commons-logging.jar/commons-pool-1.2.jar
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"></property>
<property name="url" value="jdbc:microsoft:sqlserver://localhost:1433;databaseName=shopping"></property>
<property name="username">
<value>sa</value>
</property>
<property name="password">
<value></value>
</property>
</bean>
第二步:
创建SessionFactory
<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.SQLServerDialect
</prop>
<prop key="hibernate.show_sql">
true
</prop>
<prop key="hibernate.format_sql">
true
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/vo/Customers.hbm.xml</value>
<value>com/vo/Orders.hbm.xml</value>
<value>com/vo/Orderdetail.hbm.xml</value>
<value>com/vo/Products.hbm.xml</value>
<value>com/vo/Categories.hbm.xml</value>
</list>
</property>
</bean>
第三步:配置DAO
<bean id="customerDao" class="com.dao.CustomerDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
第四步: 继承HibernateDaoSupport
public class CustomerDao extends HibernateDaoSupport {
public Customers queryCustomer() {
Customers c = (Customers)this.getHibernateTemplate().get(Customers.class, "Tom");
Set set = c.getOrderses();
}
集成Struts Spring Hibernate
配置文件和前面Sprint Struts 和 Spring Hibernate配置一样
只需要在dao中实现接口, 此接口是注入到Action中所必须的
如果不想实现接口, 则可以在Action中直接写DAO对象
如下:
private UserDAO userDao; //并添加getter和setter方法
配置则写成如下形式:
<bean id="user" class="com.dao.UserDAO">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean name="/register" class="com.action.UserAction">
<property name="userDao" ref="user"/>
</bean>
这样,就已经完成了SSH的配置
但在保存对象时, 需要处理事务
接下来配置事务, 仅在applicationContext.xml中作配置即可
具体配置如下:
1、配置sessionFactory ,此处会自动生成
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
2、配置DAO
<bean id="user" class="com.dao.UserDAO">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
3、 配置事务管理器
<bean id="myHibTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
4、 配置事务代理
<bean id="baseTransProxy" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager"
ref="myHibTransactionManager" />
<property name="proxyTargetClass" value="true"/>
<property name="transactionAttributes">
<props>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="del*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="do*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
5、使用baseTransProxy代理userDao
<bean id="userDao" parent="baseTransProxy">
<property name="target" ref="user" />
</bean>
6、 最后配置注入, 此时不是直接注入user, 而是它的代理基类userDao
<bean name="/register" class="com.action.UserAction">
<property name="userDao" ref="userDao"/>//此处的userDao和第5步的id=”userDao”对应
</bean>
如果出现以下异常:
严重: action: null
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userProxy' defined in ServletContext resource [/WEB-INF/classes/applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.objectweb.asm.ClassVisitor.visit(IILjava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)V
Caused by: java.lang.NoSuchMethodError: org.objectweb.asm.ClassVisitor.visit(IILjava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)V
原因是Hibernate和Spring中的类冲突
删除一个jar包即可: asm-2.2.3.jar
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wanghui5493701/archive/2010/01/04/5130123.aspx