Download this example – Struts-Logic-Match-NotMatch-Example.zip

Struts <logic:match> tag is used to check the given property contains the given value as substring. For example, a property result “Google Search Engine”, the value “gle” will match, while the value “ABC” will not match. If the condition is matched, the body of the tag will be execute. The Struts <logic:notMatch> is doing the opposite way.

The Struts’s match tag has a must-know attribute named “location“, with value either “start” or “end

  1. location = “start” – Only match if the given value is appear as the starting substring of the given property. E.g “Google Search Engine” – “Goog” will match, “gine” will not match.
  2. location = “end” – Only match if the given value is appear as the ending substring of the given property. E.g “Google Search Engine” – “Goog” will not match, “gine” will match.
  3. No location define – Match if the given value appear as the substring of the given property. E.g “Google Search Engine” – “Goog” will match, “gine” will match.

Here’s the example to show the use of the <logic:match> & <logic:notMatch>.

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 {
 
		request.setAttribute("email", "mkyong123456@yahoo.com");
 
		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:match&gt; & &lt;logic:notMatch&gt;</h2>
 
Email - mkyong123456@yahoo.com<br/><br/>
 
1. Is "yong" is a substring of the email? -
<logic:match name="email" value="yong">
	true
</logic:match>
<logic:notMatch name="email" value="yong">
	false
</logic:notMatch>
 
<br/><br/>
 
2. Is "yongABC" is a substring of the email? -
<logic:match name="email" value="yongABC">
	true
</logic:match>
<logic:notMatch name="email" value="yongABC">
	false
</logic:notMatch>
 
<br/><br/>
 
3. Is email start with "mkyong"? -
<logic:match name="email" value="mkyong" location="start">
	true
</logic:match>
<logic:notMatch name="email" value="mkyong" location="start">
	false
</logic:notMatch>
 
<br/><br/>
 
4.. Is email start with "yong"? -
<logic:match name="email" value="yong" location="start">
	true
</logic:match>
<logic:notMatch name="email" value="yong" location="start">
	false
</logic:notMatch>
 
<br/><br/>
 
5. Is email end with "com"? -
<logic:match name="email" value="com" location="end">
	true
</logic:match>
<logic:notMatch name="email" value="com" location="end">
	false
</logic:notMatch>
 
<br/><br/>
 
6. Is email end with "net"? -
<logic:match name="email" value="net" location="end">
	true
</logic:match>
<logic:notMatch name="email" value="net" location="end">
	false
</logic:notMatch>
 
 
</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-match-notmatch-example
Struts - <logic:match> & <logic:notMatch>
Email - mkyong123456@yahoo.com
 
1. Is "yong" is a substring of the email? - true
 
2. Is "yongABC" is a substring of the email? - false
 
3. Is email start with "mkyong"? - true
 
4.. Is email start with "yong"? - false
 
5. Is email end with "com"? - true
 
6. Is email end with "net"? - false
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