JSF h:selectOneMenu 问题:java.lang.IllegalArgumentException 无法将类 java.lang.String 类型的“字符串”转换为接口 java.util.List

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9786917/
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 07:39:17  来源:igfitidea点击:

JSF h:selectOneMenu trouble: java.lang.IllegalArgumentException Cannot convert "string" of type class java.lang.String to interface java.util.List

javajsfselectonemenu

提问by Rob

I have some JSF-trouble using h:selectOneMenu with a list from my backend bean: My xhtml file looks like this:

我在使用 h:selectOneMenu 和后端 bean 中的列表时遇到了一些 JSF 问题:我的 xhtml 文件如下所示:

  <f:view>
    <h:form id="serverOptions">
      <h:selectOneMenu id="preset" value="#{overview.pdfPresets}" >
        <f:selectItems value="#{overview.pdfPresets}" />
      </h:selectOneMenu>
      <h:commandButton action="submit" value="Submit" />
    </h:form>
  </f:view>

where the corresponding managing bean looks like this:

相应的管理 bean 如下所示:

private List<String> pdfPresets;
private String pdfPreset;

/**
 * Returns a list of pdfPresets
 * @return a List<String> of pdf preset names
 */
public final List<String> getPdfPresets() {
    return pdfPresets;
}

/**
 * Sets the name of the selected pdfPreset
 * (trying to overload setPdfPresets here)
 * @param presetName
 * @see setPdfPreset
 */
public final void setPdfPresets(String presetName) {
    // write preset name somehwere else
    this.presetName = presetName;
}

/**
 * Sets the pdfPresets
 * @param list
 */
public final void setPdfPresets(List<String> list) {
    pdfPresets = list;
}

The problem occurs on submitting the form in my browser, the full error stack looks like this:

在我的浏览器中提交表单时出现问题,完整的错误堆栈如下所示:

EVERE: An exception occurred
javax.faces.component.UpdateModelException: java.lang.IllegalArgumentException: Cannot convert screen_druckbogen of type class java.lang.String to interface java.util.List
    at javax.faces.component.UIInput.updateModel(UIInput.java:398)
    at javax.faces.component.UIInput.processUpdates(UIInput.java:299)
    at javax.faces.component.UIForm.processUpdates(UIForm.java:187)
    at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1258)
    at javax.faces.component.UIViewRoot._processUpdatesDefault(UIViewRoot.java:1317)
    at javax.faces.component.UIViewRoot.access0(UIViewRoot.java:75)
    at javax.faces.component.UIViewRoot$UpdateModelPhaseProcessor.process(UIViewRoot.java:1419)
    at javax.faces.component.UIViewRoot._process(UIViewRoot.java:1278)
    at javax.faces.component.UIViewRoot.processUpdates(UIViewRoot.java:761)
    at org.apache.myfaces.lifecycle.UpdateModelValuesExecutor.execute(UpdateModelValuesExecutor.java:34)
    at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:171)
    at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:189)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:390)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:440)
    at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230)
    at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:943)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:756)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
Caused by: java.lang.IllegalArgumentException: Cannot convert screen_druckbogen of type class java.lang.String to interface java.util.List
    at com.sun.el.lang.ELSupport.coerceToType(ELSupport.java:397)
    at com.sun.el.parser.AstValue.setValue(AstValue.java:164)
    at com.sun.el.ValueExpressionImpl.setValue(ValueExpressionImpl.java:273)
    at org.apache.myfaces.view.facelets.el.TagValueExpression.setValue(TagValueExpression.java:117)
    at javax.faces.component.UIInput.updateModel(UIInput.java:380)
    ... 29 more

采纳答案by barsju

Try this:

尝试这个:

  <f:view>
    <h:form id="serverOptions">
      <h:selectOneMenu id="preset" value="#{overview.pdfPreset}" > <!-- typo here -->
        <f:selectItems value="#{overview.pdfPresets}" />
      </h:selectOneMenu>
      <h:commandButton action="submit" value="Submit" />
    </h:form>
  </f:view>

where the corresponding managing bean looks like this:

相应的管理 bean 如下所示:

private List<String> pdfPresets;
private String pdfPreset;

/**
 * Returns a list of pdfPresets
 * @return a List<String> of pdf preset names
 */
public final List<String> getPdfPresets() {
    return pdfPresets;
}

/**
 * Sets the name of the selected pdfPreset
 * (trying to overload setPdfPresets here)
 * @param presetName
 * @see setPdfPreset
 */
public final void setPdfPreset(String presetName) { //renamed method
    // write preset name somehwere else
    pdfPreset = presetName; //use the correct variable
}

/**
 * Sets the pdfPresets
 * @param list
 */
public final void setPdfPresets(List<String> list) {
    pdfPresets = list;
}

回答by Matt Handy

Your value attribute needs to be a Stringinstead of a List<String>because it will hold the selection. And your bean is not really a bean since it needs unique setters for the fields in order to be processed by JSF.

您的 value 属性需要是 aString而不是 aList<String>因为它将保存选择。而且您的 bean 并不是真正的 bean,因为它需要为字段设置唯一的 setter 才能由 JSF 处理。

So you should change your bean code in a way that it has a List<String>with getters and setters and a Stringfor the selection with separategetters and setters:

因此,您应该更改 bean 代码,使其具有List<String>带有 getter 和 setter的方式,以及String带有单独的getter 和 setter的用于选择的代码:

private List<String> pdfPresets;
private String selectedPdfPreset;

public List<String> getPdfPresets() {
  return pdfPresets;
}

public void setPdfPresets(List<String> pdfPresets) {
  this.pdfPresets = pdfPresets;
}

public String getSelectedPdfPreset() {
  return selectedPdfPreset;
}

public void setSelectedPdfPreset(String selectedPdfPreset) {
  this.selectedPdfPreset = selectedPdfPreset;
}

And in the view:

在视图中:

<h:selectOneMenu id="preset" value="#{overview.selectedPdfPreset}" >
        <f:selectItems value="#{overview.pdfPresets}" />
</h:selectOneMenu>