Java Bean:<jsp:useBean>中的<jsp:setProperty>如何生成为Java代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10285209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Bean: how <jsp:setProperty> in <jsp:useBean> is generated into Java Code
提问by hqt
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”,我会遇到错误或 unguested-result(与我一起)。
Please give me how above code is generated into Java. (maybe just a main idea)
请告诉我如何将上述代码生成为 Java。(也许只是一个主要想法)
Thanks :)
谢谢 :)
回答by BalusC
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 最终生成为.java
编译为 servlet 的类。检查服务器的工作文件夹。如果是 Tomcat,/test.jsp
则会/org/apache/jsp/test_jsp.java
在 Tomcat 的/work
文件夹中生成一个文件。
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?)
(我所做的唯一更改是添加一个包;无包类很糟糕?)
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()
. The desired <jsp:setProperty>
method is obtained as java.lang.reflect.Method
by PropertyDescriptor#getWriteMethod()
. Finally it uses the Reflection APIto invoke the method.
您会看到,它java.beans.Introspector
用于通过 获取 bean 信息和属性BeanInfo#getPropertyDescriptors()
。所需的<jsp:setProperty>
方法如获得java.lang.reflect.Method
通过PropertyDescriptor#getWriteMethod()
。最后,它使用反射 API来调用该方法。
回答by Uchenna Nwanyanwu
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. Hope you understand this
setProperty 中的 dog 是对 useBean 的 Dog 类的引用。希望你明白这一点