October 21, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Developing J2EE Applications Using Hibernate Annotations and Spring MVC

  • January 12, 2006
  • By M. M. Islam Chisty
  • Send Email »
  • More Articles »

Create JSP pages

Here you will create a JSP page named "ViewAllMyObjects.jsp". After creation, place this under the 'springappwarjsp' directory and edit it as follows. This page will be used to display data only. Click here to download ViewAllMyObjects.jsp.

<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>

<html>
<head><title>ViewAllMyObjects</title></head>

<body bgcolor="#ffffff">
<h1>View All Objects</h1>

<table border="1" width="100%">
   <thead>
      <tr>
         <th>Id</th>
         <th>Name</th>
         <th>Address</th>
         <th>Email</th>
         <th>Phone</th>
      </tr>
   </thead>

   <c:forEach items="${myObjectModel}" var="mobj"
              varStatus="status">
      <tr>
         <td>
            <a href="MyObjectForm.html?id=${mobj.id}">
            ${mobj.id}</a>
         </td>
         <td> ${mobj.name}</td>
         <td> ${mobj.address}</td>
         <td> ${mobj.email}</td>
         <td> ${mobj.phone}</td>
      </tr>
   </c:forEach>

   <tr>
      <td colspan="5">
         <a href="MyObjectForm.html">
            Click here to add new Object
         </a>
      </td>
   </tr>
</table>

</body>
</html>

In the preceding example,

  • The ${myObjectModel} object is returned by the handleRequest() method of the MyObjectController object (the second parameter of the ModelAndView method)
  • The link to the MyObjectForm.html is the redirected URL, which actually refers to the MyObjectForm.jsp page

Now, create another JSP page named "MyObjectForm.jsp" and place it under the 'springappwarjsp' directory, or click here to get the code for MyObjectForm.jsp. The code snippet from this page should look like the following:

<%@ taglib uri="http://www.springframework.org/tags"
           prefix="Spring"%>

<html>

<body bgcolor="#ffffff">
<form method="post">
<table border="1">
   <tr>
<td>Id</td> <td> <Spring:bind path="myobject.id"> <input type="text" name="id" value="${status.value}" disabled="disabled"/> </Spring:bind> </td> </tr> <tr> <td>Name</td> <td> <Spring:bind path="myobject.name"> <input type="text" name="name" size="20" value="${status.value}"/> </Spring:bind> </td> </tr> ... ... ... </tr> <tr> <td><input type="submit" name="delete" value="Delete"/></td> <td><input type="submit" name="update" value="Update"/></td> </tr> <tr> <td colspan="2"><input type="submit" name="add" value="+ Add +"/> </td> </tr> </form> </table> </body> </html>

In the preceding code,

  • By using <%@taglib uri="http://www.springframework.org/tags" prefix="Spring" %>, the spring tag library properties are imported in your JSP page.
  • The <spring:bind> tag is used to bind an <input> form element to a command object MyObjectVO. The ${status.value} are special variables declared by the framework that can be used to display the current value of the field.

    (For details, you can check here: http://www.springframework.org/docs/MVC-step-by-step/Spring-MVC-step-by-step-Part-3.html.)

Also, create a file named test.html under the 'springappwarjsp' directory. Just keep it empty. This is not for real use; you will simply use it as a welcome file. If invoked from the URL as http://localhost:8080/war/test.html, this will simply be redirected to the MyObjectController object, which in turn, will return a list of MyObjectVOs, as explained earlier.

Configure web.xml

Create an web.xml file and place it under the 'WEB-INF' directory. Click here to download web.xml.

Within this file,

  • The servlet name "action" is used by the DispatcherServlet class. This means that it will look for a configuration file named <servlet-name>-servlet.xml. In this case, it would be action-servlet.xml.
  • The <url-pattern> is used as *.html, which means the application will go through the files with extension of type .html only.
  • You will need the spring.tld file for the Spring tag library definition. I've provided one here. Just place it under your springappwarWEB-INF directory.

Step 5: Build and Deploy the Application

In this step, you will build and deploy the application. The following steps describe this process for you.

1. Build the application.

To do this, go to the command prompt and type ant build. See Figure 6.

2. Deploy the application.

Use the command ant deploywar. See Figure 7.

3. Check your deployment.

Start tomcat, point to your browser address bar, and type http://localhost:8080/springapp/test.html. If you see something like Figure 8, everything is okay. You have not inserted any data yet; that's why your browser is displaying an empty list screen.

Just click on the "Click here to add new Object" link; a new empty form page will be displayed. Just insert some data and press "Add". See Figure 9.

Your populated screen will look something like Figure 10:

You can click any of the ID links and edit the corresponding object data.

Conclusion

This article has shown you how to apply the Hibernate annotations feature with Spring to develop J2EE-based applications. Hibernate annotation is built on top of the basic JDK5 annotations features, which do not directly affect the semantics of a program. Development and deployment tools can read these annotated codes during runtime and process them accordingly. This tutorial also described how to get Spring MVC to come into play. The objective is to spend less time on configuration and more focus on business logic rules. This article is Part II of a series on Java Annotations. In Part I, you were introduced with the JDK5 annotations features. In Part III, you will see a complex example that includes multiple database tables relationships.

Resources

  1. Source code accompanying this article.
  2. Hibernate Annotations: http://sourceforge.net/project/showfiles.php?group_id=40712&package_id=139933
  3. Hibernate3 and EJB3 at JBoss site: http://docs.jboss.org/ejb3/HibernateAnnotations/reference/en/html_single/
  4. JDK5: http://java.sun.com/developer/technicalArticles/J2SE/constraints/annotations.html
  5. Spring MVC: http://www.springframework.org/docs/MVC-step-by-step/Spring-MVC-step-by-step.html
  6. Jakarta Tomcat version 5.0.28: http://www.axint.net/apache/jakarta/tomcat-5/v5.0.28/bin/jakarta-tomcat-5.0.28.zip

About the Author

M. M. Islam Chisty currently works as a Software Engineer for M&H Informatics. His responsibilities include the development and re-engineering of Web-based applications using J2EE technologies. He has a BS in computer science from North South University. He loves spending free time doing R&D on the latest Java-based technologies, watching TV, and listening to music—not to mention that he is a great WWE fan too.





Page 4 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel