如果有看不清楚的请查看我的收藏可以看原代码的...
做一个ssh的例子:需要有以下几步:
这是在我的例子中的主要步骤:
他是spring整合struts/hibernate的
第一:在文件struts-config.xml中需要配置以下两个内容
<!---->
<action-mappings></action-mappings>
<action attribute="loginForm"></action> input="/login.jsp"
name="loginForm"
parameter="method"
path="/login"
type="org.lbg.ssh.struts.action.LoginAction" scope="request">
<forward name="add_succ" path="/add_succ.jsp"></forward>
<forward name="login_succ" path="/login_succ.jsp"></forward>
<forward name="login_fail" path="/login_fail.jsp"></forward>
<!---->
<action-mappings></action-mappings>
<action attribute="loginForm"></action> input="/login.jsp"
name="loginForm"
parameter="method"
path="/login"
scope="request">
<forward name="add_succ" path="/add_succ.jsp"></forward>
<forward name="login_succ" path="/login_succ.jsp"></forward>
<forward name="login_fail" path="/login_fail.jsp"></forward>
<1>控制器
<!---->
<!---->
<controller></controller>
<set-property value="org.springframework.web.struts.DelegatingRequestProcessor" property="processorClass"></set-property>
<2>插件
<!---->
<plug-in classname="org.springframework.web.struts.ContextLoaderPlugIn"></plug-in>
<set-property value="/WEB-INF/applicationContext.xml" property="contextConfigLocation"></set-property>
第二:applicationContext.xml文件的配置:
然而ssh的使用前提是将struts/hibernate交给了spring来管理
此时就不用创建hibernate的工厂类了和hibernate.cfg.xml文件了
而在:hibernate.cfg.xml文件中的数据源的配置(及其其他信息)全部交给了spring来管理了.
这时spring文件中的内容
<!---->
<!---->
<beans></beans>
<!---->
<bean id="dataSoruce"></bean> class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"></property> value="com.microsoft.jdbc.sqlserver.SQLServerDriver">
<property name="url"></property> value="jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=differ">
<property name="username" value="sa"></property>
<property name="password" value="bb"></property>
<!---->
<bean id="sessionFactory"></bean> class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSoruce"></ref>
</property>
<property name="hibernateProperties">
<props></props>
<prop key="hibernate.dialect"></prop>
org.hibernate.dialect.SQLServerDialect
</property>
<property name="mappingResources">
<list></list>
<value></value>org/lbg/ssh/domain/Login.hbm.xml
</property>
<!---->
<bean class="org.lbg.ssh.dao.BaseDAO" id="baseDAO"></bean>
<property name="sessionFactory">
<ref bean="sessionFactory"></ref>
</property>
<!---->
<bean id="hibernateTemplate"></bean> class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref local="sessionFactory"></ref>
</property>
<!---->
<bean id="transactionManager"></bean> class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"></ref>
</property>
<!---->
<bean id="abstractTxProxy"></bean> class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
<property name="transactionManager">
<ref local="transactionManager"></ref>
</property>
<property name="transactionAttributes">
<props></props>
<prop key="add*"></prop> PROPAGATION_REQUIRED,-Exception
<prop key="delete*"></prop>
PROPAGATION_REQUIRED,-Exception
<prop key="update*"></prop>
PROPAGATION_REQUIRED,-Exception
<prop key="get*"></prop> PROPAGATION_REQUIRED,readOnly
<prop key="*"></prop> PROPAGATION_REQUIRED,readOnly
</property>
<!---->
<!---->
<bean class="org.lbg.ssh.dao.impl.LoginDAOImpl" id="loginDAOImpl"></bean>
<property name="hibernateTemplate">
<ref local="hibernateTemplate"></ref>
</property>
<property name="baseDAO">
<ref local="baseDAO"></ref>
</property>
<!---->
<!---->
<bean id="loginService" parent="abstractTxProxy"></bean>
<property name="target">
<bean class="org.lbg.ssh.service.LoginService"></bean>
<property name="loginDAO">
<ref bean="loginDAOImpl"></ref>
</property>
</property>
<!---->
<!---->
<bean class="org.lbg.ssh.struts.action.LoginAction" name="/login"></bean>
<property name="loginService">
<ref bean="loginService"></ref>
</property>
<!---->
<!---->
package org.lbg.ssh.dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
<!---->
public class BaseDAO {
public BaseDAO() {
}
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public Session getSession() {
return this.sessionFactory.openSession();
}
}
<!---->
package org.lbg.ssh.dao.impl;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Expression;
import org.lbg.ssh.dao.BaseDAO;
import org.lbg.ssh.dao.LoginDAO;
import org.lbg.ssh.domain.Login;
import org.springframework.orm.hibernate3.HibernateTemplate;
<!---->
public class LoginDAOImpl implements LoginDAO {
/**模板类对象**/
private HibernateTemplate hibernateTemplate;
/**使用此对象可返回sessionFactory工厂对象/session对象**/
private BaseDAO baseDAO;
public BaseDAO getBaseDAO() {
return baseDAO;
}
public void setBaseDAO(BaseDAO baseDAO) {
this.baseDAO = baseDAO;
}
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
/** ************************************************** */
public void addNewInfo(Login login) {
hibernateTemplate.save(login);
}
public boolean loginValidate(Login login) {
//代码的实现。。。
}
public boolean testFind(Login login) {
Session session = baseDAO.getSession();
Transaction tr = null;
try {
tr = session.beginTransaction();
//代码的实现。。。
} catch (Exception er) {
er.printStackTrace();
return false;
} finally {
session.close();
}
}
}
<!---->
主要:
spring-aop.jar
spring-web.jar
hibernate3.jar
spring-hibernate.jar
spring-orm.jar
spring-context.jar