簡體   English   中英

訪問ManagedBean中的屬性valueExpression

[英]Access attributes valueExpression in ManagedBean

我有一個自定義的facelet標記,其中僅包含一個outputText。 使用自定義標簽的原因是根據實體字段修改值。 例如:如果將outputText用於百分比值,我想用%打印該值,而客戶端不必添加它

我的問題是如何訪問后備bean中的屬性值表達式

<f:attribute name="value" value="#{value}" />    

<h:outputText value="#{outputBean.value}"></h:outputText>

在后備豆中

public String getValue() {
    //ValueExpression valueExpression =     Read the page attributes and get the value expression of attribute "value"
    // String value =   set the value according to the value expression after necessary modifications

    return value;
}  

您想從f:attribute組件獲取ValueExpression#{value}嗎? 如果不搜索視圖,而是簡單地將屬性添加到並檢查h:outputText組件,然后為該屬性指定一個特殊的唯一名稱,這將變得更加簡單:

XHTML片段:

<h:outputText value="#{myBean.value}" >
    <f:attribute name="tofuwurst" value="#{chunk}"/>
</h:outputText>

MyBean.java:

import javax.el.ValueExpression;
import javax.enterprise.context.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.inject.Named;

@Named
@RequestScoped
public class MyBean {
    private Object value;

    public Object getValue() {
        FacesContext context = FacesContext.getCurrentInstance();
        UIComponent currentComponent = UIComponent.getCurrentComponent(context);
        ValueExpression veTofuwurst = currentComponent.getValueExpression("tofuwurst");
        assert null != veTofuwurst;
        // have fun with a #{chunk} of Tofuwurst here

        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM