|
11.2 General-Purpose ActionsJSTL provides a handful of general-purpose actions for manipulating scoped variables, beans and maps, and handling exceptions. Those actions are listed in Table 11.9.
Syntax:[1]
Syntax #1: Without a body
Syntax #2: With a body that specifies the default value
Description: The <c:out> action evaluates an expression�either an EL expression (<c:out>) or a JSP expression (<c_rt:out>)�and sends the result of that evaluation, coerced to a string, to the current JspWriter. Attributes:
Constraints and Error Handling:
In a Nutshell: The <c:out> action replaces the syntax for JSP expressions�<%= expr %>�and is JSTL's most heavily used action; it's used in nearly every code example throughout this book. You can use <c:out> for JSTL Expression Language (EL) expressions and <c_rt:out> for JSP expressions. You specify those expressions with the value attribute. You can optionally specify a default value that <c:out> sends to the current JspWriter if the specified value is null or is not a valid expression. You can specify the default value with the default attribute or in the body of the <c:out> action. The escapeXml attribute specifies whether certain characters are converted to HTML character entity codes. Those characters and their corresponding entity codes are listed in Table 11.10. By default, the escapeXml attribute is true, meaning <c:out> converts the characters listed in Table 11.10 to their corresponding character entity codes. If you specify false for the escapeXml attribute, <c:out> will not convert those characters.
Syntax:[2]
Syntax #1: Without a body, sets the value of a scoped variable
Syntax #2: With a body that specifies the value of a scoped variable
Syntax #3: Without a body, sets the value of a bean property or a key/value pair in a map
Syntax #4: With a body that specifies the value of a bean property or the value of a key/value pair in a map
Description: The <c:set> action lets you store a value in a scoped variable or a bean property. You can also use <c:set> to add, modify, or remove a key/value pair in a map. Attributes:
Constraints and Error Handling:
In a Nutshell: For syntaxes 1 and 2, <c:set> sets the value of a scoped variable that you specify with the var attribute and, optionally, the scope attribute. For syntaxes 3 and 4, <c:set> sets a property of a target object. If the target object is a bean, <c:set> sets that bean's property�which you specify with the property attribute�with the value that you specify with the value attribute. If the target object is a Java map and that map has an entry whose key corresponds to the property attribute, <c:set> sets the value of that entry to the value you specify with the value attribute. If the map does not have an entry corresponding to the property attribute, <c:set> creates an entry, adds it to the map, and sets its value to the value that you specify with the value attribute.
Syntax:[3]
Description: The <c:remove> action removes a scoped variable that you specify with the var attribute and, optionally, the scope attribute. Attributes:
In a Nutshell: If you don't specify the scope attribute, <c:remove> removes the scoped variable by calling PageContext.removeAttribute(var). That method searches the page, request, session, and application scopes�in that order�and removes the first scoped variable that it finds with the name that you specified with the var attribute. If you do specify the scope attribute, <c:remove> removes the scoped variable by calling PageContext.removeAttribute(var, scope), which removes the specified variable from the specified scope.
Syntax:[4]
Description: The <c:catch> action catches the first exception thrown from its body content. If you specify the optional var attribute, <c:catch> stores the exception in a scoped variable with a name corresponding to the var attribute's value. Attributes:
In a Nutshell: Most of the time, you will probably specify the var attribute for the <c:catch> action so that <c:catch> will store the exception that it catches in a scoped variable. If you don't specify that attribute, <c:catch> will catch the exception but it won't save it; essentially, that approach lets you ignore exceptions and is not recommended. |
|
No comments:
Post a Comment