To all:

I wanted to be able to use the jsp:setProperty tag in my JSP 
pages.  However, I would like to be able to set the "name", "property" and 
the "value"/"param" attributes dynamically (i.e. via JSP expressions).  I 
know that I can use:

<jsp:setProperty name="client" property="*" />

But, what if I need to do something like:

<jsp:setProperty name="client" property="<%= param.getName() %>" value="<%= 
param.getValue() %>" />

This isn't possible with jsp:setProperty because it doesn't evaluate the 
expression inside of the property attributes.  Is this possibly just the 
JSP engine implementation that I'm using (Caucho's Resin) or is this universal?

If this is universal, I would like to submit a sample setProperty tag that 
I've worked on to solve this limitation of using the jsp:setProperty 
tag.  If this problem is not universal, let me know!  If this setProperty 
tag is useful to the group, then I will finish a corresponding getProperty 
tag.  Let me know.

John Rayburn
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
  PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
  "http://java.sun.com/j2ee/dtds/web-jsptaglib_1_1.dtd";>

<!-- a tag library descriptor for the Qualis ICG Bean Tags -->

<taglib>
  <!-- after this the default space is
        "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd";
   -->

  <!-- The version number of this tag library -->
  <tlibversion>1.0</tlibversion>

  <!-- The JSP specification version required to function -->
  <jspversion>1.1</jspversion>

  <!-- The short name of this tag library -->
  <shortname>qbeans</shortname>

  <!-- Public URI that uniquely identifies this version of the tag library -->
  <uri>http://www.qualis-consulting.com/taglibs/qbeans</uri>

  <!-- General information about this tag library -->
  <info>
        Tag library that is used to manipulate JavaBeans
  </info>

  <!-- ******************** Defined Custom Tags *************************** -->

  <!-- setProperty tag -->
  <tag>
    <name>setProperty</name>
    <tagclass>qicg.project.tags.SetPropertyTag</tagclass>
    <bodycontent>empty</bodycontent>
    <info>
                Uses reflection to set a property in a given JavaBean 
    </info>
    <attribute>
                <name>name</name>
                <required>true</required>
                <rtexprvalue>true</rtexprvalue>
    </attribute>        
    <attribute>
                <name>param</name>
                <required>false</required>
                <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
                <name>property</name>
                <required>true</required>
                <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
                <name>value</name>
                <required>false</required>
                <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>
</taglib>
package qicg.project.tags;

import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.JspException;
import java.beans.*;
import java.lang.reflect.*;

public class SetPropertyTag extends TagSupport {
        //////////////////////////////////////////////////////////////////////
        //
        //                                                              CONSTRUCTORS
        //
        //////////////////////////////////////////////////////////////////////  
        public SetPropertyTag () {;}
        
        //////////////////////////////////////////////////////////////////////
        //
        //                                                              INSTANCE DATA
        //
        //////////////////////////////////////////////////////////////////////  
        /** The bean id */
        private String name;
        /** The scope in which the bean is found */
        private String property;
        /** Action that the form should be sent to */
        private String param;
        /** Method through which the form should be posted */
        private String value;
        
        /** Bean Object */
        private Object bean;
        
        //////////////////////////////////////////////////////////////////////
        //
        //                                                              METHODS FOR 
TAG HANDLING
        //
        //////////////////////////////////////////////////////////////////////  
        public int doStartTag () throws JspException {
                validate();
                
                bean = pageContext.getAttribute(name);
                
                // Verify that the Bean indicated by the id is not null and that it is 
an EditableBean
                if (bean == null){
                        throw new JspException ("Bean " + name + " must not be null.");
                }
                
                setProperty();

                return SKIP_BODY;
        }
        
        public int doEndTag () {
                return EVAL_PAGE;
        }
        
        /**
         * Validate the input for valid data before processing
         */
        void validate () throws JspException {
                if ((param == null && value == null) || (param != null && value != 
null)){
                        throw new JspException("Either the param or the value 
attributes must be supplied, not both or neither.");
                }
        }
        
        /**
         * Set the property
         */
        private void setProperty () throws JspException {
                try {
                        PropertyDescriptor [] pds = 
Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
                        int index = -1;

                        for (int i = 0; i < pds.length; i++){
                                // If property equals name of current property 
descriptor, remember the index
                                if (property.equals(pds[i].getName())){
                                        index = i;
                                }
                        }
                        
                        if (index == -1/**|| pds[index].isHidden()*/){
                                // If property not found
                                return;
                        }else {
                                PropertyEditor pedit = null;
                                
                                if (pds[index].getPropertyEditorClass() != null){
                                        pedit = 
(PropertyEditor)pds[index].getPropertyEditorClass().newInstance();
                                }else{
                                        pedit = 
PropertyEditorManager.findEditor(pds[index].getPropertyType());                        
         
                                }
                                
                                if (pedit == null) throw new JspException("No suitable 
property editor found!");

//                              pageContext.getOut().println(property + ": " + value + 
", Editor: " + pedit.getClass().toString() + "<br>");
                                
                                // Get the appropriate value from either the parameter 
or value provided
                                pedit.setAsText(getNewValue());
                                        
                                // Get value and invoke setter method
                                Object [] setParams = new Object[1];
                                setParams[0] = pedit.getValue();

//                              pageContext.getOut().println(property + ": " + 
pedit.getValue() + ", Editor: " + pedit.getClass().toString() + "<br>");
                                
                                Method set = pds[index].getWriteMethod();
                                set.invoke(bean, setParams);
                        }                       
                }catch(InstantiationException e){
                        e.printStackTrace();
                        throw new JspException(e.getMessage());
                }catch(IllegalArgumentException e){
                        e.printStackTrace(new 
java.io.PrintWriter(pageContext.getOut()));
                }
                catch(IllegalAccessException e){
                        e.printStackTrace();
                        throw new JspException(e.getMessage());
                }catch(IntrospectionException e){
                        e.printStackTrace();
                        throw new JspException(e.getMessage());
                }catch(InvocationTargetException e){
                        e.printStackTrace();
                        e.fillInStackTrace();
                        e.getTargetException().printStackTrace(new 
java.io.PrintWriter(pageContext.getOut()));
                        throw new JspException(e.getTargetException().getMessage() + 
", class: " + e.getTargetException().getClass());
                }catch(java.io.IOException e){
                        e.printStackTrace();
                        throw new JspException(e.getMessage());
                }
        }
        
        /**
         * Gets the new value, either from the 
         */
        private String getNewValue () throws JspException {
                String val = null;
                
                if (param != null){
                        try {
                                val = pageContext.getRequest().getParameter(param);
                        }catch(Exception e){
                                throw new JspException ("Error accessing parameter 
value, " + param);
                        }
                }else if (value != null){
                        val = getValue();
                }
                
                return val;
        }
        //////////////////////////////////////////////////////////////////////
        //
        //                                                              ACCESSOR 
METHODS
        //
        //////////////////////////////////////////////////////////////////////  
        public void setName(String name){
                this.name = name;
        }
        public void setParam(String param){
                this.param = param;
        }
        public void setValue(String value){
                this.value = value;
        }
        public void setProperty(String prop){
                this.property = prop;
        }
        public String getName() { return this.name; }
        public String getParam() { return this.param; }
        public String getValue() { return this.value; }
        public String getProperty() { return this.property; }
}

Reply via email to