I have a simple JSF web app as follows. The Java class contains:
private String myString;
public String getMyString()
{
return myString;
}
public void setMyString( String radioValue )
{
myString = someHashMap.get( radioValue );
doStuffWith( myString );
}
And the JSF page source contains:
<h:form>
<h:selectOneRadio id="topicSelectOneRadio" required="true"
requiredMessage="Choose a thing"
value="#{someBean.myString}">
<f:selectItem itemValue="radioValue1" itemLabel="1"/>
<f:selectItem itemValue="radioValue2" itemLabel="2"/>
</h:selectOneRadio>
<p><h:commandButton value="Do it"/></p>
</h:form>
As I understand it, the radioValueX string from the webpage is being used in the Java class as the parameter to the myString setter method. So I wanted to extend it one step further to use any class:
private Foo myFoo;
public Foo getMyFoo()
{
return myFoo;
}
public void setMyFoo( String radioValue )
{
myFoo = someHashMap.get( radioValue );
doStuffWith( myFoo );
}
and
<h:form>
<h:selectOneRadio id="topicSelectOneRadio" required="true"
requiredMessage="Choose a thing"
value="#{someBean.myFoo}">
<f:selectItem itemValue="radioValue1" itemLabel="1"/>
<f:selectItem itemValue="radioValue2" itemLabel="2"/>
</h:selectOneRadio>
<p><h:commandButton value="Do it"/></p>
</h:form>
However, I get the following error:
Conversion Error setting value 'radioValue' for 'null Converter'.
I’m not sure I understand where any sort of “conversion” needs to take place. In both cases, the String value from the radio button is being used as a parameter to a setter method which takes a String value as the parameter. True, in the latter case, it is actually setting the value for something that isn’t a String, but that shouldn’t matter, should it? I’m not trying to convert the radioValueX String in the latter case to any other type, just using it as the key to a hashmap, same as in the first case.
Can anybody help me understand what I’m missing? Thanks!
JSF tries to convert your
StringtoObjectbefore the setter is called. And since you have no converter registered for your object, you get this error.Conversion is performed in JSF’s lifecycle phase 3 (PROCESS VALIDATIONS). The setters are called in phase 4 (UPDATE MODEL VALUES).
You need to add a converter to make it work:
Use the converter this way:
Then (as correctly stated in Ian’s comment to your question) you should change your setter since you don’t need the conversion here anymore: