很久没发博客了,今天开始,继续发表一些学习心得。
这几天在读JEE的tutorial(原谅我有时候想不起中文意思),读到Binding a Component Instance to a Bean Property。有些心得,记下来以备以后翻阅。
绑定component和managed bean有三种。
一,就是把component的值和managed bean的property绑定
这个很好理解
<h:outputText value="#{cashierBean.shipDate}">
<f:convertDateTime type="date" dateStyle="full" />
</h:outputText>
private Date shipDate;
public Date getShipDate() {
return this.shipDate;
}
public void setShipDate(Date shipDate) {
this.shipDate = shipDate;
}
就是把bean里的property和component的值绑定,我就不多说了。
二。Binding a Component Value to an Implicit Object
在page里面
<h:outputFormat title="thanks"
value="#{bundle.ThankYouParam}">
<f:param value="#{sessionScope.name}"/>
</h:outputFormat>
在message properties 文件里
ThankYouParam=Thank you, {0}, for purchasing your books from us.
从session里读出名字放进去。
三,把component 的instance和bean 的property绑定
这个解释我读了很久才明白。
其实就是说如果一些component的需要后台处理过才能render,这个时候我们可以绑定instance给property,在bean里去给这个instance的attribute设置需要的值。
比如:
<h:selectBooleanCheckbox id="fanClub"
rendered="false"
binding="#{cashierBean.specialOffer}" />
<h:outputLabel for="fanClub"
rendered="false"
binding="#{cashierBean.specialOfferText}"
value="#{bundle.DukeFanClub}"/>
</h:outputLabel>
这个checkbox是不可见的,rendered是false,但是我们需要一些判断来渲染这个checkbox。下面就是bean里面的,
创建了component 的instance,而不是值,如果满足一定条件,就渲染这两个component
UIOutput specialOfferText = null;
UISelectBoolean specialOffer = null;
UIOutput thankYou = null;
if ((cart.getTotal() > 100.00) && !specialOffer.isRendered()) {
specialOfferText.setRendered(true);
specialOffer.setRendered(true);
return null;
} else if (specialOffer.isRendered() && !thankYou.isRendered()) {
thankYou.setRendered(true);
return null;
}
希望上面这些解释能对大家有些帮助。