The setProperty label makes me a headache for a while, a bad problem is type conversion, because the attribute values obtained from the label are character type, to convert these characters to fields, the value can be valued Correct delivery to methods or fields.
JSP: SetProperty action has the following four properties:
Property Description NameName property is required. It means which bean is to set the property. The PropertyProperty property is required. It indicates which property to set. There is a special usage: if the value of the Property is "*", the request parameters that match all the names and bean property names will be passed to the corresponding attribute set method. The Value Value property is optional. This property is used to specify the value of the bean property. String data automatically converts into numbers through standard Valueof methods in the target class, Boolean, Boolean, Byte, Byte, Char, Character. For example, the property value of the Boolean and Boolean types (such as "True") converted through the attribute value of the Boolean.ValueOf conversion, the INT, and Integer types (such as "42") via Integer.Valueof. Value and Param cannot be used at the same time, but can be used any one. Paramparam is optional. It specifies which request parameter as the value of the bean property. If there is no parameters of the current request, what is not done, the system does not pass NULL to the set method of the bean property. Therefore, you can let the bean provide the default attribute value, and only modify the default attribute value when the request parameter is clearly specified.
/ ** * Class Description: Label processing class, imitation JSP SETPROPERTY tab * Create Date: 2004-7-2 * Modify Date: 2004-7-4 * Created: dever * /
Package cn.dever.tag; import javax.servlet.jsp. *; import javax.servlet.jsp.tagext. *; import java.lang.reflect. *;
public class SetProperty extends TagSupport {private String name; // get the class name private String property; // property name is private String method; // returns the attribute method name private String value; // attribute value private String param; // Set Request the parameter name to get the value of the value public void setname (string s) {this.name = S;} public string getName () {Return this.name;} public void setproperty // convert the property name into the method name INT length = property.length (); string temp = proty.substring (0,1) .touppercase () Property.Substring (1, length); this.method = "set" Temp;} public string getproperty () {return this.property;
public void setValue (String value) {this.value = value;} public String getValue () {return this.value;} public void setParam (String param) {this.param = param;} public String getParam () {return this .param;} public int doStartTag () throws JspTagException {if (this.property.equals ( "*")) {// if the property of the attribute is "*" do not process return EVAL_BODY_INCLUDE;} else {Object getInstance = pageContext .gettribute (name); // Get the class of the class class theclass = getInstance.getClass (); // Get the type of the current instance Try {method [] themethods = THECLASS.GETMETHODS (); // Get all methods of this class Class [] typeParameter = new class [] {}; // definition method parameter type array for (int i = 0; i * / MethInstance.invoke (getInstance, objParameter);} catch (NoSuchMethodException e) {throw new JspTagException (method "() not exist is!");} Catch (InvocationTargetException e) {System.out.print ( "Invocation Target Error ! ");} Catch (illegalaccessException e) {system.out.print (" Illegal Access Error! ");} Catch (nullpointerException e) {system.out.print (" Null Pointer Error! ");}}}}}}} Return Eval_Body_include } / ** * Convert parameter values or fields to the corresponding type according to the type name * I only have a few types to implement, when you use it, you need to write all types * / private object getParaobj (Class Type) Throws JSptagexception {string type = type.getname (); object para = new object (); string temp = null; // can only choose one if (this.Value! = Null && this.param! = Null) {throw New JSptagexception ("Select One of Value or Param !!);} else if (this.value! = null) {TEMP = this.Value;} else if (this.param! = null) {TEMP = PageContext.getRequest () .GetParameter (this.Param) .tostring ();} // Type conversion IF ("Java.lang.String")) {if (temp == null) {return "null";} para = String.Valueof (TEMP);} else.equals ("Java. Lang.integer ") || type == integer.Type) {if (temp == null) {return integer.Valueof (" 0 ");} para = integer.valueof (TEMP);} else f (Typename.equals ("java.lang.boolean") || Type == boolean.type) {if (temp == null) {Return Null;} Para = Boolean.valueof (TEMP);