Selectonemenu with java.lang.String error cannot be dropped in javax.faces.model.SelectItem

I want to fill selectonemenu, but always 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 found that I should write a converter, but I do not know why? beacause I saw an example of working without a converter ??

Thank you

+6
source share
3 answers

Try using this code on your web page.

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

Do not use the value attribute to assign it for different purposes.

+19
source

The answer is a bit 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> 

PS Although this is a late answer, I posted it for others who run into the same problem.

+3
source

h:selectOneMenu as value takes a collection of SelectItem , and you passed String and therefore an Exception.

+2
source

Source: https://habr.com/ru/post/891432/


All Articles