In this example you will see how to integrate Struts 2 and Hibernate using the "Full Hibernate Plugin 1.4 GA" ( Full Hibernate Plugin 1.4 GA ).
You will see how to add a user using the user registration form shown below and to list all the existing users.
To use the Full Hibernate Plugin 1.4 GA you need to add the following lib files to the lib directory.
01.
antlr-2.7.6.jar
02.
commons-collections-3.1.jar
03.
commons-fileupload-1.2.1.jar
04.
commons-io-1.3.2.jar
05.
commons-lang-2.3.jar
06.
commons-logging-1.1.jar
07.
dom4j-1.6.1.jar
08.
ejb3-persistence.jar
09.
freemarker-2.3.13.jar
10.
hibernate3.jar
11.
hibernate-annotations.jar
12.
hibernate-commons-annotations.jar
13.
hibernate-validator.jar
14.
hsqldb.jar
15.
javassist-3.9.0.GA.jar
16.
jta-1.1.jar
17.
junit-3.8.1.jar
18.
log4j-1.2.15.jar
19.
ognl-2.6.11.jar
20.
slf4j-api-1.5.8.jar
21.
slf4j-log4j12-1.5.8.jar
22.
struts2-convention-plugin-2.1.6.jar
23.
struts2-core-2.1.6.jar
24.
struts2-fullhibernatecore-plugin-1.4-GA.jar
25.
xwork-2.1.2.jar
The session object and transaction object will be injected using the @SessionTarget and@TransactionTarget annotation respectively.
01.
package
com.vaannila.dao;
02.
03.
import
java.util.List;
04.
05.
import
org.hibernate.Session;
06.
import
org.hibernate.Transaction;
07.
08.
import
com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget;
09.
import
com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget;
10.
import
com.vaannila.domain.User;
11.
12.
public
class
UserDAOImpl
implements
UserDAO {
13.
14.
@SessionTarget
15.
Session session;
16.
17.
@TransactionTarget
18.
Transaction transaction;
19.
20.
@SuppressWarnings
(
"unchecked"
)
21.
@Override
22.
public
List<User> listUser() {
23.
List<User> courses =
null
;
24.
try
{
25.
courses = session.createQuery(
"from User"
).list();
26.
}
catch
(Exception e) {
27.
e.printStackTrace();
28.
}
29.
return
courses;
30.
}
31.
32.
@Override
33.
public
void
saveUser(User user) {
34.
try
{
35.
session.save(user);
36.
}
catch
(Exception e) {
37.
transaction.rollback();
38.
e.printStackTrace();
39.
}
40.
}
41.
42.
}
For the session and transaction injection to happen throught the plug-in the org.hibernate. Session andorg.hibernate.Transaction objects should be declared as class variables and not at method level. You can keep these variables in a generic DAO class and extend all the other DAO's from it. When using this plug-in there is no need to explicitly commit the transaction and close the session, both will be done automatically.
The "transaction.rollback()" should be used only in the methods that updates the database.
In the hibernate configuration file, we configure it to work with the hsqldb database.
01.
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
02.
<!DOCTYPE hibernate-configuration PUBLIC
03.
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
05.
<
hibernate-configuration
>
06.
<
session-factory
>
07.
<
property
name
=
"hibernate.connection.driver_class"
> org.hsqldb.jdbcDriver </
property
>
08.
<
property
name
=
"hibernate.connection.url"
> jdbc:hsqldb:hsql://localhost </
property
>
09.
<
property
name
=
"hibernate.connection.username"
>sa</
property
>
10.
<
property
name
=
"connection.password"
></
property
>
11.
<
property
name
=
"connection.pool_size"
>1</
property
>
12.
<
property
name
=
"hibernate.dialect"
> org.hibernate.dialect.HSQLDialect </
property
>
13.
<
property
name
=
"show_sql"
>true</
property
>
14.
<
property
name
=
"hbm2ddl.auto"
>create</
property
>
15.
<
mapping
class
=
"com.vaannila.domain.User"
/>
16.
</
session-factory
>
17.
</
hibernate-configuration
>
The domain object User class is shown below.
01.
package
com.vaannila.domain;
02.
03.
import
javax.persistence.Column;
04.
import
javax.persistence.Entity;
05.
import
javax.persistence.GeneratedValue;
06.
import
javax.persistence.Id;
07.
import
javax.persistence.Table;
08.
09.
@Entity
10.
@Table
(name=
"USER"
)
11.
public
class
User {
12.
13.
private
Long id;
14.
private
String name;
15.
private
String password;
16.
private
String gender;
17.
private
String country;
18.
private
String aboutYou;
19.
private
Boolean mailingList;
20.
21.
@Id
22.
@GeneratedValue
23.
@Column
(name=
"USER_ID"
)
24.
public
Long getId() {
25.
return
id;
26.
}
27.
public
void
setId(Long id) {
28.
this
.id = id;
29.
}
30.
31.
@Column
(name=
"USER_NAME"
)
32.
public
String getName() {
33.
return
name;
34.
}
35.
public
void
setName(String name) {
36.
this
.name = name;
37.
}
38.
39.
@Column
(name=
"USER_PASSWORD"
)
40.
public
String getPassword() {
41.
return
password;
42.
}
43.
public
void
setPassword(String password) {
44.
this
.password = password;
45.
}
46.
47.
@Column
(name=
"USER_GENDER"
)
48.
public
String getGender() {
49.
return
gender;
50.
}
51.
public
void
setGender(String gender) {
52.
this
.gender = gender;
53.
}
54.
55.
@Column
(name=
"USER_COUNTRY"
)
56.
public
String getCountry() {
57.
return
country;
58.
}
59.
public
void
setCountry(String country) {
60.
this
.country = country;
61.
}
62.
63.
@Column
(name=
"USER_ABOUT_YOU"
)
64.
public
String getAboutYou() {
65.
return
aboutYou;
66.
}
67.
public
void
setAboutYou(String aboutYou) {
68.
this
.aboutYou = aboutYou;
69.
}
70.
71.
@Column
(name=
"USER_MAILING_LIST"
)
72.
public
Boolean getMailingList() {
73.
return
mailingList;
74.
}
75.
public
void
setMailingList(Boolean mailingList) {
76.
this
.mailingList = mailingList;
77.
}
78.
79.
}
In the UserAction class we have two methods add() and list() to add and list all users respectively.
01.
package
com.vaannila.web;
02.
03.
import
java.util.ArrayList;
04.
import
java.util.List;
05.
06.
import
com.opensymphony.xwork2.ActionSupport;
07.
import
com.opensymphony.xwork2.ModelDriven;
08.
import
com.vaannila.dao.UserDAO;
09.
import
com.vaannila.dao.UserDAOImpl;
10.
import
com.vaannila.domain.User;
11.
12.
public
class
UserAction
extends
ActionSupport
implements
ModelDriven<User> {
13.
14.
private
static
final
long
serialVersionUID = -6659925652584240539L;
15.
16.
private
User user =
new
User();
17.
private
List<User> userList =
new
ArrayList<User>();
18.
private
UserDAO userDAO =
new
UserDAOImpl();
19.
20.
@Override
21.
public
User getModel() {
22.
return
user;
23.
}
24.
25.
public
String add()
26.
{
27.
userDAO.saveUser(user);
28.
return
SUCCESS;
29.
}
30.
31.
public
String list()
32.
{
33.
userList = userDAO.listUser();
34.
return
SUCCESS;
35.
}
36.
37.
public
User getUser() {
38.
return
user;
39.
}
40.
41.
public
void
setUser(User user) {
42.
this
.user = user;
43.
}
44.
45.
public
List<User> getUserList() {
46.
return
userList;
47.
}
48.
49.
public
void
setUserList(List<User> userList) {
50.
this
.userList = userList;
51.
}
52.
53.
}
For the session and transaction injection to happen through the plug-in, the UserDAO should be a class level declaration and should not be declared at method level.
To use this plug-in you need to extend the package from hibernate-default package. To configure this you can either use XML or annotations, I am using XML.
01.
<!DOCTYPE struts PUBLIC
02.
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
04.
05.
<
struts
>
06.
<
package
name
=
"default"
extends
=
"hibernate-default"
>
07.
<
action
name
=
"addUser"
method
=
"add"
class
=
"com.vaannila.web.UserAction"
>
08.
<
result
name
=
"success"
type
=
"redirect"
>listUser</
result
>
09.
</
action
>
10.
<
action
name
=
"listUser"
method
=
"list"
class
=
"com.vaannila.web.UserAction"
>
11.
<
result
name
=
"success"
>/register.jsp</
result
>
12.
</
action
>
13.
</
package
>
14.
</
struts
>
The register.jsp page is shown below.
01.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
02.
pageEncoding="ISO-8859-1"%>
03.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
04.
<%@taglib uri="/struts-tags" prefix="s"%>
05.
<
html
>
06.
<
head
>
07.
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=ISO-8859-1"
>
08.
<
title
>Registration Page</
title
>
09.
<
s:head
/>
10.
<
style
type
=
"text/css"
>
11.
@import url(style.css);
12.
</
style
>
13.
</
head
>
14.
<
body
>
15.
<
s:form
action
=
"addUser"
>
16.
<
s:textfield
name
=
"name"
label
=
"User Name"
/>
17.
<
s:password
name
=
"password"
label
=
"Password"
/>
18.
<
s:radio
name
=
"gender"
label
=
"Gender"
list
=
"{'Male','Female'}"
/>
19.
<
s:select
name
=
"country"
list
=
"{'India','USA','UK'}"
headerKey
=
""
20.
headerValue
=
"Country"
label
=
"Select a country"
/>
21.
<
s:textarea
name
=
"aboutYou"
label
=
"About You"
/>
22.
<
s:checkbox
name
=
"mailingList"
23.
label
=
"Would you like to join our mailing list?"
/>
24.
<
s:submit
/>
25.
</
s:form
>
26.
27.
<
s:if
test="userList.size() > 0">
28.
<
div
class
=
"content"
>
29.
<
table
class
=
"userTable"
cellpadding
=
"5px"
>
30.
<
tr
class
=
"even"
>
31.
<
th
>Name</
th
>
32.
<
th
>Gender</
th
>
33.
<
th
>Country</
th
>
34.
<
th
>About You</
th
>
35.
<
th
>Mailing List</
th
>
36.
</
tr
>
37.
<
s:iterator
value
=
"userList"
status
=
"userStatus"
>
38.
<
tr
39.
class
=
"<s:if test="
#userStatus.odd == true ">odd</
s:if
><
s:else
>even</
s:else
>">
40.
<
td
><
s:property
value
=
"name"
/></
td
>
41.
<
td
><
s:property
value
=
"gender"
/></
td
>
42.
<
td
><
s:property
value
=
"country"
/></
td
>
43.
<
td
><
s:property
value
=
"aboutYou"
/></
td
>
44.
<
td
><
s:property
value
=
"mailingList"
/></
td
>
45.
</
tr
>
46.
</
s:iterator
>
47.
</
table
>
48.
</
div
>
49.
</
s:if
>
50.
</
body
>
51.
</
html
>
No comments:
Post a Comment