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.