JavaSercer Pages 17.3.2 The Action A set of (Cms hosting)

January 27, 2007 on 4:29 am | In Java |

JavaSercer Pages 17.3.2 The Action A set of value actions can be used inside the body of an or action to set the values for placeholders in the SQL statement. The example tag library contains value actions for date/time values as well as for numeric and string values. The tag handlers for all these actions have a great deal in common, so they all extend a common superclass called com.ora.jsp.tags.sql.value.ValueTag . This superclass contains instance variables and property setter methods for the attributes shared by all value actions: stringValue, pattern, param, name, and property. In addition, it contains methods used by the subclasses to get the value when it’s specified as a parameter name or a bean name plus a property name. Let’s look at the tag handler class for the action as an example. All other value actions follow the same pattern. The com.ora.jsp.tags.sql.IntValueTag class extends the ValueTag class as shown in Example 17.21. Example 17.21. The IntValueTag Class package com.ora.jsp.tags.sql.value; import java.lang.reflect.*; import java.text.*; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import com.ora.jsp.sql.value.*; import com.ora.jsp.tags.sql.ValueTagParent; import com.ora.jsp.util.*; public class IntValueTag extends ValueTag { private int value; public void setValue(int value) { this.value = value; } public int doEndTag( ) throws JspException { if (stringValue != null) { value = toInt(stringValue, pattern); } else if (param != null) { String paramValue = getParameter(param); value = toInt(paramValue, pattern); } else if (name != null) { value = getInt(name, property, pattern); } ValueTagParent parent = (ValueTagParent) findAncestorWithClass(this, ValueTagParent.class); if (parent == null) { throw new JspException(”The sqlIntValue action is not ” + “enclosed by a supported action type”); } parent.addValue(new IntValue(value)); return EVAL_PAGE; } … } The Java datatype is different for each value action, so all subclasses implement their own value property setter methods to store the value in an instance variable. The value property for the IntValueTag is of course an int. Besides the value attribute, all value actions support three other ways to set the value: as a String value specified by the stringValue attribute, as a request parameter specified by the param attribute, or as a bean property specified by the name and property attributes. The ValueTag superclass contains the setter methods and instance variables for these attributes, and the ValueTagExtraInfo class makes sure that a page author uses only one of these alternatives to specify the value. The doEndTag( ) method finds out which alternative is used by looking at all property instance variables in turn. If it’s a String value, defined by the stringValue or param attributes, the method converts the value to an int using a private toInt( ) method. If it’s specified as a bean property, another private method, getInt( ) , is used to invoke the bean to get the value. These private methods are not shown here, but you can look at the source code if you want to see how they work. The enclosing or action’s tag handler is then located, as described in Chapter 16, using the findAncestorWithClass( ) method. The tag handlers for the enclosing actions implement the ValueTagParent interface used as the parent class argument. If an enclosing action is found, its addValue( ) method is called to add the int value wrapped in an IntValue object to the parent’s value list. page 253

Hint: This post is supported by Gama php5 hosting services

No Comments yet

TrackBack URI

Sorry, the comment form is closed at this time.