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).
Please give me how above code is generated into Java. (maybe just a main idea)
Thanks :)
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.
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:
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 API to invoke the method.
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
来源:https://stackoverflow.com/questions/10285209/java-bean-how-jspsetproperty-in-jspusebean-is-generated-into-java-code