Struts – <logic:messagesPresent> <logic:messagesNotPresent> example
Struts <logic:messagesPresent> tag is used to check the given message or error message exists on the current request.
- “Messages” are ActionMessages , under the key Globals.MESSAGE_KEY in the current request.
- “Error Messages” are ActionErrors , under the key Globals.ERROR_KEY in the current request.
Here’s few examples to show the use of the <logic:messagesPresent> and <logic:messagesNotPresent>.
- If there is any error messages or messages exists under key “Globals.ERROR_KEY” or “Globals.MESSAGE_KEY”, the body of the tag will be execute.
<logic:messagesPresent> There are errors on this page! </logic:messagesPresent> <logic:messagesNotPresent> There are no errors on this page! </logic:messagesNotPresent>
- If there is any error messages or messages named “common.email.err” exists under key “Globals.ERROR_KEY”, the body of the tag will be execute.
<logic:messagesPresent property="common.email.err"> Email address has error messages! Globals.ERROR_KEY </logic:messagesPresent> <logic:messagesNotPresent property="common.email.err"> Email address has no error messages! - Globals.ERROR_KEY </logic:messagesNotPresent>
- If there is any error messages or messages named “common.email.err” exists under key “Globals.MESSAGE_KEY”, the body of the tag will be execute.
<logic:messagesPresent property="common.email.err" message="true"> Email address has error messages! - Globals.MESSAGE_KEY </logic:messagesPresent> <logic:messagesNotPresent property="common.email.err" message="true"> Email address has no error messages! - Globals.MESSAGE_KEY </logic:messagesNotPresent>
LogicExampleAction.java
package com.mkyong.common.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; public class LogicExampleAction extends Action{ public ActionForward execute(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception { //do nothing return mapping.findForward("success"); } }
EmailForm.java – ActionForm to return an error message – ActionErrors.
package com.mkyong.common.form; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessage; public class EmailForm extends ActionForm{ String email; public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { ActionErrors errors = new ActionErrors(); errors.add("common.email.err", new ActionMessage("error.common.email.required")); return errors; } }
Common.properties
#common module error message
error.common.email.required = Email is required.LogicExample.jsp
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <%@taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%> <html> <head> </head> <body> <h2>Struts - <logic:messagesPresent> & <logic:messagesNotPresent></h2> <logic:messagesPresent> There are errors on this page! </logic:messagesPresent> <logic:messagesNotPresent> There are no errors on this page! </logic:messagesNotPresent> <br/><br/> <logic:messagesPresent property="common.email.err"> Email address has error messages! Globals.ERROR_KEY </logic:messagesPresent> <logic:messagesNotPresent property="common.email.err"> Email address has no error messages! - Globals.ERROR_KEY </logic:messagesNotPresent> <br/><br/> <logic:messagesPresent property="common.email.err" message="true"> Email address has error messages! - Globals.MESSAGE_KEY </logic:messagesPresent> <logic:messagesNotPresent property="common.email.err" message="true"> Email address has no error messages! - Globals.MESSAGE_KEY </logic:messagesNotPresent> <br/><br/> </body> </html>
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans> <form-bean name="emailForm" type="com.mkyong.common.form.EmailForm"></form-bean> </form-beans> <action-mappings> <action path="/LogicTest" type="com.mkyong.common.action.LogicExampleAction" name="emailForm" validate="true" input="/pages/LogicExample.jsp" > <forward name="success" path="/pages/LogicExample.jsp"/> </action> </action-mappings> <message-resources parameter="com.mkyong.common.Common" /> </struts-config>
Result
http://localhost:8080/StrutsExample/LogicTest.do

Struts - <logic:messagesPresent> & <logic:messagesNotPresent> There are errors on this page! Email address has error messages! Globals.ERROR_KEY Email address has no error messages! - Globals.MESSAGE_KEY