Deferred Expression是JSP2.1中的新特性, 說(shuō)白了就是因?yàn)樵瓉?lái)JSF中有了這個(gè),然后想把JSP和JSF的EL都給統(tǒng)一起來(lái), 就弄了Unified Expression Language, Sun的文檔也講了一些. 規(guī)范也講了一些.
看看Sun的關(guān)于這個(gè)的一些code:
在JEETCK CTS5中,這是一個(gè)最簡(jiǎn)單的用法, 其實(shí)沒(méi)有體現(xiàn)Deferred Value這個(gè)Deferred的作用, 只是說(shuō)了如何用.
JSP: 是這樣在JSP頁(yè)面調(diào)用的.
xml 代碼
- <test:ELDeferredValueValueTag litExpr="foo" poundExpr="#{foo}"/>
這里這個(gè)poundExpr是Deferred Expression.
TLD: 這里面決定是否用Deferred, 其實(shí)配置起來(lái)還有許多變化.
xml 代碼
- <attribute>
- <name>poundExpr</name>
- <required>true</required>
- <deferred-value> //here define to be deferred expression value
- <type>java.lang.String</type>
- </deferred-value>
- </attribute>
ELDeferredValueValueTag:具體用還是要在Tag Handler中體現(xiàn), 這里是個(gè)simple tag.
這里用的是ValueExpression, 也是JSP2.1中新引入的.
java 代碼
- public void setPoundExpr(ValueExpression poundExpr) {
- this.poundExpr = poundExpr;
- }
- public void doTag() {
- ...
- String poundVal = (String) poundExpr.getValue(elContext);
- ...
- }
But not show how deferred expression works
因?yàn)門(mén)CK中沒(méi)有講到如何個(gè)deferred法, 然后去看看JSF的實(shí)現(xiàn).
In JSF RI:
JSP:
- <h:inputText value="#{color}}" />
html_basic.tld:
- <tag-class>
- com.sun.faces.taglib.html_basic.InputTextTag
- </tag-class>
- <tei-class>
- com.sun.faces.taglib.FacesTagExtraInfo
- </tei-class>
- <attribute>
- <description>
- <![CDATA[The current value of this component.]>
- </description>
- <name>
- value
- </name>
- <required>
- false
- </required>
- <deferred-value>
- <type>
- java.lang.Object
- </type>
- </deferred-value>
- </attribute>
- <attribute>
Tag handler file:InputTextTag.setProperties() //set the value to UIComponent
UIComponentELTag.createComponent
javax.faces.component.UIInput
在JSF中還沒(méi)有發(fā)現(xiàn)更多的, 主要是對(duì)JSF還不是很熟悉, 看來(lái)得先看看JSF.
猜測(cè)一下JSF是這樣處理deferred expression的:
1. 用javascript把頁(yè)面某個(gè)field跟某個(gè)ValueExpression對(duì)象關(guān)聯(lián)起來(lái)
2. 在不同的lifecycle phase, 把頁(yè)面某個(gè)field跟ValueExpression對(duì)象進(jìn)行同步.
3. 一開(kāi)始,evaluate expression的值, 然后javascript把值傳給后臺(tái), 通過(guò)UIInput set property到封裝的ValueExpression上.
4.在后面的phase, 需要重新evaluate這個(gè)expression的值, 則又先get property,通過(guò)javascript修改頁(yè)面的element
得再看JSF.