Goal
The primary goal of this example is to create a Jakarta Struts based application that uses Hibernate to access a single table in an instance of a MySQL database. This example does not seek to explain the workings of the Struts and Hibernate applications, but rather to provide A Simple and Complete Example, Step-by-Step, Of How Such An Application May Be Built.
Description
The Application Will Provide The Following FunctionAlity:
The user will be presented with a web page listing all the items currently in the database. This listing will contain both the item's name and description. At the foot of the page, the user will be able to submit a new item, with its name and description. If the submitted new item has a non-empty name, the new item will be added to the database and the page will be redisplayed, with the new item in the list. If the submitted new item is invalid, the page will Be Returned with a list of errors Above the item List
Assumptions
In preparation for this first Example, I'll Be Making The Following Assumptions About How Your System Is Configured:
An installed copy of JDK 1.4 located on your system Apache Tomcat has been installed on your system When describing the location of a particular file, I will assume that the Tomcat installation is at:.. / Usr / local / tomcat, please replace this with the path appropriate on your system. In the example, I will refer to this directory as $ TOMCAT. Version 4.1.29 of Tomcat was used for this example. MySQL has been installed on your system. The example will use the test database. An account should be established on the MySQL server with select, insert, update and delete priviledges for use by the application. I will assume that this user has the name testuser with the password testuser. In addition, you should have available to you an account that will allow you to add tables to and drop tables from the database. A JDBC driver has been installed in your Tomcat installation under $ TOMCAT / common / lib. For this example, I used Connector / J 3.0.9. Apache Ant must be installed ON Your System in Orde R to build.xml.Please Also Note The FOLLOWING:
I have opted for a flat package structure, given that the example contains only a half-a-dozen or so classes. In a full-blown application, you would probably want to break the forms, actions, etc. into their own packages. This Application Will Send Unchecked Exceptions To The Servlet Engine, Ultimately Displaying A Stack Trace To The User. You Wouldn't Want That In a finished application.
Instructions
A Compressed Archive of All The Files Mentioned With INSTRUCTIONS IS AVAILABLE Here.
Struts preparation
Our first task will be to install Struts within our Tomcat installation. Struts provides you with a blank application as a great starting point. Starting with this, we will make some changes to the configuration files, removing anything unessential. We have chosen to identify the forms and actions needed by the application prior to their implementation.Download the Jakarta Struts framework from http://jakarta.apache.org/struts/. The version used for this example was 1.1. Create directory example1 under $ TOMCAT / webapps. Expand the Jakarta Struts tar file and copy the contents of the webapps directory to $ TOMCAT / webapps. Copy $ TOMCAT / webapps / struts-blank.war to $ TOMCAT / webapps / example1. Expand struts-blank.war using jar xvf struts-blank .war. This will give us a framework upon which to build our application. Modify the web application configuration file, web.xml, in $ TOMCAT / webapps / example1 / WEB-INF /. This file describes the configuration of the web application. Modify the struts configu ration file, struts-config.xml, in $ TOMCAT / webapps / example1 / WEB-INF /. This file contains information about the forms and action mappings that comprise the web application. We will be employing a single form, AddItemForm, that will Be buy to contain and validate the user's input.
form-beans>
Two action mappings will be defined. The first mapping, items, will be called as the intial entry point into the application. The second mapping, addItem, will be called when the user has submitted information to be validated from AddItem.jsp.
PATH = "/ items" Type = "org.apache.struts.Actions.ForwardAction" parameter = "/ pages / additem.jsp" /> Path = "/ additem" TYPE = "com.edhand.example1.additemaction" Name = "AddItemform" Scope = "request" Validate = "True" INPUT = "/ Pages / AddItem.jsp"> action> action-mappings> MySQL preparation We Well Be Using A Single MySQL Table To Store The Items for Use by The Application. Using Your Account with Create Privileges: Add The Following Table To The Test Database: Create Table `Item` `ID` Bigint (11) Not null auto_increment, `Name` VARCHAR (32) Not null default '', `description` longtext, PRIMARY Key (`ID) ) Hibernate Preparation Hibernate will be installed as a collection of jar files in your web application's / WEB-INF / lib directory. In addition to the jar files, we will need to create a properties file telling Hibernate how to connect to MySQL. Download Hibernate from http://www.hibernate.org/. The version used for this example is 2.0.3. From the root level of the expanded archive, copy hibernate2.jar into $ TOMCAT / webapps / example1 / WEB-INF / lib. Then, from the lib directory of the expanded hibernate tar file, copy * .jar into $ TOMCAT / webapps / example1 / WEB-INF / lib. Create Hibernate properties file, describing the connection to the MySQL database as well as setting several . Hibernate specific properties This file, hibernate.properties, should be located at $ TOMCAT / webapps / example1 / WEB-INF / src / java This section of the configuration file instructs Hibernate on how to connect to our database:. hibernate.dialect net .sf.hibernate.dialect.mysqldiaalectualibernate.Connection.driver_class org.gjt.mm.mysql.driver Hibernate.Connection.driver_class com.mysql.jdbc.driver Hibernate.Connection.URL JDBC: MySQL: /// TEST Hibernate.Connection.username Testuser Hibernate.Connection.Password Testuser Creating the item bean class The Item class will be the basic mechanism by which item objects will be pushed and pulled between Hibernate and our application. We will also need to provide a mapping that tells Hibernate how to connect our object to the table in MySQL Create directory / com / edhand / example1 underneath $ TOMCAT / webapps / example1 / WEB-INF / src / java. We will now create the java class to be used to store and retrieve information from the MySQL database using Hibernate. Create the java source file, Item.java, in $ TOMCAT / webapps / example1 / WEB-INF / src / java / com / edhand / example1. This Java class exposes three properties using the JavaBean standards. These properties are id, name, and description. Create the Hibernate mapping file, Item.hbm.xml, for item. The purpose of the file is to define the mapping between the Java bean Item and the item table in MySQL's test database. This file should be placed under $ TOMCAT / webapps / example1 / Web-INF / SRC / JAVA / COM / EDHAND / EXAMPLE1. id> clas> hibernate-maping> PLEASE NOTE THEN The Application IS Built, The Default Build.xml for Ant Will Copy This File INTO The $ TOMCAT / EXAMPLE1 / Web-INF / CLASS DIRECTORY TREE. Creating the itemservice and connectionfactory classes The ItemService class provides an interface to the Struts application allowing it to work with Item objects through the persistence layer. No Hibernate-related code need exist above this layer. This would allow a developer, in the future, to switch to some other method of object persistence by changing this class without affecting the Struts code.Create the java source file, ItemService.java, in $ TOMCAT / webapps / example1 / WEB-INF / src / java / com / edhand / example1. This Java class provides methods to access Item objects. These methods have been implemented using Hibernate-specific code. Create the java source file, ConnectionFactory.java, in $ TOMCAT / webapps / example1 / WEB-INF / src / java / com / edhand / example1. This Java class Provides ItemService with a hibernate session, When Requested. Creating The Additemform Class The AddItemForm class will be used to contain the information submitted from AddItem.jsp. The class also provides a validation method to ensure that the contents of the fields are acceptable before passing on the form to the action class, AddItemAction. In our case, this . Create the java source file, AddItemForm.java, in $ TOMCAT / webapps / example1 / WEB-INF / src / java / com / edhand / example1. This Java class encapsulates the data submitted by the user from AddItem.jsp, our Java Server Page. Creating The AddIction Class The AddItemAction class will be used to process the user's request and forward the response to the appropriate JSP. This class will actually connect with Hibernate, through ItemService and add the user's submitted item. Create the java source file, AddItemAction.java, in $ TOMCAT / webapps / example1 / WEB-INF / src / java / com / edhand / example1. This Java class processes the user's request and forwards the user to the appropriate response.Creating AddItem .jsp This is the user to subsser. Create the java server page, AddItem.jsp, in $ TOMCAT / webapps / example1 / pages /. For a thorough explanation of all the struts related tags, I suggest looking here. Look under the "Developer Guides" under the left column for individual Taglib families. <% @ Taglib URI = "/ Tags / Struts-bean" prefix = "bean"%> <% @ Taglib URI = "/ Tags / Struts-HTML" prefix = "html"%> <% @ Taglib Uri = "/ Tags / Struts-Logic" prefix = "logic"%> <% @ page import = "com.edHand.example1.ItemService"%> <% @ page import = "java.util.list"%>
hEAD>
<%
/ *
* This Code Will Generate A List of Objects from the
* Database and place a reason to this list in the
* Request Object.
*
* /
List itemlist = itemservice.getinstance (). GetItemlist ();
Request.setttribute ("items", itemlist);
%>
list of items in item code> Table of Database
test code>. p>