Access attributes valueExpression in ManagedBean

508 Views Asked by At

I have a custom facelet tag which simply includes an outputText. Reason for using a custom tag is to modify the value depending on the entity field. For ex: if the outputText is used for a percentage value, I want to print the value with % without the client having to add it

My problem is how do I access the attributes value expression in the backing bean

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

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

In the backing 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

There are 1 best solutions below

4
Selaron On BEST ANSWER

You want to get the ValueExpression #{value} from the f:attribute component? It'd it would be simpler to not search the view, but simply add the attribute to and inspect the h:outputText component and give the attribute a special unique name:

XHTML fragment:

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

}