Download this example – Struts-Logic-Equal-NotEqual-Example.zip

The Struts <logic:equal> is used to check the given property is equal to a given value; While the Struts <logic:notEqual> is used to check the given property is not equal to a given value, 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:equal> & <logic:notEqual>.

User.java

package com.mkyong.common;
 
public class User{
 
	String email;
 
	public String getEmail() {
		return email;
	}
 
	public void setEmail(String email) {
		this.email = email;
	}
 
}

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;
 
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.setEmail("mkyong123456@yahoo.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 - Test &lt;logic:equal&gt;</h2>
 
<logic:equal name="user" property="email" value="mkyong123456@yahoo.com">
	<h3>user's email is equal</h3>
</logic:equal>
 
<h2>Struts - Test &lt;logic:notEqual&gt;</h2>
 
<logic:notEqual name="user" property="email" value="mkyong654321@yahoo.com">
	<h3>user's email is not equal</h3>
</logic:notEqual>
 
</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-equal-notequal-example
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