An entry struts + hibernate example

  Abstract: A + hibernate entry struts examples 

  </ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> This is my learning "Example # 1: Struts with Hibernate" notes and exercises, in the original http : / / homepage.mac.com/edahand/projects/java/example1.html 
  Note that this is not the integrity of the original translation, was at fault, please refer to the original. 
  (A) Target 
  The main objective of this example is based on the Jakarta Struts to create a simple WEB application, it uses Hibernate access to the MySQL database in a single table.    This example does not seek to explain Struts and Hibernate application of the principle, but step by step to provide a simple and complete example. 

  ————————————————– —————————– 

  (2) Description 
  This application provides the following functions: 

  1, through a web page lists all the database entries, and entries contain the entry of a name and description. 
  2, the bottom of the page, users can submit a new entry, with its name and description. 
  3, if the name of a non-empty, the new entries to the database and will be displayed again with a new entry page. 
  4, if submitted in error, will return a list of errors with the above page. 


  ————————————————– —————————— 

  (C) if 
  The preparatory work for this example, I will assume that your system configuration: 

  1, installed JDK 1.4. 
  2, installed above Apache Tomcat4.1.29 
  3, the MySQL database installation 
  4, JDBC driver has been installed.    I use the mysql-connector-java-3.0.14-production-bin.jar that can be copied to $ Tomcat / common / lib, or $ TOMCAT/webapps/example1/web-inf/lib mentioned below Below. 
  5, the installation Ant1.6 above. 

  ————————————————– —————————— 

  (D) Instructions 

  Following all the documents mentioned WEB applications and the entire directory structure please download this site. 

  (5) prepare Struts 
  1, http://jakarta.apache.org/struts/ downloaded from the Jakarta Struts, I used the jakarta-struts-1.1.zip. 

  2, in the $ TOMCAT / webapps directory created under example1 

  3, and Struts related jar files copied to $ TOMCAT/webapps/example1/WEB-INF/lib, tld files copied to $ TOMCAT/webapps/example1/WEB-INF under. 

  4, in the $ TOMCAT/webapps/example1/WEB-INF / web.xml file to create. 
  5, in the $ TOMCAT/webapps/example1/WEB-INF / created under the struts-config.xml file.    This document contains the forms and movements of mapping information, we will only use the form AddItemForm, which includes verification of the user input. 

<form-beans>
  <form-bean Name="addItemForm" type="com.edhand.example1.AddItemForm"/> 
  </ Form-beans> 

  Definition of the two action mappings. 


  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"> 





  (6) for MySQL 
  In the database test in the table to create the only item, 
  CREATE TABLE `item` ( 
  `Id` bigint (11) NOT NULL auto_increment, 
  `Name` varchar (32) NOT NULL default'' 
  “ Longtext description, 
  PRIMARY KEY ( `id`) 
)



  (7) prepared to Hibernate 
  1, http://www.hibernate.org/ downloaded from Hibernate, I use the hibernate-2.1.2.zip 
  2, the document will be available to extract and reproduce hibernate2.jar to $ TOMCAT/webapps/example1/WEB-INF/lib, from extracting documents found lib directory, copy *. jar to $ TOMCAT/webapps/example1/WEB -INF/lib. 
  3, Hibernate to create documents hibernate.properties attributes, and putting them under $ TOMCAT/webapps/example1/WEB-INF/src/java.    Below this piece on how to connect to our database: 

  Hibernate.dialect net.sf.hibernate.dialect.MySQLDialect 
  Hibernate.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 


  (8) Create Item bean 
  1, created under the $ TOMCAT/webapps/example1/WEB-INF/src/java/com/edhand/example1 Item.java source files, this class has three attributes: id, name and description. 
  2, created under the $ TOMCAT/webapps/example1/WEB-INF/src/java/com/edhand/example1 Item.hbm.xml Hibernate mapping file, 









  When we use Ant build.xml Construction Construction documents, copies of this document to $ TOMCAT/example1/WEB-INF/classes directory tree. 

  (9) the creation of ItemService and ConnectionFactory 
  1, in the $ TOMCAT/webapps/example1/WEB-INF/src/java/com/edhand/example1 directory create documents ItemService.java.    This method provides Java class visit Item objects. 
  2, in the $ TOMCAT/webapps/example1/WEB-INF/src/java/com/edhand/example1 directory create documents ConnectionFactory.java.    When a request that provides ItemService and a Hibernate session. 

  (10) the creation of AddItemForm 
  $ TOMCAT/webapps/example1/WEB-INF/src/java/com/edhand/example1 directory in the source file to create AddItemForm.java.    This class load AddItem.jsp pages users from the data submitted. 

  (11) the creation of AddItemAction 
  In the directory to create $ TOMCAT/webapps/example1/WEB-INF/src/java/com/edhand/example1 JAVA source files AddItemAction.java, AddItemAction this category will be dealt with a user's request and respond to the JSP pages.    In fact, this category by category and Hibernate ItemService connections, and increased user in the database submitted entries. 

  (12) Create AddItem.jsp 
  In the $ TOMCAT/webapps/example1/pages / directory created AddItem.jsp document. 
  <% @ 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>


  </ Head> 
  <body Bgcolor="white"> 


  Example 1 



<%
  / * 
  * This code will generate a list of objects from the 
  * Database and place a reference to this list in the 
  * Request object. 
*
  * / 
  List itemList = ItemService.getInstance (). GetItemList (); 
  Request.setAttribute (the "items" itemList); 
%>

  List of items in item table of database test. 


  <table Border=1> 
  Row for each item. 

<tr>
<td>   </ Td> 
<td>   </ Td> 
  </ Tr> 

  </ Table> 

  Sumbit to add an item: 



  To the addItem Action Mapping 

  <table Border=1> 
  <tr> <td> Name: </ td> <td>    </ Tr> 
  <td> Description: </ td> <td>    </ Td> </ tr> 
  </ Table> 



  </ Body> 



  (13) Using Ant Build 
  In order to compile this example in the source document can be used to provide the build.xml file, which will result in a War compressed files and help files. 

  1, in the $ TOMCAT/webapps/example1/WEB-INF/src directory created build.xml. 

  2, in the $ TOMCAT/webapps/example1/WEB-INF/src directory implementation ant. 

  (14) running this example 
  Restart Tomcat. 

  Enter in the browser following links: http://localhost:8080/example1/items.do. 
  Function TempSave (ElementID) (CommentsPersistDiv.setAttribute ( "CommentContent" document.getElementById (ElementID). Value); CommentsPersistDiv.save ( "CommentXMLStore");) function Restore (ElementID) (CommentsPersistDiv.load ( "CommentXMLStore"); document . getElementById (ElementID). CommentsPersistDiv.getAttribute value = ( "CommentContent");) </ td> </ tr> <tr> 

  ↑ Back 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags: ,

Releated Java Articles

Comments

Leave a Reply