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.

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 (...)

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 and the 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.. and the ).

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 struts@roseindia.net

**/
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 struts@roseindia.net

**/

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

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

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.



Developing the InputLogic.jsp page

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



Using <logic> Tags



Using <logic> Tags





Enter text:






The <logic:match>tag works if the entered


text contains "amit"


The <logic:notMatch>tag always evaluate the


entered text against the"abc" value











Developing the output.jsp page

Notice the values

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



Here's Your Data...



Here's Your Data...



The text entered is:




match name="LogicForm" property="text" value="amit">
Using the tag <logic:match >

Result : entered text contains "amit"
match>



:notMatch name="LogicForm" property="text" value="abc">
Using the tag<logic:notMatch>

Result: entered text does not contains "abc"
notMatch>


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


  • Struts File Upload


    Example demonstrates how LogicAction Class works.

  • 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.

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

    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 and the notMatch Logic tag .

    The evaluated here displays the output.jsp as

    0 comments:

    Post a Comment