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

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 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 and the 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.. and the ).

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


Developing the Action Mapping in the struts-config.xml

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



Developing the input.jsp page

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



Using <logic> Tags



Using <logic> Tags





Enter your name:

















Developing the output.jsp page

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



Here's Your Data...



Here's Your Data...



The text field text:






Using the tag <logic:notEmpty >


Results:not Empty




Using the tag<logic:empty >


Results: Empty



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


  • Struts File Upload


    Example demonstrates how EmptyAction Class works.

  • 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

    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

    The notEmpty > evaluated here displays the output.jsp as

    0 comments:

    Post a Comment