Logic Present 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.

Logic Present Tag (...)

Logic Present Tag (<logic:equal>...</logic:equal>) 

     

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. This variable is needed for comparison. 

 
property

The variable to be compared is the property of the bean. This is specified by the name attribute. The property reference can be simple, nested, and/or indexed.

 
scope

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

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

notEmpty - If 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. This variable is needed for comparison. 

 
property

The variable to be compared is the property of the bean. This is specified by the name attribute. The property reference can be simple, nested, and/or indexed.  

scope

The bean scope within which to search for the bean is named 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 LogicAction.java.

package roseindia.net;

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

/**

* @author Amit Gupta
* @Web http://www.roseindia.net
* @Email [email protected]

**/
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 [email protected]

**/

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

<%@ 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 nothing to the InputLogic.jsp page displays the working of  the  Empty Logic tag <logic:equal > 

Writing nothing displays the following out.jsp page

Now  write any data 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 0

Download example code

 

      1