Using Struts Logic Tags That Match Substrings

This section provides information about the following tags:

  • <logic:match>— Evaluates its contents if the variable contains the value as a substring

  • <logic:notMatch>— Evaluates its contents if the variable doesn’t contain the value as a substring

This section uses the Logic Matching page at /StrutsTaglibs/LogicMatch.do. The rendered page is shown in Figure 14.3.

Figure 14.3. Logic Match page at /StrutsTaglibs/LogicMatch.do.


This page demonstrates a typical use of each of the tags. The source is shown in Listing 14.2.

Listing 14.2. JSP File Demonstrating Use of Struts Logic Tags Looking for Substrings (LogicMatch.jsp)
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ page import="java.util.Enumeration" %>
<html:html>
<head>
<title>Logic Match sample code</title>
</head>
<body bgcolor="white">

<h3>Logic Match sample code</h3>

<p>This page provides examples of the following Struts LOGIC tags:<br>
<ul>
<li>&lt;logic:match&gt;</li>
<li>&lt;logic:notMatch&gt;</li>
</ul>

<%--
Variables used on this page
--%>
<%
  request.setAttribute("authorName1", "James M. Turner");
request.setAttribute("authorName2", "Kevin Bedell");
%>
<%--
The following section shows match and notMatch.
--%>

<logic:match name="authorName1" scope="request" value="ames">
   <bean:write name="authorName1"/> has the string 'ames' in it.
</logic:match>
<logic:notMatch name="authorName1" scope="request" value="ames">
   <bean:write name="authorName1"/> doesn't have the string 'ames' in it.
</logic:notMatch>
<BR>
<logic:match name="authorName2" scope="request" value="ames">
   <bean:write name="authorName2"/> has the string 'ames' in it.
</logic:match>

<logic:notMatch name="authorName2" scope="request" value="ames">
   <bean:write name="authorName2"/> doesn't have the string 'ames' in it.
</logic:notMatch>
<BR>
<logic:match name="authorName1" scope="request" value="ames" location="start">
   <bean:write name="authorName1"/> starts with the string 'ames'.
</logic:match>
<logic:notMatch name="authorName1" scope="request" value="ames" location="start">
   <bean:write name="authorName1"/> doesn't start with the string 'ames'.
</logic:notMatch>
<BR>
<logic:match header="user-agent" value="Windows">
   You're running Windows
</logic:match>
<logic:notMatch header="user-agent" value="Windows">
   You're not running Windows
</logic:notMatch>
<BR>
</body>
</html:html>
				

The logic:match and logic:notMatch Tags

These tags should be familiar to anyone who has ever used the String methods indexOf, endsWith, and startsWith. As with the comparison tags, you can specify name, header, parameter, or cookie as the source of the value to compare. However, unlike the comparison tags, a missing or null value throws a runtime exception.

With no other arguments, match looks for the value attribute anywhere inside the string returned from the variable. If the location attribute is start, the variable must start with the value; if it’s end, the variable must end with the value.

This example also shows a nonbean lookup for the variable. The last two examples use the user-agent header value to check whether the client is running Microsoft Windows.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset