简体   繁体   English

Java Bean:如何 <jsp:setProperty> 在 <jsp:useBean> 被生成为Java代码

[英]Java Bean: how <jsp:setProperty> in <jsp:useBean> is generated into Java Code

For example, I have this code: 例如,我有以下代码:

<jsp:useBean id="dog" class="Dog" scope="application">
     <jsp:setProperty name="dog" property="breed" value="House Dog !!!"/>
</jsp:useBean>

I know how it works. 我知道它是如何工作的。 But, sometimes, I change some some code in this, for example: "dog" to "newDog", I will meet error or unguested-result (with me). 但是,有时,我会在其中更改一些代码,例如:将“ dog”更改为“ newDog”,我会遇到错误或毫无根据的结果。

Please give me how above code is generated into Java. 请给我上面的代码如何生成到Java中。 (maybe just a main idea) (也许只是一个主意)

Thanks :) 谢谢 :)

JSPs ultimately get generated to .java classes which get compiled as servlets. JSP最终生成为.java类,并作为servlet进行编译。 Check the server's work folder. 检查服务器的工作文件夹。 In case of Tomcat, a /test.jsp gets generated as /org/apache/jsp/test_jsp.java file in Tomcat's /work folder. 如果是Tomcat, /test.jsp在Tomcat的/work文件夹/org/apache/jsp/test_jsp.java作为/org/apache/jsp/test_jsp.java文件生成。

The following lines 以下几行

<jsp:useBean id="dog" class="com.example.Dog" scope="application">
     <jsp:setProperty name="dog" property="breed" value="House Dog !!!"/>
</jsp:useBean>

(the only change I made is adding a package; packageless classes are Bad™) (我所做的唯一更改是添加了一个程序包;无程序包类是Bad™)

are generated as 生成为

  com.example.Dog dog = null;
  synchronized (application) {
    dog = (com.example.Dog) _jspx_page_context.getAttribute("dog", javax.servlet.jsp.PageContext.APPLICATION_SCOPE);
    if (dog == null){
      dog = new com.example.Dog();
      _jspx_page_context.setAttribute("dog", dog, javax.servlet.jsp.PageContext.APPLICATION_SCOPE);
      out.write("\n");
      out.write("     ");
      org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper(_jspx_page_context.findAttribute("dog"), "breed", "House Dog !!!", null, null, false);
      out.write('\n');
    }
  }

Tomcat is open source, according to its source code, the JspRuntimeLibrary#introspecthelper() method delegates to internalIntrospecthelper() which ultimately does this: Tomcat是开放源代码,根据其源代码, JspRuntimeLibrary#introspecthelper()方法委托给internalIntrospecthelper()最终实现此目的:

Method method = null;
Class<?> type = null;
Class<?> propertyEditorClass = null;
try {
    java.beans.BeanInfo info
        = java.beans.Introspector.getBeanInfo(bean.getClass());
    if ( info != null ) {
        java.beans.PropertyDescriptor pd[]
            = info.getPropertyDescriptors();
        for (int i = 0 ; i < pd.length ; i++) {
            if ( pd[i].getName().equals(prop) ) {
                method = pd[i].getWriteMethod();
                type   = pd[i].getPropertyType();
                propertyEditorClass = pd[i].getPropertyEditorClass();
                break;
            }
        }
    }
    if ( method != null ) {
        if (type.isArray()) {
            if (request == null) {
                throw new JasperException(
                    Localizer.getMessage("jsp.error.beans.setproperty.noindexset"));
            }
            Class<?> t = type.getComponentType();
            String[] values = request.getParameterValues(param);
            //XXX Please check.
            if(values == null) return;
            if(t.equals(String.class)) {
                method.invoke(bean, new Object[] { values });
            } else {
                createTypedArray (prop, bean, method, values, t,
                                  propertyEditorClass); 
            }
        } else {
            if(value == null || (param != null && value.equals(""))) return;
            Object oval = convert(prop, value, type, propertyEditorClass);
            if ( oval != null )
                method.invoke(bean, new Object[] { oval });
        }
    }
} catch (Exception ex) {
    Throwable thr = ExceptionUtils.unwrapInvocationTargetException(ex);
    ExceptionUtils.handleThrowable(thr);
    throw new JasperException(ex);
}

You see, it's using java.beans.Introspector to get bean information and properties by BeanInfo#getPropertyDescriptors() . 您会看到,它使用java.beans.Introspector通过BeanInfo#getPropertyDescriptors()获取bean信息和属性。 The desired <jsp:setProperty> method is obtained as java.lang.reflect.Method by PropertyDescriptor#getWriteMethod() . 通过PropertyDescriptor#getWriteMethod()作为java.lang.reflect.Method获得所需的<jsp:setProperty>方法。 Finally it uses the Reflection API to invoke the method. 最后,它使用Reflection API调用该方法。

This is how it is generated: 它是这样生成的:

Dog dog = new Dog();
dog.setBreed("House Dog !!!");

The dog in setProperty is a reference to the Class Dog of useBean. setProperty中的dog是对useBean的类Dog的引用。 Hope you understand this 希望你能理解

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM