访问ManagedBean中的属性valueExpression

时间:2018-10-22 17:32:43

标签: jsf managed-bean custom-tag facelet-tag

我有一个自定义的facelet标记,其中仅包含一个outputText。使用自定义标签的原因是根据实体字段修改值。例如:如果将outputText用于百分比值,我想用%来打印该值,而无需客户端添加

我的问题是如何访问后备bean中的属性值表达式

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

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

在支持bean中

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;
}  

1 个答案:

答案 0 :(得分:0)

您想从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;
    }

}