[an error occurred while processing this directive]

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(); ) { roseindia.net.dao.hibernate.Tutorial tutorial = (roseindia.net.dao.hibernate.Tutorial)itr.next(); %> <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 (SearchTutorialActionForm.java)
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 (SearchTutorialAction.java)

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 roseindia.net.plugin.HibernatePlugIn;

import roseindia.net.dao.hibernate.Tutorial;

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.


[an error occurred while processing this directive]