• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

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

October 19, 2010 //  by Krishna Srinivasan//  Leave a Comment

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:

  • Struts 2 Tutorials
  • Struts Interview Questions
  • Struts and Spring Integration

Syntax to use Struts LOGIC tag library

	&lt;%@ taglib prefix=&quot;logic&quot; uri=&quot;http://struts.apache.org/tags-logic&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.
index.jsp

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

&lt;jsp:forward page=&quot;logicmessage.do&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.

LogicMessageTag.jsp

&lt;%@page contentType=&quot;text/html&quot; pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;%@taglib  uri=&quot;http://struts.apache.org/tags-logic&quot; prefix=&quot;logic&quot; %&gt;
&lt;%@taglib uri=&quot;http://struts.apache.org/tags-bean&quot; prefix=&quot;bean&quot; %&gt;

&lt;html&gt;
    &lt;head&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;/head&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;/h4&gt;
&lt;/logic:messagesPresent&gt;
&lt;logic:messagesNotPresent message=&quot;true&quot;&gt;
    &lt;h4&gt;There are no errors messages on this page!&lt;/h4&gt;
&lt;/logic:messagesNotPresent&gt;

    &lt;/body&gt;
&lt;/html&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.

LogicMessageForm.java

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() {
        super();

    }

    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {

         ActionErrors errors = new ActionErrors();

	   errors.add(&quot;common.errors.text&quot;,
		new ActionMessage(&quot;common.errors.text&quot;));
        return errors;
    }
}

4.ApplicationResource.properties file contains the messages and their corresponding keys.

common.errors.text=Text Required

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

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;
          &quot;http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd&quot;&gt;

&lt;struts-config&gt;
    &lt; form-beans&gt;
        &lt; form-bean name=&quot;LogicMessageForm&quot; type=&quot;com.myapp.struts.LogicMessageForm&quot;/&gt;
    &lt;/form-beans&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;/action&gt;
&lt;/action-mappings&gt;
     &lt;;message-resources parameter=&quot;com/myapp/struts/ApplicationResource&quot;/&gt;;
	&lt;/struts-config&gt;

7.Building and running the application
Output

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

also read:

  • Struts 2.0 Introduction and Validations using Annotations
  • Introduction to Struts Actions
  • What’s new in Struts 2.0?
  • Internationalization and Taglibs in Struts 1.2

Category: StrutsTag: Struts Tags

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Struts LOGIC match and notMatch Tag ( < logic:match > and < logic:notMatch >)
Next Post: Domino 7 Lotus Notes Application Development »

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact