Java Bean:如何< jsp:setProperty>在< jsp:useBean>中生成到Java代码中 [英] Java Bean: how <jsp:setProperty> in <jsp:useBean> is generated into Java Code

查看:125
本文介绍了Java Bean:如何< jsp:setProperty>在< jsp:useBean>中生成到Java代码中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有这样的代码:

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>

我知道它是如何运作的。但是,有时候,我在这里更改了一些代码,例如:dog到newDog,我会遇到错误或unguested-result(和我一起)。

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).

请告诉我如何将上面的代码生成到Java中。 (也许只是一个主要想法)

Please give me how above code is generated into Java. (maybe just a main idea)

谢谢:)

推荐答案

JSP最终生成为 .java 类,这些类被编译为servlet。检查服务器的工作文件夹。对于Tomcat, /test.jsp 生成为 /org/apache/jsp/test_jsp.java 文件在Tomcat的 / work 文件夹中。

JSPs ultimately get generated to .java classes which get compiled as servlets. 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.

以下行

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

(我做的唯一更改是添加包;无包装类是Bad™)

生成为

  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是开源的, JspRuntimeLibrary#introspecthelper()方法委托给 internalIntrospecthelper()最终这样做:

Tomcat is open source, according to its source code, the JspRuntimeLibrary#introspecthelper() method delegates to internalIntrospecthelper() which ultimately does this:

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);
}

你看,它正在使用 java.beans.Introspector 获取bean信息和属性通过 的BeanInfo#getPropertyDescriptors() 。所需的< jsp:setProperty> 方法是以 java.lang.reflect.Method PropertyDescriptor#getWriteMethod() 。最后,它使用 Reflection API 来调用该方法。

You see, it's using java.beans.Introspector to get bean information and properties by BeanInfo#getPropertyDescriptors(). The desired <jsp:setProperty> method is obtained as java.lang.reflect.Method by PropertyDescriptor#getWriteMethod(). Finally it uses the Reflection API to invoke the method.

这篇关于Java Bean:如何&lt; jsp:setProperty&gt;在&lt; jsp:useBean&gt;中生成到Java代码中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆