Struts <logic:present> and <logic:notPresent> Tag

The <logic:present> tag generates the nested body content of this tag if the specified value is present in this request. Whereas <logic:notPresent> tag generates the nested body content of this tag if the specified value is not present in this request. In order to use the Struts HTML Tags you need to include the following taglib directive in the jsp page.

<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> 

First create a new Dynamic Web Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml


1. Create DynaActionForm

First we create an entry for DynaActionForm in struts-config.xml

    <form-bean name="EmployeeForm" 
        <form-property name="employeeId" type="java.lang.Integer" />
         <form-property name="employeeName" type="java.lang.String" />

2. Action Class (Controller)

Let us create an Action class that will handle the request. Here Action Class contains the business logic.

package com.kruders.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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 org.apache.struts.action.DynaActionForm;
public class EmployeeAction extends Action{
    public ActionForward execute(ActionMapping mapping,ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
                throws Exception {
        DynaActionForm EmployeeForm = (DynaActionForm)form;
        EmployeeForm.set("employeeName","Puneet Aggarwal");
        return mapping.findForward("success");

3. View

Create welcome.jsp and write the following code.

<%@taglib uri="" prefix="logic"%>
<logic:forward name="checkPresent" />

Now create another jsp file checkPresent.jsp that contains the struts logic tag to check if the specified value is present in the request.

<%@taglib uri="" prefix="html"%>
<%@taglib uri="" prefix="logic"%>
<%@taglib uri="" prefix="bean"%>
<title>Struts Logic Present Tag <logic:present> and NotPresent Tag <logic:notPresent></title>
<b>Struts - Checks if the specified value is present in this request using <logic:present> and <logic:notPresent></b><br>
EmployeeForm object exists? - 
<logic:present name="EmployeeForm">
<logic:notPresent name="EmployeeForm">
<br>EmployeeForm1 object exists? - 
<logic:present name="EmployeeForm1">
<logic:notPresent name="EmployeeForm1">
<br>In EmployeeForm, employeeName property exists? - 
<logic:present name="EmployeeForm" property="employeeName">
<logic:notPresent name="EmployeeForm" property="employeeName">
<br>In EmployeeForm, employeeAge property exists? - 
<logic:present name="EmployeeForm" property="employeeAge">
<logic:notPresent name="EmployeeForm" property="employeeAge">

4. Struts Config

Add the following code in struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
        <form-bean name="EmployeeForm" 
            <form-property name="employeeId" type="java.lang.Integer" />
            <form-property name="employeeName" type="java.lang.String" />
        <forward name="checkPresent" path="/"/>
        <action input="/checkPresent.jsp" name="EmployeeForm" path="/checkPresent"  validate="true" scope="request" type="com.kruders.action.EmployeeAction">
            <forward name="success" path="/checkPresent.jsp" />

5. Deployment Descriptor

Now configure the deployment descriptor. Here, we have asked the container to give our ActionServlet any request that matches the pattern *.do

Add the following configuration information in the web.xml file


6. Run Project

Now when you run the project, following screen will be displayed as in Figure 38.1

Figure 38.1 Figure 38.1

The folder structure of the example is shown below in Figure 38.2

Figure 38.2 Figure 38.2

You can download the source code of this example here.


No comments yet.

Leave a Reply