Struts Hibernate Integration

In this tutorial I will show you how to integrate Struts and Hibernate. After completing this tutorial you will be able to use Hibernate in your Struts project. We will be using Hibernate Struts plug-in to write high performance application using Struts and Hibernate.


Hibernate is Object-Oriented mapping tool that maps the object view of data into relational database and provides efficient persistence services such as create, read, update and delete (CRUD).


In this tutorial we will implement small search engine application that shows a search form to the user. User enters search string and presses search button. Struts framework process the request and passes it to the action for further processing. Action class uses Hibernate to make database calls and retrieves matching records from database and results are displayed to the user.


Full example code is provided with the tutorial and you can download and start working on it for your project or to learn Struts and Hibernate Integration. You can download the source code from here.



  1. Setting up MySQL Database and table
    This section describes how to setup database and populate it with the data. We are using MySQL Database for this tutorial.
      
  2. Downloading Struts, Hibernate and Integrate It
    This section explains you to setup the develop workbench for our Struts Hibernate tutorial
      
  3. Writing Hibernate configuration file, POJO class and Tutorial.hbm.xml (Hibernate mapping class)
    In this section we will write required hibernate objects like configuration files and then integrate all the stuffs.
      
  4. Developing Hibernate Struts Plugin
    In this section we will write Hibernate Struts Plugin Java code and integrate it with the Struts.
     
  5. Writing Web Client to Search the database using Struts Hibernate Plugin
    In this section we will write web client to test struts Plugin. We will be developing a search form to search the tutorials from the table.
     
  6. Build and testing the application
    In this section we will build our Struts Hibernate Plugin Application and then test.

Struts 2 Tutorials

Struts 2 Tutorials



  1. Introduction to Struts 2
    Introduction to Struts 2 framework.

Struts Hibernate Integration Tutorial NEW
In this tutorial I will show you how to integrate Struts and Hibernate. After completing this tutorial you will be able to use Hibernate in your Struts project. Download the source code of Struts Hibernate Integration Tutorial.



  1. Struts Hibernate Integration Tutorial
    In this tutorial I will show you how to integrate Struts and Hibernate. After completing this tutorial you will be able to use Hibernate in your Struts project. We will be using Hibernate Struts plug-in to write high performance application using Struts and Hibernate
      
  2. Setting up MySQL Database and table
    This section describes how to setup database and populate it with the data. We are using MySQL Database for this tutorial.
      
  3. Downloading Struts, Hibernate and Integrate It
    This section explains you to setup the develop workbench for our Struts Hibernate tutorial
      
  4. Writing Hibernate configuration file, POJO class and Tutorial.hbm.xml (Hibernate mapping class)
    In this section we will write required hibernate objects like configuration files and then integrate all the stuffs.
      
  5. Developing Hibernate Struts Plugin
    In this section we will write Hibernate Struts Plugin Java code and integrate it with the Struts.
     
  6. Writing Web Client to Search the database using Struts Hibernate Plugin
    In this section we will write web client to test struts Plugin. We will be developing a search form to search the tutorials from the table.
     
  7. Build and testing the application
    In this section we will build our Struts Hibernate Plugin Application and then test.

Logic Present Tag ()

present tag -This tag evaluates its nested body contents if the specified value is present in the request.


This tag checks the current request and depending on which attribute is specified, it evaluates the nested body content of this tag only if the specified value is present. 


Attributes of Present Tag































Attribute Name Description
cookie

It checks  the existence of a cookie with the specified name.

header

It checks the existence of an HTTP header with the specified name. The name match is performed in a case insensitive manner.

name

It checks the existence of a JSP bean, in any scope, with the specified name. If property is also specified, checks for a non-null property value for the specified property.

parameter

It checks the existence of at least one occurrence of the specified request parameter on this request, even if the parameter value is a zero-length string.

property

It checks the existence of a non-null property value, returned by a property getter method on the JSP bean (in any scope) that is specified by the name attribute. Property references can be simple, nested, and/or indexed.

role

It checks whether the currently authenticated user (if any) has been associated with any of the specified security roles. Use a comma-delimited list to check for multiple roles. 

scope

The bean scope within which to search for the bean named by the name property, or “any scope” if not specified.

user

It checks whether the currently authenticated user principal has the specified name.


 


Logic notPresent Tag (<logic:not Present>…</logic:not Present>) 


notPresent tag -This tag evaluates its nested body content if the specified value is not present in the request.


Tag evaluation of the nested body content occurs only if the specified value is not available in the request i.e… works in reverses to present tag.

Attributes of notPresent  Tag































Attribute Name Description
cookie

It checks  the existence of a cookie with the specified name.

header

It checks the existence of an HTTP header with the specified name. The name match is performed in a case insensitive manner.

name

It checks the existence of a JSP bean, in any scope, with the specified name. If property is also specified, checks for a non-null property value for the specified property.

parameter

It checks the existence of at least one occurrence of the specified request parameter on this request, even if the parameter value is a zero-length string.

property

It checks the existence of a non-null property value, returned by a property getter method on the JSP bean (in any scope) that is specified by the name attribute. Property references can be simple, nested, and/or indexed.

role

It checks whether the currently authenticated user (if any) has been associated with the specified security role.

scope

The bean scope within which to search for the bean named by the name property, or “any scope” if not specified.

user

It checks whether the currently authenticated user principal has the specified name.


 


 


Example Illustrating the use of the present <logic:present > and the notPresent <logic:notPresent > logic tags.


Here you will learn to use the Struts  Logic tags. We will cover an example that will show  a comparison between  the two logic tags (ie..<logic:present > and the <logic:notPresent >).


Example code


Creating an Action  Class


Develop a simple action class LogicAction.java.











package roseindia.net;

import java.io.*;
import java.util.*;

/**

* @author Amit Gupta
* @Web http://www.roseindia.net
* @Email struts@roseindia.net

**/
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import org.apache.struts.action.*;

public class LogicAction extends Action 
{
  public ActionForward execute(ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws IOException, ServletException {

      

        return mapping.findForward(“success”);
    }
}


Creating Form Bean


Our form bean class contains only one property text. Here is the code of FormBean (LogicForm.java)











package roseindia.net;

import org.apache.struts.action.*;

/**

* @author Amit Gupta
* @Web http://www.roseindia.net
* @Email struts@roseindia.net

**/

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

public class EmptyForm extends ActionForm 
{
    
    private String text = “”;
       
  public String getText() 
    {
        return text;
    }
    
    public void setText(String text) 
    {
    this.text=text;
  }
   
    
  
    
}


Defining form Bean in struts-config.xml file


Add the following entry in the struts-config.xml file for defining the form bean






<form-bean name=”LogicForm”  type=”roseindia.net.LogicForm” />

 Developing the Action Mapping in the struts-config.xml

Here, Action mapping helps to select the method from the Action class for specific requests.






<action path=”/LogicAction”
type=”roseindia.net.LogicAction”
name=”LogicForm”
scope=”request”
input=”/pages/InputLogic.jsp”>
<forward name=”success” path=”/pages/output.jsp”/>
</action>

Developing the InputLogic.jsp page






<%@ taglib uri=”/tags/struts-html” prefix=”html” %>
<%@ taglib uri=”/tags/struts-logic” prefix=”logic” %>
<%@ taglib uri=”/tags/struts-bean” prefix=”bean” %>
<html:html>
<head>
<title>Using &lt;logic&gt; Tags</title>
</head>

<body>
<h1>Using &lt;logic&gt; Tags</h1>

<html:form action=”/LogicAction” method =”post”>

<h2>Enter text :</h2>
<html:text property=”text”/>
<html:submit value=”Submit”/> 
<html:cancel/>
</html:form>
</body>
</html:html>


Developing the output.jsp page:


Notice the property values






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

<HTML>
<HEAD>
<TITLE>Here’s Your Data…</TITLE>
</HEAD>
<BODY>
<H4>Here’s Your Data…</H4>

<h4>The text entered is:

<bean:write name=”LogicForm1″ property=”text”/></h4>

<logic:present name=”LogicForm1″ property=”text” >
<h5>Using the tag &lt;logic:present &gt; </h5> 
Result: Successfully supported 
</logic:present>

<logic:notPresent name=”LogicForm1″ property=”text1″ >
<h5>Using the tag&lt;logic:notPresent&gt;</h5> 
Result: Support for a Variable other than a String is not Present </logic:notPresent>


</BODY>
</HTML>


Add the following line in the index.jsp to call the form.


<li>
<html:link page=”/pages/InputLogic.jsp”>Struts File Upload</html:link>
<br>
Example demonstrates  how LogicAction Class works.
</li>


Building and Testing the Example 


To build and deploy the application go to Struts\Strutstutorial directory and type ant on the command prompt. This will deploy the application. Open the browser and navigate to the InputLogic.jsp page. Your browser displays the following  page.


Writing  a text string to the InputLogic.jsp page lets you to see the working of  the  present Logic tag <logic:present > and the notPresent Logic tag <logic:notPresent >. 







It displays the following out.jsp page







Logic Match Tag ()

match tag – We use this tag to evaluate the contents contained in the nested body parts of this tag if the specified value is an appropriate substring of the requested variable.


This tag matches the variable specified as a String against the specified constant value. If the value is a substring  then the nested body content of this tag is evaluated.


Attributes of match Tag































Attribute Name Description
cookie

The variable to be matched is the value of the cookie whose name is specified by this attribute.

header

The variable to be matched is the value of the header whose name is specified by this attribute. The name match is performed in a case insensitive manner.

location

If not specified, a match between the variable and the value may occur at any position within the variable string. If specified, the match must occur at the specified location (either start or end) of the variable string.

name

The variable to be matched is the JSP bean specified by this attribute, if property is not specified, or the value of the specified property of this bean, if property is specified.

parameter

The variable to be matched is the first, or only, value of the request parameter specified by this attribute.

property

The variable to be matched is the property (of the bean specified by the name attribute) specified by this attribute. The property reference can be simple, nested, and/or indexed.

scope

The bean scope within which to search for the bean named by the name property, or “any scope” if not specified.

value

The constant value which is checked for existence as a substring of the specified variable.


 


Logic notMatch  Tag (<logic:notMatch  >…</logic:notMatch  >) 


notMatchWe use this tag to evaluate the contents contained in the nested body parts of this tag if the specified value is not a substring of the requested variable.


This tag matches the variable specified by one of  attributes (as a String) against the specified constant value. If the value is not a substring  the nested body content of this tag is evaluated.



Attributes of notMatch  Tag































Attribute Name Description
cookie

The variable to be matched is the value of the cookie whose name is specified by this attribute.

header

The variable to be matched is the value of the header whose name is specified by this attribute. The name match is performed in a case insensitive manner.

location

If not specified, a match between the variable and the value may occur at any position within the variable string. If specified, the match must occur at the specified location (either start or end) of the variable string.

name

The variable to be matched is the JSP bean specified by this attribute, if property is not specified, or the value of the specified property of this bean, if property is specified.

parameter

The variable to be matched is the first, or only, value of the request parameter specified by this attribute.

property

The variable to be matched is the property (of the bean specified by the name attribute) specified by this attribute. The property reference can be simple, nested, and/or indexed.

scope

The bean scope within which to search for the bean named by the name property, or “any scope” if not specified.

value

The constant value which is checked for existence as a substring of the specified variable.


 


Example Illustrating the use of the Match <logic:match > and the notMatch  <logic:notMatch  > logic tags.


Here you will learn to use the Struts  Logic tags. We will cover an example that will show  a comparison between  the two logic tags (ie..<logic:match > and the <logic:notMatch>).


Example code


Creating an Action  Class


Develop a simple action class LogicAction.java.











package roseindia.net;

import java.io.*;
import java.util.*;

/**

* @author Amit Gupta
* @Web http://www.roseindia.net
* @Email struts@roseindia.net

**/
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import org.apache.struts.action.*;

public class LogicAction extends Action 
{
  public ActionForward execute(ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws IOException, ServletException {

      

        return mapping.findForward(“success”);
    }
}


Creating Form Bean


Our form bean class contains only one property text. Here is the code of FormBean (LogicForm.java)











package roseindia.net;

import org.apache.struts.action.*;

/**

* @author Amit Gupta
* @Web http://www.roseindia.net
* @Email struts@roseindia.net

**/

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

public class EmptyForm extends ActionForm 
{
    
    private String text = “”;
       
  public String getText() 
    {
        return text;
    }
    
    public void setText(String text) 
    {
    this.text=text;
  }
   
    
  
    
}


Defining form Bean in struts-config.xml file


Add the following entry in the struts-config.xml file for defining the form bean






<form-bean name=”LogicForm”  type=”roseindia.net.LogicForm” />

 Developing the Action Mapping in the struts-config.xml

Here, Action mapping helps to select the method from the Action class for specific requests.






<action path=”/LogicAction”
type=”roseindia.net.LogicAction”
name=”LogicForm”
scope=”request”
input=”/pages/InputLogic.jsp”>
<forward name=”success” path=”/pages/output.jsp”/>
</action>

Developing the InputLogic.jsp page






<%@ taglib uri=”/tags/struts-html” prefix=”html” %>
<%@ taglib uri=”/tags/struts-logic” prefix=”logic” %>

<html:html>
<head>
<title>Using &lt;logic&gt; Tags</title>
</head>

<body>
<h1>Using &lt;logic&gt; Tags</h1>

<html:form action=”/LogicAction” method =”post”>

<h2>Enter text:</h2>
<html:text property=”text”/>

<br>
<h4>The &lt;logic:match&gt;tag works if the entered </h4>
<h4>text contains “amit”</h4>
<h4>The &lt;logic:notMatch&gt;tag always evaluate the </h4>
<h4>entered text against the”abc” value</h4>
<br>

<html:submit value=”Submit”/>


<html:cancel/>
</html:form>
</body>
</html:html>

Developing the output.jsp page


Notice the values






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

<HTML>
<HEAD>
<TITLE>Here’s Your Data…</TITLE>
</HEAD>

<BODY>
<H4>Here’s Your Data…</H4>

<h4>The text entered is:

<bean:write name=”LogicForm” property=”text”/></h4>


<logic:match name=”LogicForm” property=”text” value=”amit”>
<h5>Using the tag &lt;logic:match &gt; </h5> 
Result : entered text contains “amit”
</logic:match>



<logic:notMatch name=”LogicForm” property=”text” value=”abc”>
<h5>Using the tag&lt;logic:notMatch&gt;</h5> 
Result: entered text does not contains “abc”
</logic:notMatch>

</BODY>
</HTML>

Add the following line in the index.jsp to call the form.


<li>
<html:link page=”/pages/InputLogic.jsp”>Struts File Upload</html:link>
<br>
Example demonstrates  how LogicAction Class works.
</li>


Building and Testing the Example 


To build and deploy the application go to Struts\Strutstutorial directory and type ant on the command prompt. This will deploy the application. Open the browser and navigate to the InputLogic.jsp page. Your browser displays the following  page.


Writing text including “amit” and excluding “abc” to the InputLogic.jsp page displays the working of  the  match Logic tag <logic:match > and the notMatch Logic tag <logic:notMatch > .







It displays the following to the out.jsp page







Writing text including “amit” and  “abc” to the InputLogic.jsp page displays the working of  the  match Logic tag <logic:match > and the notMatch Logic tag <logic:notMatch > .







The <logic:notEqual > evaluated here displays the output.jsp as







Logic LessEqual Tag ()

lessEqual Tag – If the requested variable is either less or equal to the specified values then we use this tag to evaluate the contents contained in the nested body parts of this tag.


This tag compares the variable against the specified constant value. If the variable is less than or equal to the specified value then the nested body contents of this tag is evaluated


Attributes of lessEqual Tag



























Attribute Name Description
cookie

The variable contains  the cookie value needed to compare .Here the name is specified by this attribute.

header

The variable contains the header value needed to compare whose name is specified by this attribute. The name match is performed in a case insensitive manner.

name

The variable to be compared is the JSP bean specified by this attribute, if property is not specified, or the value of the specified property of this bean, if property is specified.

parameter

The variable to be compared is the first, or only, value of the request parameter specified by this attribute.

property

The variable contains  the bean property value needed to compare ,it is specified by this attribute. The property reference can be simple, nested, and/or indexed.

scope

This specifies the bean scope within which  the bean is searched  by the name property, or “any scope” if not specified.

value

The constant value is specified by other attribute(s) of this tag, needed to compare


 


Logic LessThan Tag (<logic:lessThan >…</logic:lessThan>) 


lessThan TagIf the requested variable is less than the specified values then we use this tag to evaluate the contents contained in the nested body parts of this tag.


This tag compares the variable against the specified constant value. If the variable is less than  the specified value then the nested body contents of this tag is evaluated


Attributes of lessThan Tag



























Attribute Name Description
cookie

The variable contains  the cookie value needed to compare .Here the name is specified by this attribute.

header

The variable contains the header value needed to compare whose name is specified by this attribute. The name match is performed in a case insensitive manner.

name

The variable to be compared is the JSP bean specified by this attribute, if property is not specified, or the value of the specified property of this bean, if property is specified.

parameter

The variable to be compared is the first, or only, value of the request parameter specified by this attribute.

property

The variable contains  the bean property value needed to compare ,it is specified by this attribute. The property reference can be simple, nested, and/or indexed.

scope

This specifies the bean scope within which  the bean is searched  by the name property, or “any scope” if not specified.

value

The constant value is specified by other attribute(s) of this tag, needed to compare


Example Illustrating the use of the lessEqual <logic:LessEqual > and the lessThan <logic:LessThan > logic tags.


Here you will learn to use the Struts  Logic tags. We will cover an example that will show  a comparison between  the two logic tags (ie..<logic:lessEqual > and the <logic:LessThan >).


Example code


Creating an Action  Class


Develop a simple action class LogicAction.java.











package roseindia.net;

import java.io.*;
import java.util.*;

/**

* @author Amit Gupta
* @Web http://www.roseindia.net
* @Email struts@roseindia.net

**/
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import org.apache.struts.action.*;

public class LogicAction extends Action 
{
  public ActionForward execute(ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws IOException, ServletException {

      

        return mapping.findForward(“success”);
    }
}


Creating Form Bean


Our form bean class contains only one property number. Here is the code of FormBean (LogicForm.java)











package roseindia.net;

import org.apache.struts.action.*;

/**

* @author Amit Gupta
* @Web http://www.roseindia.net
* @Email struts@roseindia.net

**/

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

public class LogicForm extends ActionForm 
{
    
    private long number;        
  public 
long getNumber() 
    {
        return number;
    }
    
    public void setNumber(long number) 
    {
    this.number=number;
  }
   
    
  
    
}


Defining form Bean in struts-config.xml file


Add the following entry in the struts-config.xml file for defining the form bean






<form-bean name=”LogicForm”  type=”roseindia.net.LogicForm” />

 Developing the Action Mapping in the struts-config.xml

Here, Action mapping helps to select the method from the Action class for specific requests.






<action path=”/LogicAction”
type=”roseindia.net.LogicAction”
name=”LogicForm”
scope=”request”
input=”/pages/InputLogic.jsp”>
<forward name=”success” path=”/pages/output.jsp”/>
</action>

Developing the InputLogic.jsp page






<%@ taglib uri=”/tags/struts-html” prefix=”html” %>
<%@ taglib uri=”/tags/struts-logic” prefix=”logic” %>

<html:html>
<head>
<title>Using &lt;logic&gt; Tags</title>
</head>

<body>
<h1>Using &lt;logic&gt; Tags</h1>

<html:form action=”/LogicAction” method =”post”>

<h2>Enter a number:</h2>
<html:text property=”number”/>

<br>
<h3>The &lt;logic:lessEqual&gt;tag works if you enter a value</h3>
<h3>less than or equal to 100</h3>
<h3>The &lt;logic:lessThan&gt;tag works if you enter a value</h3>
<h3>less than to 10000</h3>
<br>

<html:submit value=”Submit”/>


<html:cancel/>
</html:form>
</body>
</html:html>

Developing the output.jsp page






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

<HTML>
<HEAD>
<TITLE>Here’s Your Data…</TITLE>
</HEAD>

<BODY>
<H3>Here’s Your Data…</H3>

<h4>The number entered is:

<bean:write name=”LogicForm” property=”number”/></h4>


<logic:lessEqual name=”LogicForm” property=”number” value=”100“>
<h4>Using the tag&lt;logic:lessEqual&gt;</h4> 
Result : lessEqual
</logic:lessEqual>


<logic:lessThan name=”LogicForm” property=”number” value=”10000“>
<h4>Using the tag &lt;logic:lessThan &gt; </h4> 
Result : lessThan
</logic:lessThan>


</BODY>
</HTML>
</HTML>

Add the following line in the index.jsp to call the form.


<li>
<html:link page=”/pages/InputLogic.jsp”>Struts File Upload</html:link>
<br>
Example demonstrates  how LogicAction Class works.
</li>


Building and Testing the Example 


To build and deploy the application go to Struts\Strutstutorial directory and type ant on the command prompt. This will deploy the application. Open the browser and navigate to the InputLogic.jsp page. Your browser displays the following  page.


Writing a number equal to 101 to the InputLogic.jsp page displays the working of  the  lessThan Logic tag <logic:lessThan > 







It displays the following out.jsp page







Now  write data equal 100  to the InputLogic.jsp to compare the working of  the lessEqual and lessThan tags







Here both tags are evaluated it displays the output.jsp as







Logic greaterEqual Tag (… )


empty tag – if the requested variable is greater than or equal to the specified value then this tag is used to evaluate the contents contained in the nested body parts of this tag.



This tag compares the variable against the specified constant value. If the variable is greater than or equal to the specified value then the nested body contents of this tag is evaluated


Attributes of greaterEqual Tag



























Attribute Name Description
cookie

The variable contains  the cookie value needed to compare .Here the name is specified by this attribute.

header

The variable contains the header value needed to compare whose name is specified by this attribute. The name match is performed in a case insensitive manner.

name

The variable to be compared is the JSP bean specified by this attribute, if property is not specified, or the value of the specified property of this bean, if property is specified.

parameter

The variable to be compared is the first, or only, value of the request parameter specified by this attribute.

property

The variable contains  the bean property value needed to compare ,it is specified by this attribute. The property reference can be simple, nested, and/or indexed.

scope

This specifies the bean scope within which  the bean is searched  by the name property, or “any scope” if not specified.

value

The constant value is specified by other attribute(s) of this tag, needed to compare


Logic greaterThan Tag (<logic: greaterThan>…
</ logic:greaterThan>) 


greaterThan Tag : Tag evaluation of the nested body content occurs only if the specified value is greater than the specified value.


This tag compares the variable against the specified constant value. If the variable is greater than  the specified value then the nested body contents of this tag is evaluated


Attributes of greaterThan Tag




























Attribute Name Description
cookie

The variable contains  the cookie value needed to compare .Here the name is specified by this attribute.

header

The variable contains the header value needed to compare whose name is specified by this attribute. The name match is performed in a case insensitive manner.

name

The variable to be compared is the JSP bean specified by this attribute, if property is not specified, or the value of the specified property of this bean, if property is specified.

parameter

The variable to be compared is the first, or only, value of the request parameter specified by this attribute.

property

The variable contains  the bean property value needed to compare ,it is specified by this attribute. The property reference can be simple, nested, and/or indexed.

scope

This specifies the bean scope within which  the bean is searched  by the name property, or “any scope” if not specified.

value

The constant value is specified by other attribute(s) of this tag, needed to compare


Example Illustrating the use of the Logic greaterEqual Tag<logic:greaterEqual> and the Logic greaterThan Tag<logic:greaterThan> .


Here you will learn to use the Struts  Logic tags. We will cover an example that will show  a comparison between  the two logic tags (ie..&lt$$logic:greaterEqual> and the <logic:greaterThan>).


Example code


Creating an Action  Class


Develop a simple action class LogicAction.java.











package roseindia.net;

import java.io.*;
import java.util.*;

/**

* @author Amit Gupta
* @Web http://www.roseindia.net
* @Email struts@roseindia.net

**/
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import org.apache.struts.action.*;

public class LogicAction extends Action 
{
  public ActionForward execute(ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws IOException, ServletException {

      

        return mapping.findForward(“success”);
    }
}


Creating Form Bean


Our form bean class contains only one property number. Here is the code of FormBean (LogicForm.java)











package roseindia.net;

import org.apache.struts.action.*;

/**

* @author Amit Gupta
* @Web http://www.roseindia.net
* @Email struts@roseindia.net

**/

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

public class LogicForm extends ActionForm 
{
    
    private long number;        
  public 
long getNumber() 
    {
        return number;
    }
    
    public void setNumber(long number) 
    {
    this.number=number;
  }
   
    
  
    
}


Defining form Bean in struts-config.xml file


Add the following entry in the struts-config.xml file for defining the form bean






<form-bean name=”LogicForm”  type=”roseindia.net.LogicForm” />

 Developing the Action Mapping in the struts-config.xml

Here, Action mapping helps to select the method from the Action class for specific requests.






<action path=”/LogicAction”
type=”roseindia.net.LogicAction”
name=”LogicForm”
scope=”request”
input=”/pages/InputLogic.jsp”>
<forward name=”success” path=”/pages/output.jsp”/>
</action>

Developing the InputLogic.jsp page






<%@ taglib uri=”/tags/struts-html” prefix=”html” %>
<%@ taglib uri=”/tags/struts-logic” prefix=”logic” %>

<html:html>
<head>
<title>Using &lt;logic&gt; Tags</title>
</head>

<body>
<h1>Using &lt;logic&gt; Tags</h1>

<html:form action=”/LogicAction” method =”post”>

<h2>Enter a number:</h2>
<html:text property=”number”/>

<br>
<h3>The &lt;logic:greaterEqual&gt;tag works if you enter a value</h3>
<h3>greater than or equal to 1</h3>
<h3>The &lt;logic:greaterThan&gt;tag works if you enter a value</h3>
<h3>greater than to 10000</h3>
<br>

<html:submit value=”Submit”/>


<html:cancel/>
</html:form>
</body>
</html:html>

Developing the output.jsp page






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

<HTML>
<HEAD>
<TITLE>Here’s Your Data…</TITLE>
</HEAD>

<BODY>
<H3>Here’s Your Data…</H3>

<h4>The number entered is:

<bean:write name=”LogicForm” property=”number”/></h4>


<logic:greaterEqual name=”LogicForm” property=”number” value=”1“>
<h4>Using the tag&lt;logic:greaterEqual&gt;</h4> 
Result : greaterEqual
</logic:greaterEqual>



<logic:greaterThan name=”LogicForm” property=”number” value=”10000“>
<h4>Using the tag &lt;logic:greaterThan &gt; </h4> 
Result : greaterThan
</logic:greaterThan>


</BODY>
</HTML>

Add the following line in the index.jsp to call the form.


<li>
<html:link page=”/pages/InputLogic.jsp”>Struts File Upload</html:link>
<br>
Example demonstrates  how LogicAction Class works.
</li>


Building and Testing the Example 


To build and deploy the application go to Struts\Strutstutorial directory and type ant on the command prompt. This will deploy the application. Open the browser and navigate to the InputLogic.jsp page. Your browser displays the following  page.


Entering a number greater than 1 and less than 10000  to the InputLogic.jsp page evaluates the working of the <logic:greaterEqual> tag







It displays the following out.jsp page







Writing a number greater than 10000 to the InputLogic.jsp displays







The output.jsp displays the working of the both tags.






Logic Equal Tag ()


equal tag – if the requested variable is equal to the specified value then this tag is used to evaluate the contents contained in the nested body parts of this tag.



Compares the variable passed against the specified constant value. The nested body content of this tag is evaluated if the variable and value are equal.




























Attribute Name Description
cookie

The variable contains  the cookie value needed to compare .Here the name is specified by this attribute.

header

The variable contains the header value needed to compare whose name is specified by this attribute. The name match is performed in a case insensitive manner.

name

The variable to be compared is the JSP bean specified by this attribute, if property is not specified, or the value of the specified property of this bean, if property is specified.

parameter

The variable to be compared is the first, or only, value of the request parameter specified by this attribute.

property

The variable contains  the bean property value needed to compare ,it is specified by this attribute. The property reference can be simple, nested, and/or indexed.

scope

This specifies the bean scope within which  the bean is searched  by the name property, or “any scope” if not specified.

value

The constant value is specified by other attribute(s) of this tag, needed to compare


Logic notEqual Tag (<logic:notEqual >…</logic:notEqual>) 


notEmptyif the requested variable is not equal to the specified value then this tag is used to evaluate the contents contained in the nested body parts of this tag.


Compares the variable passed against the specified constant value. The nested body content of this tag is evaluated if the variable and value are not equal.

Attributes of notEmpty Tag




























Attribute Name Description
cookie

The variable contains  the cookie value needed to compare .Here the name is specified by this attribute.

header

The variable contains the header value needed to compare whose name is specified by this attribute. The name match is performed in a case insensitive manner.

name

The variable to be compared is the JSP bean specified by this attribute, if property is not specified, or the value of the specified property of this bean, if property is specified.

parameter

The variable to be compared is the first, or only, value of the request parameter specified by this attribute.

property

The variable contains  the bean property value needed to compare ,it is specified by this attribute. The property reference can be simple, nested, and/or indexed.

scope

This specifies the bean scope within which  the bean is searched  by the name property, or “any scope” if not specified.

value

The constant value is specified by other attribute(s) of this tag, needed to compare


Example Illustrating the use of the Equal<logic:equal> and the notEqual <logic:notEqual> logic tags.


Here you will learn to use the Struts  Logic  Equal and notEqual tags. We will cover an example that will show  a comparison between  the two logic tags (ie..<logic:equal > and the <logic:notEqual >).


Example code


Creating an Action  Class


Develop a simple action class LogicAction.java.











package roseindia.net;

import java.io.*;
import java.util.*;

/**

* @author Amit Gupta
* @Web http://www.roseindia.net
* @Email struts@roseindia.net

**/
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import org.apache.struts.action.*;

public class LogicAction extends Action 
{
  public ActionForward execute(ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws IOException, ServletException {

      

        return mapping.findForward(“success”);
    }
}


Creating Form Bean


Our form bean class contains only one property number. Here is the code of FormBean (LogicForm.java)











package roseindia.net;

import org.apache.struts.action.*;

/**

* @author Amit Gupta
* @Web http://www.roseindia.net
* @Email struts@roseindia.net

**/

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

public class LogicForm extends ActionForm 
{
    
    private long number;        
  public long getNumber() 
    {
        return number;
    }
    
    public void setNumber(long number) 
    {
    this.number=number;
  }
   
    
  
    
}


Defining form Bean in struts-config.xml file


Add the following entry in the struts-config.xml file for defining the form bean






<form-bean name=”LogicForm”  type=”roseindia.net.LogicForm” />

 Developing the Action Mapping in the struts-config.xml

Here, Action mapping helps to select the method from the Action class for specific requests.






<action path=”/LogicAction”
type=”roseindia.net.LogicAction”
name=”LogicForm”
scope=”request”
input=”/pages/InputLogic.jsp”>
<forward name=”success” path=”/pages/output.jsp”/>
</action>

Developing the InputLogic.jsp page






<%@ taglib uri=”/tags/struts-html” prefix=”html” %>
<%@ taglib uri=”/tags/struts-logic” prefix=”logic” %>

<html:html>
<head>
<title>Using &lt;logic&gt; Tags</title>
</head>

<body>
<h1>Using &lt;logic&gt; Tags</h1>

<html:form action=”/LogicAction” method =”post”>

<h2>Enter a number:</h2>
<html:text property=”number”/>

<br>
<h3>The &lt;logic:Equal&gt;tag works if you enter 1</h3>

<h3>else The &lt;logic:notEqual&gt; is processed </h3>

<br>

<html:submit value=”Submit”/>


<html:cancel/>
</html:form>
</body>
</html:html>

Developing the output.jsp page






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

<HTML>
<HEAD>
<TITLE>Here’s Your Data…</TITLE>
</HEAD>

<BODY>
<H3>Here’s Your Data…</H3>

<h4>The number entered is:

<bean:write name=”LogicForm” property=”number”/></h4>

<logic:equal name=”LogicForm” property=”number” value=”1″>
<h4>Using the tag &lt;logic:equal &gt;</h4> 
Result : Equal
</logic:equal>

<logic:notEqual name=”LogicForm” property=”number” value=”1″>
<h4>Using the tag &lt;logic:notEqual &gt; </h4> 
Result : notEqual
</logic:notEqual>


</BODY>
</HTML>

Add the following line in the index.jsp to call the form.


<li>
<html:link page=”/pages/InputLogic.jsp”>Struts File Upload</html:link>
<br>
Example demonstrates  how LogicAction Class works.
</li>


Building and Testing the Example 


To build and deploy the application go to Struts\Strutstutorial directory and type ant on the command prompt. This will deploy the application. Open the browser and navigate to the InputLogic.jsp page. Your browser displays the following  page.


Writing value equal to1 to the InputLogic.jsp page displays the working of  the  Equal Logic tag <logic:equal > 







It displays the following out.jsp page







Now  write any data other than 1 to the InputLogic.jsp to see the working of  the notEqual logic tag <logic:notEqual>







The <logic:notEqual > evaluated here displays the output.jsp as






Logic Empty Tag ()

empty tag – If the requested variable is either null or an empty string then we use this tag to evaluate the contents contained in the nested body parts of this tag.


Tag evaluation of the nested body content occurs only if the specified value is either absent (i.e. null), an empty string (i.e. a java.lang.String with a length of zero), or an empty java.util.Collection or java.util.Map (tested by the isEmpty() method on the respective interface).


Attributes of Empty Tag

















Attribute Name Description
name

This attribute specifies a  JSP bean variable which is required to make comparison. 

property

The variable contains the property of the bean which is required to make comparison and is specified by the name attribute. 

scope

This specifies the bean scope within which  the bean is searched  by the name property, or “any scope” if not specified.


Logic notEmpty Tag (<logic:notEmpty>…</logic:notEmpty>) 


notEmptyIf the requested variable is neither null nor an empty string then we use this tag to evaluate the contents contained in the nested body parts of this tag.


Tag evaluation of the nested body content occurs only if the specified value is available (i.e.not null) and is not an empty string (i.e. a java.lang.String with a length of zero).

Attributes of notEmpty Tag

















Attribute Name Description
name

This attribute specifies a  JSP bean variable which is required to make comparison.  

property

The variable contains the property of the bean which is required to make comparison and is specified by the name attribute. 

scope

This specifies the bean scope within which  the bean is searched  by the name property, or “any scope” if not specified.


 


Example Illustrating the use of the Empty<logic:empty> and the notEmpty <logic:notEmpty> logic tags.


Here you will learn to use the Struts  Logic tags. We will cover an example that will show  a comparison between  the two logic tags (ie..<logic:empty> and the <logic:notEmpty>).


Example code


Creating an Action  Class


Develop a simple action class EmptyAction.java.











package roseindia.net;

import java.io.*;
import java.util.*;

/**

* @author Amit Gupta
* @Web http://www.roseindia.net
* @Email struts@roseindia.net

**/
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import org.apache.struts.action.*;

public class EmptyAction extends Action 
{
  public ActionForward execute(ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws IOException, ServletException {

      

        return mapping.findForward(“success”);
    }
}


Creating Form Bean


Our form bean class contains only one property text. Here is the code of FormBean (EmptyForm.java)











package roseindia.net;

import org.apache.struts.action.*;

/**

* @author Amit Gupta
* @Web http://www.roseindia.net
* @Email struts@roseindia.net

**/

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

public class EmptyForm extends ActionForm 
{
    
    private String text = “”;
       
  public String getText() 
    {
        return text;
    }
    
    public void setText(String text
    {
    this.text=text;
  }
   
    
  
    
}


Defining form Bean in struts-config.xml file


Add the following entry in the struts-config.xml file for defining the form bean






<form-bean name=”EmptyForm” type=”roseindia.net.EmptyForm”/>

 Developing the Action Mapping in the struts-config.xml

Here, Action mapping helps to select the method from the Action class for specific requests.






<action path=”/EmptyAction”
type=”roseindia.net.EmptyAction”
name=”EmptyForm”
scope=”request”
input=”/pages/input.jsp”>
<forward name=”success” path=”/pages/output.jsp”/>
</action>

Developing the input.jsp page






<%@ taglib uri=”/tags/struts-html” prefix=”html” %>
<%@ taglib uri=”/tags/struts-logic” prefix=”logic” %>

<html:html>
<head>
<title>Using &lt;logic&gt; Tags</title>
</head>

<body>
<h1>Using &lt;logic&gt; Tags</h1>

<html:form action=”/EmptyAction” method =”post”>

<h2>Enter your name:</h2>
<html:text property=”text”/>

<br>
<br>

<html:submit value=”Submit”/>


<html:cancel/>
</html:form>
</body>
</html:html>


Developing the output.jsp page






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

<HTML>
<HEAD>
<TITLE>Here’s Your Data…</TITLE>
</HEAD>

<BODY>
<H3>Here’s Your Data…</H3>

<h4>The text field text:</h4>

<bean:write name=”EmptyForm” property=”text”/>

<logic:notEmpty name=”EmptyForm” property=”text” scope=”request”>
<h4>Using the tag &lt;logic:notEmpty &gt; </h4> 
Results:not Empty
</logic:notEmpty>


<logic:empty name=”EmptyForm” property=”text” scope=”request”>
<h4>Using the tag&lt;logic:empty &gt;</h4> 
Results: Empty
</logic:empty>


</BODY>
</HTML>

Add the following line in the index.jsp to call the form.


<li>
<html:link page=”/pages/input.jsp”>Struts File Upload</html:link>
<br>
Example demonstrates  how EmptyAction Class works.
</li>


Building and Testing the Example 


To build and deploy the application go to Struts\Strutstutorial directory and type ant on the command prompt. This will deploy the application. Open the browser and navigate to the input.jsp page. Your browser displays the following  page.


Writing nothing to the out.jsp page displays the working of  the  Empty Logic tag <logic:empty > 







 Writing nothing displays the following out.jsp page







Now  write any data to the input.jsp to see the working of  the notEmpty logic tag <logic:notEmpty >







The <logic:notEmpty > evaluated here displays the output.jsp as







Struts Logic Tags: An Introduction

Struts logic tags are conditional tags that replaces scriptlets in the jsp files.


This tag library contains tags that do the following things :
    conditional generation of the output text,
    generate iterations to output the text,
    application flow management,
    matching strings and substrings…


Logic tags available in the Struts Framework


























































Tag Name Description
empty  If the requested variable is either null or an empty string then the nested body content of this tag is evaluated
equal  If the requested variable is equal to the specified value then the nested body content of this tag is evaluated
forward  It forwards the control to the page specified by the specified ActionForward entry.
greaterEqual If the value of the requested variable is greater than or equal to the specified value then the nested body content of this tag is evaluated
greaterThan  If the value of the requested variable is greater than the specified value then the nested body content of this tag is evaluated
iterate  It repeats the nested body content of this tag over a specified collection.
lessEqual  If the value of the requested variable is less than or equal to the specified value then the nested body content of this tag is evaluated
lessThan If the value of the requested variable is less than the specified value then the nested body content of this tag is evaluated
match Evaluates the nested body content of this tag if the specified value is an appropriate substring of the requested variable.
messagesNotPresent If the specified message is not present in the request then it generates the nested body content of this tag .
messagesPresent If the specified message is present in this request then it generates the nested body content of this tag .
notEmpty If the requested variable is neither null nor an empty string nor an empty java.util.Collection (tested by the isEmpty() method on the java.util.Collection interface) then the nested body content of this tag is evaluated
notEqual If the requested variable is not equal to the specified value then the nested body content of this tag is evaluated
notMatch If the specified value is not an appropriate substring of the requested variable then the nested body content of this tag is evaluated 
notPresent If the specified value is not present in the request then it generates the nested body content of this tag .
present If the specified value is present in the request then it generates the nested body content of this tag 
redirect It renders an HTTP Redirect request.

Struts logic tags uses equal, greaterEqual, greaterThan, lessEqual, lessThan, notEqual tags to compare the values. There are rules that must be  followed :



  • Initially the specified variable needed to be compared is examined (whether it can be converted to a double or to a long). Then a  numeric (either floating point or integer) type comparison is done. If numeric conversion fails than a String comparison is performed.
  • The variable to be compared is retrieved from the request attribute(s)  that may be a cookie, header, name, parameter or property available to the specified tag. This variable is then converted to the appropriate type needed for the comparison..
  • If the variable or property to be compared returns null, it is converted into a zero-length string before the comparison occurs.
  • If the comparison logic returns a true value then only the specific comparison is performed and subsequently the nested body content is evaluated .

Struts logic tags  can also do the substring matching using the tags (match, notMatch). There are rules that must be  followed :



  • The variable to be compared is retrieved from the request attribute(s)  that may be a cookie, header, name, parameter or property available on this tag. Then this variable is converted to a String, if necessary.
  • If the retrieval of the variable fails or it contains a null value then a request time exception is thrown.  
  • The existence of the variable as a substring is examined at the position specified by the location attribute Location can be specified at the beginning (if location is set to start), at the end (if location is set to end), or anywhere (if location is not specified).

A JspException is thrown at runtime if any tag is utilized incorrectly or any invalid combination of tag attributes are used. To process the actual exception that caused the problem,  a request attribute under key org.apache.struts.action.EXCEPTION  is passed to the error page.


In the subsequent section we will show you the examples of these tags.


Struts Logic Tags

Struts Logic Tags examples.



  1. Introduction to Struts Logic Tags
    Struts logic tags are conditional tags that replaces scriptlets in the jsp files.
     

  2. Logic Empty and notEmpty Tags
    Tag evaluation of the nested body content occurs only if the specified value is either absent (i.e. null), an empty string (i.e. a java.lang.String with a length of zero)
      

  3. Logic Equal and notEqual Tags
    if the requested variable is equal to the specified value then this tag is used to evaluate the contents contained in the nested body parts of this tag
      

  4. Logic greaterEqual and greaterThan Tags
    This tag compares the variable against the specified constant value. If the variable is greater than or equal to the specified value then the nested body contents of this tag is evaluated
      

  5. Logic lessEqual and lessThan Tags
    This tag compares the variable against the specified constant value. If the variable is less than or equal to the specified value then the nested body contents of this tag is evaluated
      

  6. Logic match and notMatch Tags
    This tag matches the variable specified as a String against the specified constant value. If the value is a substring  then the nested body content of this tag is evaluated.
      

  7. Logic present and notPresent Tags
    This tag checks the current request and depending on which attribute is specified, it evaluates the nested body content of this tag only if the specified value is present.