一、创建SSH2项目,导入Struts2并测试。
web.xml
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
index.jsp
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
*.action
default.jsp
default pageUserAction.java
packagecom.cwq.action;importcom.opensymphony.xwork2.ActionSupport;public class UserAction extendsActionSupport {
@Overridepublic String execute() throwsException {//TODO Auto-generated method stub//return super.execute();
returnSUCCESS;
}/***@paramargs*/
public static voidmain(String[] args) {//TODO Auto-generated method stub
}
}
struts.xml
default.jsp
发布项目,启动Tomcat,运行结果:
二、导入Spring包,配置数据源
三,Spring与Hibernate整合
在web.xml中加入
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
index.jsp
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
*.action
applicationContext.xml(自动生成)
org.hibernate.dialect.MySQLDialect
测试,发布,启动Tomcat,如果启动成功则整合成功。
四、根据数据库反向生成代码
Users.java
packagecom.cwq.model;importjava.sql.Timestamp;/*** Users entity.@authorMyEclipse Persistence Tools*/
public class Users implementsjava.io.Serializable {//Fields
privateInteger id;privateString username;privateString password;privateTimestamp birthdate;//Constructors
/**default constructor*/
publicUsers() {}/**full constructor*/
publicUsers(String username, String password, Timestamp birthdate) {this.username =username;this.password =password;this.birthdate =birthdate;
}//Property accessors
publicInteger getId() {return this.id;
}public voidsetId(Integer id) {this.id =id;
}publicString getUsername() {return this.username;
}public voidsetUsername(String username) {this.username =username;
}publicString getPassword() {return this.password;
}public voidsetPassword(String password) {this.password =password;
}publicTimestamp getBirthdate() {return this.birthdate;
}public voidsetBirthdate(Timestamp birthdate) {this.birthdate =birthdate;
}
}
user.hbm.xml
/p>
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
UserDao.java
packagecom.cwq.dao;importjava.sql.Timestamp;importjava.util.List;importorg.hibernate.LockMode;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.context.ApplicationContext;importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;importcom.cwq.model.Users;/*** A data access object (DAO) providing persistence and search support for Users
* entities. Transaction control of the save(), update() and delete() operations
* can directly support Spring container-managed transactions or they can be
* augmented to handle user-managed Spring transactions. Each of these methods
* provides additional information for how to configure it for the desired type
* of transaction control.
*@seecom.cwq.model.Users
*@authorMyEclipse Persistence Tools*/
public class UsersDAO extendsHibernateDaoSupport {private static final Logger log = LoggerFactory.getLogger(UsersDAO.class);//property constants
public static final String USERNAME = "username";public static final String PASSWORD = "password";protected voidinitDao() {//do nothing
}public voidsave(Users transientInstance) {
log.debug("saving Users instance");try{
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
}catch(RuntimeException re) {
log.error("save failed", re);throwre;
}
}public voiddelete(Users persistentInstance) {
log.debug("deleting Users instance");try{
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
}catch(RuntimeException re) {
log.error("delete failed", re);throwre;
}
}publicUsers findById(java.lang.Integer id) {
log.debug("getting Users instance with id: " +id);try{
Users instance= (Users) getHibernateTemplate().get("com.cwq.model.Users", id);returninstance;
}catch(RuntimeException re) {
log.error("get failed", re);throwre;
}
}publicList findByExample(Users instance) {
log.debug("finding Users instance by example");try{
List results=getHibernateTemplate().findByExample(instance);
log.debug("find by example successful, result size: " +results.size());returnresults;
}catch(RuntimeException re) {
log.error("find by example failed", re);throwre;
}
}publicList findByProperty(String propertyName, Object value) {
log.debug("finding Users instance with property: " + propertyName + ", value: " +value);try{
String queryString= "from Users as model where model." + propertyName + "= ?";returngetHibernateTemplate().find(queryString, value);
}catch(RuntimeException re) {
log.error("find by property name failed", re);throwre;
}
}publicList findByUsername(Object username) {returnfindByProperty(USERNAME, username);
}publicList findByPassword(Object password) {returnfindByProperty(PASSWORD, password);
}publicList findAll() {
log.debug("finding all Users instances");try{
String queryString= "from Users";returngetHibernateTemplate().find(queryString);
}catch(RuntimeException re) {
log.error("find all failed", re);throwre;
}
}publicUsers merge(Users detachedInstance) {
log.debug("merging Users instance");try{
Users result=(Users) getHibernateTemplate().merge(detachedInstance);
log.debug("merge successful");returnresult;
}catch(RuntimeException re) {
log.error("merge failed", re);throwre;
}
}public voidattachDirty(Users instance) {
log.debug("attaching dirty Users instance");try{
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
}catch(RuntimeException re) {
log.error("attach failed", re);throwre;
}
}public voidattachClean(Users instance) {
log.debug("attaching clean Users instance");try{
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
}catch(RuntimeException re) {
log.error("attach failed", re);throwre;
}
}public staticUsersDAO getFromApplicationContext(ApplicationContext ctx) {return (UsersDAO) ctx.getBean("UsersDAO");
}
}
applicationContext.xml
org.hibernate.dialect.MySQLDialect
com/cwq/model/Users.hbm.xml
五、Struts整合Spring
UserAction.java
packagecom.cwq.action;importjava.util.List;importcom.cwq.dao.UsersDAO;importcom.cwq.model.Users;importcom.opensymphony.xwork2.ActionSupport;public class UserAction extendsActionSupport {private List users = null;privateUsersDAO userDao;public voidsetUserDao(UsersDAO userDao) {this.userDao =userDao;
}
@Overridepublic String execute() throwsException {//TODO Auto-generated method stub//return super.execute();
users =userDao.findAll();returnSUCCESS;
}public ListgetUsers() {returnusers;
}public void setUsers(Listusers) {this.users =users;
}/***@paramargs*/
public static voidmain(String[] args) {//TODO Auto-generated method stub
}
}
applicationContext.xml
加入
org.hibernate.dialect.MySQLDialect
com/cwq/model/Users.hbm.xml
struts.xml
加入
default.jsp
default.jsp
default page${user.username}--${user.password}--${user.birthdate}
如果要指定进入某个action加入下面代码
document.location.href = "showUsers.action";
index.jsp
Stringpath=request.getContextPath();StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
">
My JSP 'index.jsp' starting pagedocument.location.href= "showUsers.action";