| This Chapter | |
| - | Chapter 9: The Logic Tag Library |
| - | The Value Comparison Tags |
| - | The Substring Matching Tags |
| - | The Presentation Location Tags |
| - | The Collection Utility Tag |
| - | Summary |
Substring matching tags can be used to verify if a value is an exact match of the specified value, or if it starts or ends with the specified value. Table 9.2 lists the Logic Tag Library’s tags that fall into this category.
| Tag | Description | JSTL Equivalent |
|---|---|---|
| match | Executes the tag’s body if a value is an exact match of the specified value, or if it starts or ends with the specified value. | c:if |
| notMatch | Executes the tag’s body if a value is not an exact match of the specified value and if it does not start or end with the specified value. | c:if |
Table 9.2: The substring matching tags
The value to be compared can be a cookie, a request header, a request parameter, a scoped object, or a scoped object’s property. Just like the value comparison tags, you need the following attributes.
Additionally, you can use the scope attribute when using match or notMatch to compare the value of a scoped object or a scoped object’s property, to specify the scope of the scoped object. Its value may be one of the following: page, request, session, application, or any scope.
In addition, the match and notMatch tags have the optional location attribute that specifies how the string comparison should be performed. This attribute may have one of two possible values, start and end. If the value of this attribute is start, the comparison succeeds if the value tested starts with the specified value. If the value of location is end, the comparison evaluates to true if the value tested ends with the specified value. To test if a value is an exact match of a given value, the value of location can either be start or end. For example, consider the following match tag.
<logic:match parameter="category" value="classA" location="start"> category starts with classA </logic:match>
This tag tests if the value of the category request parameter starts with classA. The test evaluates to true if the category request parameter is classA, classAbc, etc. However, abcClass does not pass the test.
In the absence of the location attribute, the test succeeds if the tested value contains the prescribed value. For instance, the following match tag evaluates to true if the value of the category request parameter is classA, classAbc, abclassA, abclassAbc, and so forth.
<logic:match parameter="category" value="classA"> category starts with classA </logic:match>
If the value being compared does not exist, the match tag throws an exception. Therefore, you may want to use the present tag around this tag.
The notMatch tag does the opposite of the match tag. Here is an example of the notMatch tag.
<logic:notMatch parameter="category" value="classA location="start"> category does not start with classA </logic:notMatch>