Latest Tutorials| Questions and Answers|Ask Questions?|Site Map

Home Struts Struts-hibernate Developing Struts Web Module

Related Tutorials


Share on Google+Share on Google+

Developing Struts Web Module

In this we will be creating search interface for enabling the user to search tutorials.

Developing Struts Web Module


In this we will be creating search interface for enabling the user to search tutorials. This example is an client to test our Struts Hibernate Plugin.

The web component of the application consists of the following files:

1. Search Tutorial Form (SearchTutorial.jsp):

This file is used to display the search form to the user. Here is the code of search form:



<%@ taglib uri="/tags/struts-bean" prefix="bean" %>

   <%@ taglib uri="/tags/struts-html" prefix="html" %>
   <html:html locale="true">
   <title><bean:message key="welcome.title"/></title>
   <body bgcolor="white">

   <html:form action="/searchTutorial">



          <td align="right">
            Search Tutorial
          <td align="left">
            <html:text property=
"keyword" size="30" maxlength="30"/> </td> </tr>  <tr> <td align="right"> <html:submit>Search</html:submit> </td> </tr> </table> </html:form> </body> </html:html>

Save SearchTutorial.jsp in to "C:\Struts-Hibernate-Integration\code\pages" directory.

2. Search Result Page (SearchResultPage.jsp)
This page is used to display the search result. Here is the code of search result page:

        <%@page language="java" import="java.util.*"%>
	<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
        <%@ taglib uri="/tags/struts-html" prefix="html" %>
        <p><font size="4" color=
"#800000" face="Arial">Search Results</font></p> <% List searchresult =
(List) request.getAttribute("searchresult"); %> <% for
(Iterator itr=searchresult.iterator(); itr.hasNext(); ) { tutorial = (; %> <p><a href="<%=tutorial.getPageurl()%>"> <font face="Arial" size="3"><%=
tutorial.getShortdesc()%></font></a><br> <font face="Arial" size="2"><%=
tutorial.getLongdesc()%></font></p> <% } %> <html:link page="/pages/SearchTutorial.jsp">
Back to Search Page</html:link>

Save SearchResultPage.jsp in to "C:\Struts-Hibernate-Integration\code\pages" directory.

3. Search Java Form (
This is the Struts action form class. Here is the code of the Action Form:

package roseindia.web;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.*;

public class SearchTutorialActionForm extends ActionForm
  private String keyword=null;

public void setKeyword(String keyword){

  public String getKeyword(){
  return this.keyword;

 public void reset(ActionMapping
 mapping, HttpServletRequest request
) {
 public ActionErrors validate
  ActionMapping mapping, HttpServletRequest request ) {
  ActionErrors errors = new ActionErrors();
  ifgetKeyword() == null || getKeyword().length() ) {
new ActionMessage

  return errors;


 4. Search Action Class (

This is Struts Action Class of our application. Here is the code of the Action Class:

package roseindia.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;

import java.util.List;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;



import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.hibernate.Criteria;

public class SearchTutorialAction extends Action
  public ActionForward execute(
  ActionMapping mapping,
  ActionForm form,
  HttpServletRequest request,
  HttpServletResponse responsethrows Exception{
  SearchTutorialActionForm formObj =

 System.out.println("Getting session factory");
 /*Get the servlet context */
 ServletContext context =
 /*Retrieve Session Factory */
 SessionFactory _factory = (SessionFactory)  
 /*Open Hibernate Session */
 Session  session = _factory.openSession();
 //Criteria Query Example
 Criteria crit = session.createCriteria(Tutorial.class);
+ formObj.getKeyword
() +"%"))//Like condition

 //Fetch the result from database
 List tutorials= crit.list();
  /*Close session */
  System.out.println("Hibernate Session Closed");
  return mapping.findForward("success");

5. Entries into struts-config.xml

Add the following lines into your struts-config.xml file.

Form Bean: 

Action Entry:
<forward name="success" path="/pages/SearchResultPage.jsp"/>

Now we have created all the required stuffs for the web client. In the next section we will test our application.



If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 18, 2008

Related Tutorials

Discuss: Developing Struts Web Module   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
September 6, 2011

where is -->Tutorial class Criteria crit = session.createCriteria(Tutorial.class);