markush81
markush81

Reputation: 181

Converter for entity in h:outputText

We have an entity MyType:

MyType { String s; getS();}

And we have a JSF converter MyTypeConverter for this type:

@FacesConverter(forClass=MyType.class)
MyTypeConverter { 
    public String getAsString() { return ((MyType)o).getS().toUpperCase();}
}

And we have a backing bean MyBean:

MyBean { 
    getMyType() { return new MyType("hallo");}
}

And we have a view my.xhtml:

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

The above view works fine:

HALLO

When I edit it as follows:

<h:outputText value="(#{myBean.myType})"/>

I expected to see:

(HALLO)

However, the result is:

(org.playground.model.MyType@cb4a479)

How is this caused and how can I solve it?

Upvotes: 1

Views: 2005

Answers (2)

BalusC
BalusC

Reputation: 1109725

The converter applies to the entire component's value. In your case,

<h:outputText value="(#{myBean.myType})"/>

this is evaluated as String because of the parentheses.

Just put the parentheses outside.

(<h:outputText value="#{myBean.myType}"/>)

See also:

Upvotes: 1

user1983983
user1983983

Reputation: 4841

When you have a converter with a forClass it gets called if the value is of the specified type. myBean.myType is obviously of type MyType but (#{myBean.myType}) which evaluates to #{'('.concat(myBean.myType.toString()).concat(')') is of type String, so the converter will not and can not be applied on the given value.

I think your intention was, that the converter will be used whenever a EL expression evaluates to a value of type MyType. But it will only be applied as if you would specify it by <f:converter converterId="myTypeConverter"/> on some ui-controls. This would not work either if the given value-property is of an invalid type.

To resolve this you would either have to override the toString-method on MyType, to call the converter explicitly in the EL or to define another method on MyType which you call explicitly in the EL and which returns the String you would like to see.

Upvotes: 1

Related Questions