Logic Match Tag (...)

Match tag - We use this tag to evaluate the contents contained in the nested body parts of this tag if the specified value is an appropriate substring of the requested variable.

Logic Match Tag (...)

Logic Match Tag (<logic:match >...</logic:match >) 

     

match tag - We use this tag to evaluate the contents contained in the nested body parts of this tag if the specified value is an appropriate substring of the requested variable.

This tag matches the variable specified as a String against the specified constant value. If the value is a substring  then the nested body content of this tag is evaluated.

Attributes of match Tag

Attribute Name Description
cookie

The variable to be matched is the value of the cookie whose name is specified by this attribute.

header

The variable to be matched is the value of the header whose name is specified by this attribute. The name match is performed in a case insensitive manner.

location

If not specified, a match between the variable and the value may occur at any position within the variable string. If specified, the match must occur at the specified location (either start or end) of the variable string.

name

The variable to be matched is the JSP bean specified by this attribute, if property is not specified, or the value of the specified property of this bean, if property is specified.

parameter

The variable to be matched is the first, or only, value of the request parameter specified by this attribute.

property

The variable to be matched is the property (of the bean specified by the name attribute) specified by this attribute. The property reference can be simple, nested, and/or indexed.

scope

The bean scope within which to search for the bean named by the name property, or "any scope" if not specified.

value

The constant value which is checked for existence as a substring of the specified variable.

 

Logic notMatch  Tag (<logic:notMatch  >...</logic:notMatch  >) 

notMatch - We use this tag to evaluate the contents contained in the nested body parts of this tag if the specified value is not a substring of the requested variable.

This tag matches the variable specified by one of  attributes (as a String) against the specified constant value. If the value is not a substring  the nested body content of this tag is evaluated.


Attributes of notMatch  Tag

Attribute Name Description
cookie

The variable to be matched is the value of the cookie whose name is specified by this attribute.

header

The variable to be matched is the value of the header whose name is specified by this attribute. The name match is performed in a case insensitive manner.

location

If not specified, a match between the variable and the value may occur at any position within the variable string. If specified, the match must occur at the specified location (either start or end) of the variable string.

name

The variable to be matched is the JSP bean specified by this attribute, if property is not specified, or the value of the specified property of this bean, if property is specified.

parameter

The variable to be matched is the first, or only, value of the request parameter specified by this attribute.

property

The variable to be matched is the property (of the bean specified by the name attribute) specified by this attribute. The property reference can be simple, nested, and/or indexed.

scope

The bean scope within which to search for the bean named by the name property, or "any scope" if not specified.

value

The constant value which is checked for existence as a substring of the specified variable.

 

Example Illustrating the use of the Match <logic:match > and the notMatch  <logic:notMatch  > logic tags.

Here you will learn to use the Struts  Logic tags. We will cover an example that will show  a comparison between  the two logic tags (ie..<logic:match > and the <logic:notMatch>).

Example code

Creating an Action  Class

Develop a simple action class LogicAction.java.

package roseindia.net;

import java.io.*;
import java.util.*;

/**

* @author Amit Gupta
* @Web http://www.roseindia.net
* @Email [email protected]

**/
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import org.apache.struts.action.*;

public class LogicAction extends Action 
{
  public ActionForward execute(ActionMapping mapping,
  ActionForm form,
  HttpServletRequest request,
  HttpServletResponse response)
  throws IOException, ServletException {

  

  return mapping.findForward("success");
  }
}

Creating Form Bean

Our form bean class contains only one property text. Here is the code of FormBean (LogicForm.java)

package roseindia.net;

import org.apache.struts.action.*;

/**

* @author Amit Gupta
* @Web http://www.roseindia.net
* @Email [email protected]

**/

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class EmptyForm extends ActionForm 
{
  
  private String text = "";
 
  public String getText() 
  {
  return text;
  }
  
  public void setText(String text) 
  {
  this.text=text;
  }
 
  
  
  
}

Defining form Bean in struts-config.xml file 0

Add the following entry in the struts-config.xml file for defining the form bean

<form-bean name="LogicForm"  type="roseindia.net.LogicForm" />

 Developing the Action Mapping in the struts-config.xml

Here, Action mapping helps to select the method from the Action class for specific requests.

<action path="/LogicAction"
type="roseindia.net.LogicAction"
name="LogicForm"
scope="request"
input="/pages/InputLogic.jsp">
<forward name="success" path="/pages/output.jsp"/>
</action>

Developing the InputLogic.jsp page 1

<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>

<html:html>
<head>
<title>Using &lt;logic&gt; Tags</title>
</head>

<body>
<h1>Using &lt;logic&gt; Tags</h1>

<html:form action="/LogicAction" method ="post">

<h2>Enter text:</h2>
<html:text property="text"/>

<br>
<h4>The &lt;logic:match&gt;tag works if the entered </h4>
<h4>text contains "amit"</h4>
<h4>The &lt;logic:notMatch&gt;tag always evaluate the </h4>
<h4>entered text against the"abc" value</h4>
<br>

<html:submit value="Submit"/>


<html:cancel/>
</html:form>
</body>
</html:html>

Developing the output.jsp page

Notice the values

<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>

<HTML>
<HEAD>
<TITLE>Here's Your Data...</TITLE>
</HEAD>

<BODY>
<H4>Here's Your Data...</H4>

<h4>The text entered is:

<bean:write name="LogicForm" property="text"/></h4>


<logic:match name="LogicForm" property="text" value="amit">
<h5>Using the tag &lt;logic:match &gt; </h5> 
Result : entered text contains "amit"
</logic:match>



<logic:notMatch name="LogicForm" property="text" value="abc">
<h5>Using the tag&lt;logic:notMatch&gt;</h5> 
Result: entered text does not contains "abc"
</logic:notMatch>

</BODY>
</HTML>

Add the following line in the index.jsp to call the form. 2

<li>
<html:link page="/pages/InputLogic.jsp">Struts File Upload</html:link>
<br>
Example demonstrates  how LogicAction Class works.
</li>

Building and Testing the Example 

To build and deploy the application go to Struts\Strutstutorial directory and type ant on the command prompt. This will deploy the application. Open the browser and navigate to the InputLogic.jsp page. Your browser displays the following  page. 3

Writing text including "amit" and excluding "abc" to the InputLogic.jsp page displays the working of  the  match Logic tag <logic:match > and the notMatch Logic tag <logic:notMatch > .

It displays the following to the out.jsp page

Writing text including "amit" and  "abc" to the InputLogic.jsp page displays the working of  the  match Logic tag <logic:match > and the notMatch Logic tag <logic:notMatch > . 4

The <logic:notEqual > evaluated here displays the output.jsp as