Sunday 25 September 2011

Struts2 Hibernate Spring Tutorial

Struts - Hibernate - Spring Application

This is a tutorial that explains how Struts2 , Hibernate and Spring(IoC) is mixed together and used in the same application.

I based my work on the examples given in Vaan Nila Struts2 tutorial. Vaan Nila gives the Struts2Example14 for Struts2 - Spring and Struts2Example17 for Struts2 - Hibernate.

My trick is: I took the Struts2Example17 project and added it the lib/jars of the Struts2Example14. Then I changed the struts.xml from:

/register.jsp

to: (my StrutsHibernateSpring application)

/register.jsp


Similar Struts2Example14 's WEB-INF/applicationContext.xml:





I did WEB-INF/applicationContext.xml in StrutsHibernateSpring:





I also added the Spring listener to the web.xml in StrutsHibernateSpring :

org.springframework.web.context.ContextLoaderListener

which is similar to the Struts2Example14 web.xml.

It worked.

Here is the code:


struts.xml
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">




listUser



/register.jsp





hibernate.config.xml

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">


org.hsqldb.jdbcDriver
jdbc:hsqldb:hsql://localhost
sa

1
org.hibernate.dialect.HSQLDialect
true
create





com.vaannila.dao
UserDAO.java
package com.vaannila.dao;

import java.util.List;
import com.vaannila.domain.User;

public interface UserDAO {

public void saveUser(User user);
public List listUser();
}

UserDAOImpl.java
package com.vaannila.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget;
import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget;
import com.vaannila.domain.User;

public class UserDAOImpl implements UserDAO {

@SessionTarget
Session session;
@TransactionTarget
Transaction transaction;

@SuppressWarnings("unchecked")
@Override
public List listUser() {
List courses = null;
try {
courses = session.createQuery("from User").list();
} catch (Exception e) {
e.printStackTrace();
}
return courses;
}

@Override
public void saveUser(User user) {
try {
session.save(user);
} catch (Exception e) {
transaction.rollback();
e.printStackTrace();
}
}

}

com.vaannila.domain
User.java
package com.vaannila.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="USER")
public class User {

private Long id;
private String name;
private String password;
private String gender;
private String country;
private String aboutYou;
private Boolean mailingList;

@Id
@GeneratedValue
@Column(name="USER_ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}

@Column(name="USER_NAME")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

@Column(name="USER_PASSWORD")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

@Column(name="USER_GENDER")
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}

@Column(name="USER_COUNTRY")
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}

@Column(name="USER_ABOUT_YOU")
public String getAboutYou() {
return aboutYou;
}
public void setAboutYou(String aboutYou) {
this.aboutYou = aboutYou;
}

@Column(name="USER_MAILING_LIST")
public Boolean getMailingList() {
return mailingList;
}
public void setMailingList(Boolean mailingList) {
this.mailingList = mailingList;
}

}

WebContent
WEB-INF
lib
application-Context.xml







web.xml


StrutsHibernateSpringARS

struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter



org.springframework.web.context.ContextLoaderListener


struts2
/*



index.jsp



WebContent
hs.bat
set classpath=.\WEB-INF\lib\hsqldb.jar;%classpath%
java org.hsqldb.Server
hs2.bat
java -cp ./WEB-INF/lib/hsqldb.jar org.hsqldb.util.DatabaseManager




I went one step further and I did a DAO dependency injection in my application StrHibSprARS2.
applicationContext.xml in src together with hibernate.cfg.xml and struts.xml









(It would be wiser to change the name of the applicationContext.xml to beans.xml.)
UserAction.java would change to:
...
//private UserDAO userDAO = new UserDAOImpl();

XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
private UserDAO userDAO = (UserDAOImpl) factory.getBean("userDAOImplClass");
...
(again the name applicationContext.xml gets mixed with the real one in the WEB-INF. I tried
successfully to clean up the unnecessary entries in these two applicationContext files, namely
I deleted id="userActionClass" item from the src beans.xml and vise versa. No problem.)