JSF 使用ValueBinding類

3.6  使用ValueBinding類
javax.faces.el.ValueBinding類表示一個對象,可以用它來訪問由一個動作或值引用表達式所表示的屬性。可調用javax.faces.application.Application類的getValueBinding方法,同時傳入值引用,以此來獲取一個ValueBinding的實例。
ValueBinding類有4個方法:getType、getValue、setValue和isReadOnly。我們在以下的章節中討論這些方法。
3.6.1  獲取ValueBinding所表示的對象的類型
調用Application類的getValueBinding方法需要傳入一個值引用,返回值可能是一個JavaBean、一個JavaBean的屬性或是另外一個對象。getType方法可返回ValueBinding所表示的對象的類型。下面是getType方法的簽名:
public abstract Class getType(FacesContext facesContext)
throws javax.faces.el.PropertyNotFoundExcetpion;
比如,假設您的應用配置文件裏有如下managed-bean標記:
<managed-bean>
<managed-bean-name>shoppingCartBean</managed-bean-name>
<managed-bean-class>
buydirect.ShoppingCartBean
</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
則使用以下代碼會在控制檯輸出表示ShoppingCartBean的類名:buydirect.ShoppingCartBean。
FacesContext facesContext = FacesContext.getCurrentInstance();
ApplicationFactory factory = (ApplicationFactory)
FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
Application application = factory.getApplication();
ValueBinding valueBinding =
application.getValueBinding("shoppingCartBean");
System.out.println(valueBinding.getType(facesContext).getName());
3.6.2  獲取和設置ValueBinding對象的屬性
要獲取由ValueBinding對象表示的屬性值,可使用getValue方法。此方法具有如下簽名:
public abstract Object getValue(FacesContext facesContext)
throws javax.faces.el.PropertyNotFoundException
比如,下面的代碼提取由一個ValueBinding表示的ShoppingCartBean對象:
FacesContext facesContext = FacesContext.getCurrentInstance();
ApplicationFactory factory = (ApplicationFactory)
FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
Application application = factory.getApplication();
ValueBinding valueBinding =
application.getValueBinding("shoppingCartBean");
ShoppingCartBean bean =
(ShoppingCartBean) valueBinding.getValue(facesContext);
setValue方法則是用來改變由ValueBinding對象表示的屬性值。其方法簽名如下:
public abstract void setValue
(FacesContext facesContext, Object value)
throws javax.faces.el.PropertyNotFoundException
比如,假設ShoppingCartBean有一個叫作purchaseId的屬性,其聲明如下:
private String purchaseId;
public String getPurchaseId() {
return purchaseId;
}
public void setPurchaseId(String purchaseId) {
this.purchaseId = purchaseId;
}
則下面的代碼可獲取ShoppingCartBean對象的purchaseId屬性,並將其值設爲12345。
FacesContext facesContext = FacesContext.getCurrentInstance();
ApplicationFactory factory = (ApplicationFactory)
FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
Application application = factory.getApplication();
ValueBinding valueBinding =
application.getValueBinding("ShoppingCartBean.purchaseId");
valueBinding.setValue(facesContext, "12345");
// print the current value
System.out.println("Purchase Id:" +
valueBinding.getValue(facesContext));
這段代碼會在控制檯輸出如下文本:
Purchase Id: 12345
3.6.3  檢查ValueBinding屬性是否可寫
isReadOnly方法返回一個指示當前ValueBinding對象所表示的屬性是否可寫的boolean值。其方法簽名如下:
public abstract boolean isReadOnly(FacesContext facesContext)
throws javax.faces.el.PropertyNotFoundException
比如,下面的代碼在改變ShoppingCartBean的purchaseId屬性前先檢查其是否可寫。
FacesContext facesContext = FacesContext.getCurrentInstance();
ApplicationFactory factory = (ApplicationFactory)
FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
Application application = factory.getApplication();
ValueBinding valueBinding =
application.getValueBinding("shoppingCartBean.purchaseId");
if (!valueBinding.isReadOnly(facesContext))
valueBinding.setValue(facesContext, "12345");
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章