Struts <logic:match> and <logic:notMatch> Tag

The <logic:match> tag evaluates if the specified value is an appropriate substring of the requested variable. Whereas <logic:notMatch> tag evaluates if the specified value is not an appropriate substring of the requested variable. In order to use the Struts HTML Tags you need to include the following taglib directive in the jsp page.

<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> 

First create a new Dynamic Web Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.2</version>
      <scope>test</scope>
    </dependency>
  
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts-core</artifactId>
      <version>1.3.10</version>
    </dependency>
  
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts-taglib</artifactId>
      <version>1.3.10</version>
    </dependency>
  
</dependencies>

1. Create DynaActionForm

First we create an entry for DynaActionForm in struts-config.xml

<form-beans>
    <form-bean name="EmployeeForm" 
        type="org.apache.struts.action.DynaActionForm">
        <form-property name="employeeId" type="java.lang.Integer" />
         <form-property name="employeeName" type="java.lang.String" />
    </form-bean>
</form-beans>

2. Action Class (Controller)

Let us create an Action class that will handle the request. Here Action Class contains the business logic.

EmployeeAction.java

package com.kruders.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 org.apache.struts.action.DynaActionForm;
 
public class EmployeeAction extends Action{
    public ActionForward execute(ActionMapping mapping,ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
                throws Exception {
        DynaActionForm EmployeeForm = (DynaActionForm)form;
        EmployeeForm.set("employeeName","Tajinder Singh");
         
        return mapping.findForward("success");
    }
}

3. View

Create welcome.jsp and write the following code.

<%@taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<logic:forward name="checkMatch" />


Now create another jsp file checkMatch.jsp that contains the struts logic tag to check the specific value is appropriate substring of requested variable.

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
<title>Struts Logic Match Tag <logic:match> and Not Match Tag <logic:notMatch></title>
</head>
<body>
 
<b>Struts - Check value is appropriate substring of requested variable or not <logic:match> and <logic:notMatch></b><br>
"Tajinder" is a substring of "Tajinder Singh" - 
<logic:match name="EmployeeForm" property="employeeName" value="Tajinder">
Yes
</logic:match>
<logic:notMatch name="EmployeeForm" property="employeeName" value="Tajinder">
No
</logic:notMatch>
 
<br>"Sahil" is a substring of "Tajinder Singh" - 
<logic:match name="EmployeeForm" property="employeeName" value="Sahil">
Yes
</logic:match>
<logic:notMatch name="EmployeeForm" property="employeeName" value="Sahil">
No
</logic:notMatch>
<br><br>
<b>Struts - Use of location attribute in <logic:match> and <logic:notMatch></b>
<br>"Tajinder Singh" starts with "Taj" -
<logic:match name="EmployeeForm" property="employeeName" value="Taj" location="start">
Yes
</logic:match>
<logic:notMatch name="EmployeeForm" property="employeeName" value="Taj" location="start">
No
</logic:notMatch>
 
<br>"Tajinder Singh" starts with "der" -
<logic:match name="EmployeeForm" property="employeeName" value="der" location="start">
Yes
</logic:match>
<logic:notMatch name="EmployeeForm" property="employeeName" value="der" location="start">
No
</logic:notMatch>
 
<br>"Tajinder Singh" ends with "ngh" -
<logic:match name="EmployeeForm" property="employeeName" value="ngh" location="end">
Yes
</logic:match>
<logic:notMatch name="EmployeeForm" property="employeeName" value="ngh" location="end">
No
</logic:notMatch>
 
<br>"Tajinder Singh" ends with "ingh" -
<logic:match name="EmployeeForm" property="employeeName" value="ingh" location="end">
Yes
</logic:match>
<logic:notMatch name="EmployeeForm" property="employeeName" value="ingh" location="end">
No
</logic:notMatch>
 
</body>
</html>

4. Struts Config

Add the following code in 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>
    <form-beans>
        <form-bean name="EmployeeForm" 
            type="org.apache.struts.action.DynaActionForm">
            <form-property name="employeeId" type="java.lang.Integer" />
            <form-property name="employeeName" type="java.lang.String" />
        </form-bean>
    </form-beans>
    <global-forwards>
        <forward name="checkMatch" path="/checkMatch.do"/>
    </global-forwards>
       
    <action-mappings>
        <action input="/checkMatch.jsp" name="EmployeeForm" path="/checkMatch"  validate="true" scope="request" type="com.kruders.action.EmployeeAction">
            <forward name="success" path="/checkMatch.jsp" />
        </action>
    </action-mappings>
</struts-config>

5. Deployment Descriptor

Now configure the deployment descriptor. Here, we have asked the container to give our ActionServlet any request that matches the pattern *.do

Add the following configuration information in the web.xml file

<welcome-file-list>
    <welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>
 
<servlet>
    <servlet-class>
        org.apache.struts.action.ActionServlet
    </servlet-class>
    <init-param>
        <param-name>config</param-name>
        <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
  
<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

6. Run Project

Now when you run the project, following screen will be displayed as in Figure 36.1

Figure 36.1 Figure 36.1

The folder structure of the example is shown below in Figure 36.2

Figure 36.2 Figure 36.2

You can download the source code of this example here.

,

No comments yet.

Leave a Reply