Struts <logic:present> tag is used to check the given object or property is present or exists in the current request; while the <logic:notPresent> is doing the opposite way.

Here’s an example to show the use of the <logic:present> and <logic:notPresent>.

User.java – An user class contains a url property.

package com.mkyong.common;
 
public class User{
 
	String url;
 
	public String getUrl() {
		return url;
	}
 
	public void setUrl(String url) {
		this.url = url;
	} 
}

LogicExampleAction.java – Initial the User object and set the url property and store it into the request session.

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;
 
import com.mkyong.common.User;
 
public class LogicExampleAction extends Action{
 
	public ActionForward execute(ActionMapping mapping,ActionForm form,
		HttpServletRequest request,HttpServletResponse response) 
        throws Exception {
 
		User user = new User();
		user.setUrl("http://www.mkyong.com");
 
		request.setAttribute("user", user);
 
	    return mapping.findForward("success");
	}
 
}

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 - &lt;logic:present&gt; & &lt;logic:notPresent&gt;</h2>
 
<logic:present name="user">
	User object is exists.
</logic:present>
<logic:notPresent name="user">
	User object does not exists.
</logic:notPresent>
 
<br/></br/>
 
<logic:present name="abc">
	Abc object is exists.
</logic:present>
<logic:notPresent name="abc">
	Abc object does not exists.
</logic:notPresent>
 
<br/></br/>
 
<logic:present name="user" property="url">
	User object, url property is exists.
</logic:present>
<logic:notPresent name="user" property="url">
	User object, url property does not exists.
</logic:notPresent>
 
<br/></br/>
 
<logic:present name="user" property="email">
	User object, email property is exists.
</logic:present>
<logic:notPresent name="user" property="email">
	User object, email property does not exists.
</logic:notPresent>
 
 
</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-present-notpresent-example
Struts - <logic:present> & <logic:notPresent>
User object is exists.
 
Abc object does not exists.
 
User object, url property is exists.
 
User object, email property does not exists.
Note : You can find more similar articles at - Struts 1.x Tutorials