最終的にJSPはサーブレットとしてコンパイルされるクラス.java
に生成されます。サーバーの作業フォルダを確認します。 Tomcatの場合、/test.jsp
は、/work
フォルダに/org/apache/jsp/test_jsp.java
というファイルとして生成されます。
次の行
<jsp:useBean id="dog" class="com.example.Dog" scope="application">
<jsp:setProperty name="dog" property="breed" value="House Dog !!!"/>
</jsp:useBean>
(私が作った唯一の変化は、パッケージを追加して、パッケージレスクラスは™悪いです)
が
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()
にデリゲートします。これは最終的にはこの処理が行われます。
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);
}
あなたが見る、BeanInfo#getPropertyDescriptors()
で豆情報とプロパティを取得するためにjava.beans.Introspector
を使用しています。所望の<jsp:setProperty>
の方法はjava.lang.reflect.Method
でPropertyDescriptor#getWriteMethod()
となる。最後に、Reflection APIを使用してメソッドを呼び出します。
いいえ、コードの行が不足していることを知っています:)実際にが実際にどのように生成されたか分かりません。 –
hqt
この動作の仕方は次のとおりです。1.使用Beanには、id、classおよびscope属性があります。 idは、クラス属性に渡すクラス(FQCN)をインスタンス化するために使用される名前または変数です。 2. setPropertyメソッドは、name属性の値を使用してDogクラスのインスタンスを特定します。このクラスを見つけたら、reflectionを使用して、value属性に指定された値を持つDogクラスのbreedプロパティを設定しますsetPropertyのこれは、usebeanのid属性とsetPropertyのname属性の値が一致しなければならない理由です。 –