11
Logic Tags • The Logic tag library contains tags that are useful in managing conditional generation of output text, looping over object collections for repetitive generation of output text, and application flow management.

Struts_Logic Tags

  • Upload
    rajesh

  • View
    193

  • Download
    1

Embed Size (px)

DESCRIPTION

this is only

Citation preview

Page 1: Struts_Logic Tags

Logic Tags

• The Logic tag library contains tags that are useful in managing conditional generation of output text, looping over object collections for repetitive generation of output text, and application flow management.

Page 2: Struts_Logic Tags

Logic Tags• For tags that do value comparisons (equal, greaterEqual, greaterThan, lessEqual, lessThan, notEqual), the following rules apply:– The specified value is examined. If it can be converted

successfully to a double or a long, it is assumed that the ultimate comparison will be numeric (either floating point or integer). Otherwise, a String comparison will be performed.

– The variable to be compared to is retrieved, based on the selector attribute(s) (cookie, header, name, parameter, property) present on this tag. It will be converted to the appropriate type for the comparison, as determined above.

– A request time exception will be thrown if the specified variable cannot be retrieved, or has a null value.

– The specific comparison for this tag will be performed, and the nested body content of this tag will be evaluated if the comparison returns a true result.

Page 3: Struts_Logic Tags

Logic Tags• For tags that do substring matching (match, notMatch), the following rules apply:– The specified variable is retrieved, based on the

selector attribute(s) (cookie, header, name, parameter, property) present on this tag. The variable is converted to a String, if necessary.

– A request time exception will be thrown if the specified variable cannot be retrieved, or has a null value.

– The specified value is checked for existence as a substring of the variable, in the position specified by the location attribute, as follows: at the beginning (if location is set to start), at the end (if location is set to end), or anywhere (if location is not specified).

Page 4: Struts_Logic Tags

Logic TagsTag Name Description

empty Evaluate the nested body content of this tag if the requested variable is either null or an empty string.

equal Evaluate the nested body content of this tag if the requested variable is equal to the specified value.

forward Forward control to the page specified by the specified ActionForward entry.

greaterEqual Evaluate the nested body content of this tag if requested variable is greater than or equal to specified value.

greaterThan Evaluate the nested body content of this tag if the requested variable is greater than the specified value.

iterate Repeat the nested body content of this tag over a specified collection.

lessEqual Evaluate the nested body content of this tag if requested variable is greater than or equal to specified value.

lessThan Evaluate the nested body content of this tag if the requested variable is less than the specified value.

matchEvaluate the nested body content of this tag if specified value is an appropriate substring of requested variable.

messagesNotPresent Generate the nested body content of this tag if the specified message is not present in this request.

messagesPresent Generate the nested body content of this tag if the specified message is present in this request.

notEmpty Evaluate the nested body content of this tag if the requested variable is neither null nor an empty string.

notEqual Evaluate the nested body content of this tag if the requested variable is not equal to the specified value.

notMatch Evaluate the nested body content of tag if specified value not an appropriate substring of requested variable.

notPresent Generate the nested body content of this tag if the specified value is not present in this request.

present Generate the nested body content of this tag if the specified value is present in this request.

redirect Render an HTTP Redirect

Page 5: Struts_Logic Tags

Logic Tags - Example<html:html>

<head>

<title><bean:message key="imagebrokerlink.title"/></title>

<META name="GENERATOR" content="IBM WebSphere Studio">

</head>

<body>

<html:form action="/ImageLocationForm" >

<center>

<font size=3>

<br>

<b>

<logic:notEqual property="action" name="ImageLocationForm" value="Insert">

<bean:message key="imagelocationdetail.title"/>

</logic:notEqual>

<logic:equal property="action" name="ImageLocationForm" value="Insert">

<bean:message key="imagelocationinsert.title"/>

</logic:equal>

Page 6: Struts_Logic Tags

To use the Logic Tag Library in a Struts application, your application's JSPs mustdeclare their use of the library with a JSP taglib directive:<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>

<strutslgc:present name="searchForm" property="results">

<logic:present name="searchForm" property="results">

Using the Logic Tag Library

if you chose to use a prefix of "strutslgc",

Page 7: Struts_Logic Tags

The empty Tag

Example Usage

<logic:empty name="results"> Your search yielded no results. </logic:empty> If the results object is null, contains a zero-length string, or contains a Collection or Map with zero elements, then the content between the starting and ending empty tags will be processed. .

<logic:empty name="bizObj" property="results"> Your search yielded no results. </logic:empty>

you can explicitly specify the scope of the object with the scope attribute.

Page 8: Struts_Logic Tags

<logic:equal cookie="role" value="Manager"> User is a Manager. </logic:equal>

<logic:equal header="User-Agent" value="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"> Browser is Internet Explorer. </logic:equal>

<logic:equal parameter="catId" value="10"> Category Id is 10. </logic:equal>

<logic:equal name="resultCaount" value="0"> Search returned no results. </logic:equal>

<logic:equal name="employee" property="name" value="Bob"> Hello, Bob! </logic:equal>

Example Usage

Page 9: Struts_Logic Tags

The iterate Tag

The iterate tag is used to wrap content that is repeated for each element of a specified collection. The collection can be any of the following:An array of Java objects or primitives (e.g., int, long, and so on)An implementation of java.util.Collection An implementation of java.util.Enumeration An implementation of java.util.Iterator An implementation of java.util.Map

<logic:iterate id="result" name="results"> Result: <bean:write name="result"/><br> </logic:iterate>

<logic:iterate id="result" name="searchObj" property="results"> Result: <bean:write name="result"/><br> </logic:iterate>

Page 10: Struts_Logic Tags

The match Tag

<logic:match header="User-Agent" location="end" value="Windows NT 5.1)"> Browser is Windows-based. </logic:match>

<logic:match parameter="category" location="start" value="Cloth"> Category is Clothes. </logic:match>

<logic:match name="employee" property="lastName" location="start" value="H“>Last name starts with "H".</logic:match>

The messagesNotPresent Tag

<logic:messagesNotPresent> No errors are present. </logic:messagesNotPresent>

Page 11: Struts_Logic Tags

The present Tag

<logic:present cookie="role"> Role cookie exists. </logic:present>

<logic:present header="Host"> Host header was specified. </logic:present>

<logic:present name="employee" property="name"> Employee object has a name field. </logic:present>

The redirect Tag

<logic:redirect href="http://www.yahoo.com/" paramId="query" paramName="queryObj"/>