Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Struts
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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.

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

4 comments so far (post your own) View All Comments Latest 10 Comments:

I cont get clear idea abt struts....Need more expls....

Posted by RAmesh on Monday, 02.18.08 @ 16:49pm | #48886

Hi,
Everything is fine except one thng, I would loike to ask that where should i store the following files(path)

SearchTutorialActionForm.java
SearchTutorialAction.java

Posted by vijay on Thursday, 12.20.07 @ 11:24am | #43075

Its a good search exaple

i need search example in very elabrately like

very huge amount of records and many condition

and its must very good access speed

if any one done it
plese mail me the coding

Posted by anbarasan on Wednesday, 06.20.07 @ 16:14pm | #19784

Its very good tutioral. I start always from here

Posted by jam on Tuesday, 03.13.07 @ 13:12pm | #11570

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.