Oscommerce web hosting - JavaSercer Pages The UsePropertyTagExtraInfo class sets the scope

JavaSercer Pages The UsePropertyTagExtraInfo class sets the scope to AT_END . As you can see in Example 16.5, this results in the variable declaration and assignment code being added after the doEndTag( ) call. To satisfy the second requirement, the tag handler must therefore give the variable a value and save it in one of the JSP scopes at the very latest in the doEndTag( ) method. Example 16.7 shows the doEndTag( ) method for the UsePropertyTag class. Example 16.7. Saving the New Object in a JSP Scope public int doEndTag( ) throws JspException { Object obj = pageContext.findAttribute(name); if (obj == null) { throw new JspException(”Variable ” + name + ” not found”); } Object propObj = getProperty(obj, property, className); pageContext.setAttribute(id, propObj); return SKIP_BODY; } The value is added to the page scope by calling the setAttribute( ) method on the current PageContext object, using the name specified by the id attribute. If the scope is specified as AT_BEGIN instead, the declaration is added before the doStartTag( ) call and the assignment code is added right after the call. In this case, the tag handler must save the variable in a JSP scope in the doStartTag( ) method. If the tag handler implements BodyTag, assignment code is also added so that it is executed for every evaluation of the body, and after the call to doAfterBody( ) . This allows the tag handler to modify the variable value in the doAfterBody( ) method, so each evaluation of the body has a new value. When we look at an iteration action later, you’ll see why this is important. Finally, if the scope is set to NESTED, both the declaration and the value assignment code are inserted in the code block representing the action body. The tag handler must therefore make the variable available in either the doStartTag( ) method or the doInitBody( ) method, and can also modify the value in the doAfterBody( ) method. The UsePropertyTagExtraInfo class sets the varName and className properties of the VariableInfo bean to the values of the id and className attributes specified by the page author in the JSP page. This is done using another simple class named TagData , passed as the argument to the getVariableInfo( ) method, as shown in Example 16.6. The TagData instance is created by the JSP container and contains information about all action attributes that the page author specified in the JSP page. It has two methods of interest. First, the getAttributeString( ) method, used in Example 16.6, simply returns the specified attribute as a String. But some attribute values may be specified by a JSP expression instead of a string literal, so-called request- time attributes. Since these values are not known during the translation phase, the TagData class also provides the getAttribute( ) method to indicate if an attribute value is a literal string, a request-time attribute, or not set at all. The getAttribute( ) method returns an Object. If the attribute is specified as a request-time value, the special REQUEST_TIME_VALUE object is returned. Otherwise, a String is returned, or null if the attribute is not set. 16.6 Developing an Iterating Action As I alluded to earlier, a tag handler can iterate over the element’s body until some condition is true. The evaluation of the body may be different for each iteration, since the tag handler can introduce a variable (used in the body) that changes its value. An example of an iterating action is the used in this book. It can be used to iterate over the element body once for each value in an array, a java.util.Vector, a java.util.Dictionary, or a java.util.Enumeration. Here’s an example of how the action can be used: <%@ page language="java" contentType="text/html" %> <%@ taglib uri="/orataglib" prefix="ora" %> <% String[] test = new String[4]; test[0] = "first"; test[1] = "second"; test[2] = "third"; test[3] = "fourth"; pageContext.setAttribute("test", test); %> page 226
Note: If you are looking for best hosting provider to host and run your tomcat application check Astra tomcat hosting services

Comments are closed.