以前用过struts,hibernate做过东西,也做过spring测试的例子,但是没有将它们整合起来一起用过!现在把他们整合了,以后就用这3个框架做东西了!现在把代码写在这里,做个记号,同时也给大家参考下!
1.LoginAction的代码:(没有用struts的验证,开始不太明白这个validator,这2天正在做struts的验证测试,随后给出)
public
class
LoginAction
extends
Action
...
{
private UserDAO userDAO;
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) ...{
// 没有用struts的验证
// DynaValidatorForm loginForm = (DynaValidatorForm) form;
// String username = (String)loginForm.get("username");
// String password = (String)loginForm.get("password");
String username=request.getParameter("username");
String password=request.getParameter("password");
if (userDAO.isValidUser(username,password)) ...{
return mapping.findForward("success");
}else ...{
return mapping.getInputForward();
}
}
public UserDAO getUserDAO() ...{
return userDAO;
}
public void setUserDAO(UserDAO userDAO) ...{
this.userDAO = userDAO;
}
}
private UserDAO userDAO;
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) ...{
// 没有用struts的验证
// DynaValidatorForm loginForm = (DynaValidatorForm) form;
// String username = (String)loginForm.get("username");
// String password = (String)loginForm.get("password");
String username=request.getParameter("username");
String password=request.getParameter("password");
if (userDAO.isValidUser(username,password)) ...{
return mapping.findForward("success");
}else ...{
return mapping.getInputForward();
}
}
public UserDAO getUserDAO() ...{
return userDAO;
}
public void setUserDAO(UserDAO userDAO) ...{
this.userDAO = userDAO;
}
}
2.struts-config.xml
<?
xml version
=
"
1.0
"
encoding
=
"
UTF-8
"
?>
<! DOCTYPE struts - config PUBLIC " -//Apache Software Foundation//DTD Struts Configuration 1.2//EN " " http://struts.apache.org/dtds/struts-config_1_2.dtd " >
< struts - config >
< global - forwards >
< forward name = " success " path = " /success.jsp " />
</ global - forwards >
< action - mappings >
< action input = " /login2.jsp " path = " /login " scope = " request " type = " org.springframework.web.struts.DelegatingActionProxy " />
</ action - mappings >
< message - resources parameter = " com.test.struts.ApplicationResources " />
< plug - in className = " org.springframework.web.struts.ContextLoaderPlugIn " >
< set - property property = " contextConfigLocation " value = " /WEB-INF/applicationContext.xml " />
</ plug - in >
</ struts - config >
<! DOCTYPE struts - config PUBLIC " -//Apache Software Foundation//DTD Struts Configuration 1.2//EN " " http://struts.apache.org/dtds/struts-config_1_2.dtd " >
< struts - config >
< global - forwards >
< forward name = " success " path = " /success.jsp " />
</ global - forwards >
< action - mappings >
< action input = " /login2.jsp " path = " /login " scope = " request " type = " org.springframework.web.struts.DelegatingActionProxy " />
</ action - mappings >
< message - resources parameter = " com.test.struts.ApplicationResources " />
< plug - in className = " org.springframework.web.struts.ContextLoaderPlugIn " >
< set - property property = " contextConfigLocation " value = " /WEB-INF/applicationContext.xml " />
</ plug - in >
</ struts - config >
3.applicationConte
<?
xml version
=
"
1.0
"
encoding
=
"
UTF-8
"
?>
<! DOCTYPE beans PUBLIC " -//SPRING//DTD BEAN//EN " " http://www.springframework.org/dtd/spring-beans.dtd " >
< beans >
< bean name = " /login " class = " com.test.struts.action.LoginAction " singleton = " false " >
< property name = " userDAO " >< ref local = " userDAOProxy " /></ property >
</ bean >
< bean id = " userDAOProxy " class = " org.springframework.transaction.interceptor.TransactionProxyFactoryBean " >
< property name = " transactionManager " >< ref local = " transactionManager " /></ property >
< property name = " target " >< ref local = " userDAO " /></ property >
< property name = " transactionAttributes " >
< props >
< prop key = " is* " > PROPAGATION_REQUIRED,readOnly </ prop >
</ props >
</ property >
</ bean >
< bean id = " transactionManager " class = " org.springframework.orm.hibernate3.HibernateTransactionManager " >
< property name = " sessionFactory " >< ref local = " sessionFactory " /></ property >
</ bean >
< bean id = " userDAO " class = " com.test.UserDAOImp " >
< property name = " sessionFactory " >< ref local = " sessionFactory " /></ property >
</ bean >
< bean id = " sessionFactory " class = " org.springframework.orm.hibernate3.LocalSessionFactoryBean " >
< property name = " dataSource " >< ref bean = " dataSource " /></ property >
< property name = " mappingResources " >
< list >< value > com / test / User.hbm.xml </ value ></ list >
</ property >
< property name = " hibernateProperties " >
< props >
< prop key = " hibernate.dialect " > org.hibernate.dialect.SQLServerDialect </ prop >
< prop key = " hibernate.show_sql " > true </ prop >
</ props >
</ property >
</ bean >
< bean id = " dataSource " class = " org.apache.commons.dbcp.BasicDataSource " >
< property name = " driverClassName " >< value > com.microsoft.jdbc.sqlserver.SQLServerDriver </ value ></ property >
< property name = " url " >< value > jdbc:microsoft:sqlserver: // localhost:2000;DatabaseName=test;SelectMethod=cursor</value></property>
< property name = " username " >< value > sa </ value ></ property >
< property name = " password " >< value > 123 </ value ></ property >
</ bean >
</ beans >
<! DOCTYPE beans PUBLIC " -//SPRING//DTD BEAN//EN " " http://www.springframework.org/dtd/spring-beans.dtd " >
< beans >
< bean name = " /login " class = " com.test.struts.action.LoginAction " singleton = " false " >
< property name = " userDAO " >< ref local = " userDAOProxy " /></ property >
</ bean >
< bean id = " userDAOProxy " class = " org.springframework.transaction.interceptor.TransactionProxyFactoryBean " >
< property name = " transactionManager " >< ref local = " transactionManager " /></ property >
< property name = " target " >< ref local = " userDAO " /></ property >
< property name = " transactionAttributes " >
< props >
< prop key = " is* " > PROPAGATION_REQUIRED,readOnly </ prop >
</ props >
</ property >
</ bean >
< bean id = " transactionManager " class = " org.springframework.orm.hibernate3.HibernateTransactionManager " >
< property name = " sessionFactory " >< ref local = " sessionFactory " /></ property >
</ bean >
< bean id = " userDAO " class = " com.test.UserDAOImp " >
< property name = " sessionFactory " >< ref local = " sessionFactory " /></ property >
</ bean >
< bean id = " sessionFactory " class = " org.springframework.orm.hibernate3.LocalSessionFactoryBean " >
< property name = " dataSource " >< ref bean = " dataSource " /></ property >
< property name = " mappingResources " >
< list >< value > com / test / User.hbm.xml </ value ></ list >
</ property >
< property name = " hibernateProperties " >
< props >
< prop key = " hibernate.dialect " > org.hibernate.dialect.SQLServerDialect </ prop >
< prop key = " hibernate.show_sql " > true </ prop >
</ props >
</ property >
</ bean >
< bean id = " dataSource " class = " org.apache.commons.dbcp.BasicDataSource " >
< property name = " driverClassName " >< value > com.microsoft.jdbc.sqlserver.SQLServerDriver </ value ></ property >
< property name = " url " >< value > jdbc:microsoft:sqlserver: // localhost:2000;DatabaseName=test;SelectMethod=cursor</value></property>
< property name = " username " >< value > sa </ value ></ property >
< property name = " password " >< value > 123 </ value ></ property >
</ bean >
</ beans >
4.UserDAOImp.java(UserDAO的代码没有给出)
import
org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
public class UserDAOImp extends HibernateDaoSupport implements UserDAO ... {
private static String hql = "from User as u where u.username=:userName"+" and u.password=:passWord";
public boolean isValidUser(String username, String password) ...{
// Query query=sessionFactory.getCurrentSession().createQuery(hql);
Query query=this.getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(hql);
query.setString("userName", username);
query.setString("passWord", password);
return (query.list().size()>0);
}
}
import org.hibernate.Query;
import org.hibernate.SessionFactory;
public class UserDAOImp extends HibernateDaoSupport implements UserDAO ... {
private static String hql = "from User as u where u.username=:userName"+" and u.password=:passWord";
public boolean isValidUser(String username, String password) ...{
// Query query=sessionFactory.getCurrentSession().createQuery(hql);
Query query=this.getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(hql);
query.setString("userName", username);
query.setString("passWord", password);
return (query.list().size()>0);
}
}