Struts LOGIC present and notPresent Tag ( <logic:present> and <logic:notPresent>)

Struts LOGIC Tag Library

Struts LOGIC tag library provides tags that are useful in managing conditional generation of output text, looping over object collections for repetitive generation of output text, and application flow management.

Syntax to use Struts LOGIC tag library

	<%@ taglib prefix="logic" uri="" %>

logic:present and logic:notPresent

< logic:present &> -This tag checks in the current request depending upon the attribuite specified if the specified value is present the nested content of this tag is evaluated.
< logic:notPresent&> – This tag checks in the current request depending upon the attribuite specified if the specified value is not present the nested content of this tag is evaluated.

Example Code for < logic:present &> and < logic:notPresent

1.Create of Modify the index.jsp page which redirects to the action class to set the values.

< %@page contentType="text/html"%>
< %@page pageEncoding="UTF-8"%>
< jsp:forward page="">

2.Create an Jsp page and name it as LogicPresentTag.jsp.It is the output page for user which contains the logic tags to check the specified attribute in this current request and display the result.


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="html" uri="" %>
<%@taglib prefix="bean" uri="" %>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title> Struts Logic Present Tag< /title>
    <body bgcolor="DDDDDD">
        <h1> Struts logic:present and logic:notPresent tag< /h1>
        <logic:present property="name" name="LogicPresentForm">
<h4> Using &amplt;logic:present &amp>; tag < i> name</i> is
 < bean:write name="LogicPresentForm" property="name"/>
< /h4>
            <logic:notPresent name="LogicPresentForm" property ="number" >
<h4> Using &amplt; logic:notPresent&amp>; tag  <i>number</i> is not set < /h4>


3.Create a Form bean.Form bean is used to hold the properties of the submitted form which is a sub class of ActionForm.It contains only two properties of type int and String to hold the integer number and name which are set from the action class.

package com.myapp.struts;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class LogicPresentForm extends org.apache.struts.action.ActionForm {

    private String name;

    public String getName() {
        return name;

    public void setName(String string) {
        name = string;

    public LogicPresentForm() {



4.Simple Action class which is a sub class of Action class used to process the user’s request.

package com.myapp.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

public class LogicPresentAction extends org.apache.struts.action.Action {

    private final static String SUCCESS = "success";

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        LogicPresentForm formBean=(LogicPresentForm)form;
        formBean.setName("Java Struts");

        return mapping.findForward(SUCCESS);

5.Create or modify struts config file struts-config.xml with action mappings.Struts-config file contains the information about the configuration of the struts framework to the application.It contains the action mappings which helps to select Action,ActionForm and other information for specific user’s request’s.

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"

< struts-config>
    < form-beans>
        <form-bean name="LogicPresentForm" type="com.myapp.struts.LogicPresentForm"/>

< action-mappings>
        < action name="LogicPresentForm" path="/logicpresent" scope="session" type="com.myapp.struts.LogicPresentAction" validate="false">
        < forward name="success" path="/LogicPresentTag.jsp"/>



6.Building and running the application

Access page:http://localhost:8080/logicpresent/

also read:

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>