rym
rym

Reputation: 113

selectonemenu with the error java.lang.String cannot be cast to javax.faces.model.SelectItem

I want to fill a selectonemenu but always I have this error :

java.lang.String cannot be cast to javax.faces.model.SelectItem

this is the code:

public class ToolsJIRA implements Serializable{

private String myChoicePeriod;

 //getters and setters
}

JSF:

  <h:selectOneMenu value="#{ToolsJIRA.myChoicePeriod}">
                   <f:selectItem itemValue="Month" value="Month"/>
                   <f:selectItem itemValue="Week" value="Week"/>
                   <f:selectItem itemValue="Year" value="Year"/>
  </h:selectOneMenu> 

I have found that I should write a converter but I don't Know why? beacause I have seen some example work without a converter??

thank you

Upvotes: 8

Views: 24775

Answers (3)

Aditzu
Aditzu

Reputation: 706

The answer is "a little" late but probably the best solution is :

<h:selectOneMenu value="#{ToolsJIRA.myChoicePeriod}">
                   <f:selectItem itemLabel="Month" itemValue="Month"/>
                   <f:selectItem itemLabel="Week" itemValue="Week"/>
                   <f:selectItem itemLabel="Year" itemValue="Year"/>
  </h:selectOneMenu> 

P.S. Although is a late answer I posted it for others who will face the same problem

Upvotes: 5

Maddy
Maddy

Reputation: 3816

Try this code in in your webpage

<h:selectOneMenu value="#{checkBoxBean.myChoicePeriod}">
                <f:selectItem itemValue="Month" />
                <f:selectItem itemValue="Week" />
                <f:selectItem itemValue="Year" />
             </h:selectOneMenu>

Do not use value attribute its inteded for different purpose

Upvotes: 22

Jigar Joshi
Jigar Joshi

Reputation: 240996

h:selectOneMenu as a value accepts collection of SelectItem and you passed String and so the Exception.

Upvotes: 2

Related Questions