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
<%@ taglib prefix="logic" uri="http://struts.apache.org/tags-logic" %>
< logic:messagesPresent > and < logic:messagesNotPresent>
< logic:messagesPresent > -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.
< logic:messagesNotPresent > – 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 < logic:messagesPresent > and < logic:messagesNotPresent>
1.Create of Modify the index.jsp page which is the welcome page for the users.It forwards to the action class.
index.jsp
<%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%> <jsp:forward page="logicmessage.do"/>
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
<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %> <%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Struts Logic Message Present not Present Tag</title> </head> <body bgcolor="DDDDDD"> <h1>struts logic:messagesPresent and logic:messagesNotPresent tag</h1> <logic:messagesPresent > <h4>There are errors on this page! The error message is <bean:message key="common.errors.text"/> </h4> </logic:messagesPresent> <logic:messagesNotPresent message="true"> <h4>There are no errors messages on this page!</h4> </logic:messagesNotPresent> </body> </html>
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("common.errors.text", new ActionMessage("common.errors.text")); 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 = "success"; 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.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"> <struts-config> < form-beans> < form-bean name="LogicMessageForm" type="com.myapp.struts.LogicMessageForm"/> </form-beans> < action-mappings> < action input="/LogicMessageTag.jsp" name="LogicMessageForm" path="/logicmessage" scope="request" validate="true" type="com.myapp.struts.LogicMessageAction"> < forward name="success" path="/LogicMessageTag.jsp"/> </action> </action-mappings> <;message-resources parameter="com/myapp/struts/ApplicationResource"/>; </struts-config>
7.Building and running the application
Output
Access page:http://localhost:8080/logicmessage/
also read: