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


<bean:message key="welcome.title">


















Search Tutorial

"keyword" size="30" maxlength="30"/>

Search



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

"#800000" style="font-family:Arial;font-size:130%;">Search Results



<%
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();
%>

">
<%=
tutorial.getShortdesc()%>


<%=
tutorial.getLongdesc()%>



<%
}
%>

Back to Search Page


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();

if( getKeyword() == null || getKeyword().length() < 1 ) {
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 response) throws 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:
name="TutorialSearch"
type="roseindia.web.SearchTutorialActionForm">

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

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

0 comments:

Post a Comment