带有错误 java.lang.String 的 selectonemenu cannot be cast to javax.faces.model.SelectItem

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6490883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:21:36  来源:igfitidea点击:

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

javajsf

提问by rym

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

我想填写一个 selectonemenu 但我总是有这个错误:

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:

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

谢谢你

采纳答案by Maddy

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

不要将 value 属性用于不同的目的

回答by Jigar Joshi

h:selectOneMenuas a valueaccepts collection of SelectItemand you passed Stringand so the Exception.

h:selectOneMenu作为一个value接受集合,SelectItem你通过了String,所以异常。

回答by Aditzu

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

PS虽然是一个迟到的答案,但我将其发布给将面临同样问题的其他人