Struts LOGIC messagesPresent and messagesNotPresent Tag ( < logic:messagesPresent > and < logic:messagesNotPresent >)

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.

also read:

Syntax to use Struts LOGIC tag library

	&lt;%@ taglib prefix=&quot;logic&quot; uri=&quot;; %&gt;

&lt logic:messagesPresent &gt and &lt logic:messagesNotPresent&gt

&lt logic:messagesPresent &gt -This tag is used if an ActionMessages object, ActionErrors object, a String, or a String array is present in any scope the nested body content of this tag is evaluated.
&lt logic:messagesNotPresent &gt – This tag is used if an ActionMessages object, ActionErrors object, a String, or a String array is not present in any scope the nested body content of this tag is evaluated.
By default both the tags will retrieve the bean it will iterate over from the Globals.ERROR_KEY constant string, but if message attribute is set to ‘true’ the bean will be retrieved from the Globals.MESSAGE_KEY constant string.If message attribute is set to ‘true’, any value assigned to the name attribute will be ignored.

Example Code for &lt logic:messagesPresent &gt and &lt logic:messagesNotPresent&gt

1.Create of Modify the index.jsp page which is the welcome page for the users.It forwards to the action class.

&lt;%@page contentType=&quot;text/html&quot;%&gt;
&lt;%@page pageEncoding=&quot;UTF-8&quot;%&gt;

&lt;jsp:forward page=&quot;;/&gt;

2.Create an Jsp page and name it as LogicMessageTag.jsp.It is the output page for user which contains the logic tags to to check whether the page contains any error messages or not.


&lt;%@page contentType=&quot;text/html&quot; pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;%@taglib  uri=&quot;; prefix=&quot;logic&quot; %&gt;
&lt;%@taglib uri=&quot;; prefix=&quot;bean&quot; %&gt;

        &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
        &lt;title&gt;Struts Logic Message Present not Present Tag&lt;/title&gt;
    &lt;body bgcolor=&quot;DDDDDD&quot;&gt;
        &lt;h1&gt;struts logic:messagesPresent and logic:messagesNotPresent tag&lt;/h1&gt;
        &lt;logic:messagesPresent &gt;
	&lt;h4&gt;There are errors on this page!
    The error message is
    &lt;bean:message key=&quot;common.errors.text&quot;/&gt;
&lt;logic:messagesNotPresent message=&quot;true&quot;&gt;
    &lt;h4&gt;There are no errors messages on this page!&lt;/h4&gt;


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 one property of type String to hold the text.This form contains the validate method to add error messages to the request page.

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 LogicMessageForm extends org.apache.struts.action.ActionForm {

    private String text;

    public String getText() {
        return text;

    public void setText(String text) {
        this.text = text;

    public LogicMessageForm() {


    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {

         ActionErrors errors = new ActionErrors();

		new ActionMessage(&quot;common.errors.text&quot;));
        return errors;
} file contains the messages and their corresponding keys.

common.errors.text=Text Required

5.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 LogicMessageAction extends org.apache.struts.action.Action {

    private final static String SUCCESS = &quot;success&quot;;

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        return mapping.findForward(SUCCESS);

6.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.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;

&lt;!DOCTYPE struts-config PUBLIC
          &quot;-//Apache Software Foundation//DTD Struts Configuration 1.2//EN&quot;

    &lt; form-beans&gt;
        &lt; form-bean name=&quot;LogicMessageForm&quot; type=&quot;com.myapp.struts.LogicMessageForm&quot;/&gt;

&lt; action-mappings&gt;
        &lt; action input=&quot;/LogicMessageTag.jsp&quot; name=&quot;LogicMessageForm&quot; path=&quot;/logicmessage&quot; scope=&quot;request&quot; validate=&quot;true&quot; type=&quot;com.myapp.struts.LogicMessageAction&quot;&gt;
        &lt; forward name=&quot;success&quot; path=&quot;/LogicMessageTag.jsp&quot;/&gt;

     &lt;;message-resources parameter=&quot;com/myapp/struts/ApplicationResource&quot;/&gt;;

7.Building and running the application

Access page:http://localhost:8080/logicmessage/

also read:

Leave a Reply

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

Pin It on Pinterest

Share This

Share this post with your friends!