[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">
   <head>
   <title><bean:message key="welcome.title"/></title>
   <html:base/>
   </head>
   <body bgcolor="white">

   <html:form action="/searchTutorial">

   <html:errors/>

   <table>

     <tr>
          <td align="right">
            Search Tutorial
          </td>
          <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){
    this.keyword=keyword;
  }

  public String getKeyword(){
    return this.keyword;
  }

   public void reset(ActionMapping
 mapping, HttpServletRequest request
) {
    this.keyword=null;
    }
   public ActionErrors validate
      ActionMapping mapping, HttpServletRequest request ) {
      ActionErrors errors = new ActionErrors();
      
      ifgetKeyword() == null || getKeyword().length() ) {
        errors.add("keyword",
new ActionMessage
("error.keyword.required"));
     }

      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 =
 
(SearchTutorialActionForm)form;

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

   //Fetch the result from database
   List tutorials= crit.list();
   request.setAttribute("searchresult",tutorials);
   
    /*Close session */
      session.close();
    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: 
<form-bean
name="TutorialSearch"
type="roseindia.web.SearchTutorialActionForm">
</form-bean>

Action Entry:
<action
path="/searchTutorial"
type="roseindia.web.SearchTutorialAction"
name="TutorialSearch"
scope="request"
validate="true"
input="/pages/SearchTutorial.jsp">
<forward name="success" path="/pages/SearchResultPage.jsp"/>
</action>

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]