YOU ARE HERE: Home > Tech > J2EE > Article

Using Struts and Hibernate (Part II)
By Jack Xu This article was not rated yet.
 
Printer Version Printer Friendly | Add As Favorite | Link to Article

About the Author

Jack Xu has been worked in IT industry for 12 years. He has many interests in Internet and database development. He has developed Java based forums and search engines.

This article teaches how to use struts and hibernate framework in web applications. You will learn dynamic action forms and validation framework. You will use DynaValidatorForm, a subclass of DynaActionForm in this application.

You should have completed the article "Using Struts and Hibernate (Part I)", where we have explained configuration of hibernate and setup of struts. Instead of starting from scratch, you need modify the codes you have completed from that application.

First, you should have your MySQL database running just like in article "Using Struts and Hibernate (Part I)".

Assume TOMCAT_HOME is the folder C:\jakarta-tomcat-5, where Tomcat 5 has been installed. You should have the directory structure looks like:

You need copy hibernateapp folder and its contents, and paste it on webapps folder. You should change the folder "Copy of hibernateapp" by rename it to "hibernateapp2". Now you should have directory as


To use dynamic action forms, you don't need the action form class, so remove the class MyappForm.java and MyappForm.class from folder TOMCAT_HOME\webapps\hibernateapp2\com\usanalyst\struts


1. Struts configuration modification


You need to modify struts-config.xml file for using DynaValidatorForm.


<form-beans>
<form-bean name="registerForm"
type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="email" type="java.lang.String"/>
<form-property name="firstName" type="java.lang.String"/>
<form-property name="lastName" type="java.lang.String"/>
<form-property name="phone" type="java.lang.String"/>
</form-bean>
</form-beans>

Above is the only change you need to replace previous definition of . Save struts-config.xml


2. Refactor model and struts action

In class HbappModel.java, HashMap has been used to receive data from controler. For a better design, you should use an interface, namely java.util.Map. So we can redesign this class as

List 2.1 HbappModel.java

package com.usanalyst.struts;
import java.sql.*;
import java.util.Map;
import com.usanalyst.hibernate.*;
import org.hibernate.*;
import org.hibernate.cfg.*;
import java.util.Date;

public class HbappModel {

Map map=null;

public void setMap(Map map) {
this.map=map;
}

public void saveByHibernate() {


Session session1 = HibernateUtil.currentSession();
Transaction tx = session1.beginTransaction();

user_info user = new user_info();
user.setEmail((String)map.get("email"));
user.setFirstName((String)map.get("firstName"));
user.setLastName((String)map.get("lastName"));
user.setPhone((String)map.get("phone"));
user.setRequestTime(new Date());

session1.save(user);

tx.commit();
HibernateUtil.closeSession();

}
}

In addition, you must also change MyappAcion.java to use Map interface.

List 2.2 MyappAcion.java

package com.usanalyst.struts;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;

import org.apache.struts.util.MessageResources;
import org.apache.commons.beanutils.PropertyUtils;

import org.apache.struts.validator.DynaValidatorForm;

public final class MyappAction extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {

MyappAttributes mAttr=new MyappAttributes();


DynaActionForm myForm = (DynaActionForm)form;

Map map=myForm.getMap();

HbappModel mModel=new HbappModel();
mModel.setMap(map);
mModel.saveByHibernate();

mAttr.setStrSuccess("Your record has been saved.");
request.setAttribute("usanalyst.success",mAttr);

request.removeAttribute(mapping.getAttribute());

return (mapping.findForward("NotifySave"));
}

}

You must recompile the changed files before restart tomcat.


3. Tomcat Configuration


Since you are using JNDI for database connection in Hibernate, you need add to server.xml in node <Host>


<Context path="/hibernateapp2" docBase="hibernateapp2">

<Resource name="jdbc/getjobs" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="myguest" password="myapp0" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql:///myapp" />

</Context>

Usually, you also need to verify in TOMCAT_HOME\webapps\hibernateapp2\WEB-INF\web.xml, there is an element


List 3. JNDI resource reference


<resource-ref>
<res-ref-name>jdbc/getjobs</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

where jdbc/getjobs relates Resource element JNDI name in server.xml

If you copied web.xml from last article (TOMCAT_HOME\webapps\hibernateapp\WEB-INF\web.xml), you don't need change anything because elements in List 3 should be there.


4. Running the application


Restart Tomcat server and start your favorite browser. Type in

http://localhost:8081/hibernateapp2/myapp.jsp

(We have use port 8081 for http. You may have a different port, such as 8080 or 80).

If you type the inputs and click the Submit button, you should be able to save your inputs into the MySQL database.


Was this article helpful to you?yesno

Related Publications
 
Using iBATIS and struts
Using iBATIS in web applications
Using Struts and Hibernate (Part III)
Using Struts and Hibernate (Part II)
Using Struts and Hibernate (Part I)
Using Hibernate In J2EE Applications (Part I)
Effective Ant Build For Developers
Introduction To Struts By Example
How To Build Custom JSP Tags (Part II)
How To Build Custom JSP Tags (Part I)
Configuring SSL on BEA WebLogic Server 8.1

(Registered users can post questions/comments)

 
 TLINKS SEARCH
Advanced Search
Help
 Recommended Links
Red Cross
Responding to hurricane katrina relieve. Donate today. It's a Great Feeling to Help.
http://www.redcross.org
Getusjobs.com
Getusjobs.com is the job site focused on American jobs. See the results that put us on top.
http://www.getusjobs.com
Database Tool
TLinkSoft® tools empowers developers, integrators and DBAs to be more productive.
http://www.cppunit.org/download.jsp
USAnalyst.com
USAnalyst.com provide a community for database analysts, business analysts, developer analysts and managers.
http://www.cppunit.org/article

Powered by Tlinks Systems