0

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;
}  
  • You know jsf has 'converters' for this? E.g. the numberConverter? https://www.mkyong.com/jsf2/jsf-2-convertnumber-example/ https://docs.oracle.com/javaee/7/tutorial/jsf-page-core001.htm – Kukeltje Oct 22 '18 at 18:22
  • Yes, but I'm trying to avoid having to use in every such percentage tag. This answer shows how to do it in a taghanlder, but I want to access it in the managed bean https://stackoverflow.com/questions/31311960/access-raw-expression-of-valueexpression-attribute-to-taglib-component – Cadrian Brown Oct 22 '18 at 19:04
  • So instead of having a standardized converter you want to add an `f:attribute` that refers to something (that could return a boolean and activate the converter or not)... and achieve the same? Strange descision. – Kukeltje Oct 23 '18 at 07:18
  • And you know you can create 'composite' tags that contain this automatically, right? Something like ` which in it has the outputext and the converter... Waaay more simple, clean etc... – Kukeltje Oct 23 '18 at 09:53

1 Answers1

0

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

}
Selaron
  • 6,105
  • 4
  • 31
  • 39