Download this example – Struts-Logic-Empty-NotEmpty-Example.zip

The Struts <logic:empty> is execute only if the specified property is null, zero-length String or doesn’t exists; While the Struts <logic:notEmpty> is doing the opposite ways. If the condition is matched, the body of the tag will be execute.

Here’s the example to show the use of the Struts <logic:empty> & <logic:notEmpty>, and test with the following three lists.

  1. listMsg0 – A list contains values.
  2. listMsg1 – An empty list.
  3. listMsg2 – A list which is doesn’t exists

LogicExampleAction.java

package com.mkyong.common.action;
 
import java.util.ArrayList;
import java.util.List;
 
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 {
 
		//listMsg0 - A list contains values
		List<String> listMsg0 = new ArrayList<String>();
 
		listMsg0.add("Message A");
		listMsg0.add("Message B");
		listMsg0.add("Message C");
		listMsg0.add("Message D");
 
		request.setAttribute("listMsg0", listMsg0);
 
		//listMsg1 - An empty list
		List<String> listMsg1 = new ArrayList<String>();
		request.setAttribute("listMsg1", listMsg1);
 
                //listMsg2 - A list which is doesn't exists
 
		return mapping.findForward("success");
	}
 
}

LogicExample.jsp

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@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 - Test &lt;logic:empty&gt;</h2>
 
<logic:empty name="listMsg0">
	<h3>listMag0 is empty</h3>
</logic:empty>
 
<logic:empty name="listMsg1">
	<h3>listMag1 is empty</h3>
</logic:empty>
 
<logic:empty name="listMsg2">
	<h3>listMag2 is empty</h3>
</logic:empty>
 
<h2>Struts - Test &lt;logic:notEmpty&gt;</h2>
 
<logic:notEmpty name="listMsg0">
	<h3>listMag0 is not empty</h3>
	<logic:iterate name="listMsg0" id="listMsgId">
		<p>
		List Messages 0 - <bean:write name="listMsgId"/>
		</p>
	</logic:iterate>
 
</logic:notEmpty>
 
<logic:notEmpty name="listMsg1">
	<h3>listMag1 is not empty</h3>
	<logic:iterate name="listMsg1" id="listMsgId">
		<p>
		List Messages 1 - <bean:write name="listMsgId"/>
		</p>
	</logic:iterate>
</logic:notEmpty>
 
<logic:notEmpty name="listMsg2">
	<h3>listMag2 is not empty</h3>
	<logic:iterate name="listMsg2" id="listMsgId">
		<p>
		List Messages 2 - <bean:write name="listMsgId"/>
		</p>
	</logic:iterate>
</logic:notEmpty>
 
</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>
  <action-mappings>
 
	 <action
		path="/LogicTest"
		type="com.mkyong.common.action.LogicExampleAction">
 
		<forward name="success" path="/pages/LogicExample.jsp"/>
 
	</action>
 
  </action-mappings>
</struts-config>

Result

http://localhost:8080/StrutsExample/LogicTest.do

Struts-logic-empty-notempty-example

In Struts – Test <logic:empty>, only listMsg1 and listMsg2 are displayed, this is because listMag1 is an empty list, while listMag2 doesn’t exists at all.

In Struts – Test <logic:notEmpty>, only listMsg0 is displayed, because this is the only list contains values.

Any Java questions or problems? please post at this JavaNullPointer.com forum, see you there ~
[ Read More ] You can find more similar articles at Struts 1.x Tutorials