简体   繁体   English

JSF-Selectonemenu数组int

[英]JSF - Selectonemenu array int

i want to set value in array but receive Classcastexception.. 我想在数组中设置值,但收到ClasscastException。

example of code: page.xhtml 代码示例:page.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:p="http://primefaces.org/ui">
<h:head>
    <title>Really simple CRUD</title>
</h:head>
<h:body>
    <h:form id="form">
        <p:growl autoUpdate="true" showDetail="true"/>
        <h:selectOneMenu converter="javax.faces.Integer" value="#{adminMBean.a[0]}">
            <f:selectItem itemLabel="1" itemValue="1"/>
            <f:selectItem itemLabel="2" itemValue="2"/>
        </h:selectOneMenu>
        <h:commandButton value="ok" action="#{adminMBean.ok()}"/>
        <h:outputText value="sa:#{adminMBean.a[0]}"/>
    </h:form>
</h:body>

@ManagedBean(name = "adminMBean")
@RequestScoped
public class AdminMBean {
  int[] a=new int[1];
public AdminMBean(){}
    public int[] getA() {
    return a;
}
public void setA(int[] a) {
    this.a = a;
}
}

how to set value into array with selectonemenu? 如何使用selectonemenu将值设置为数组?

Have you read the ClassCastException message? 阅读ClassCastException消息吗? It should be something like this: 应该是这样的:

java.lang.ClassCastException: Unable to add an object of type [java.lang.Integer] to an array of objects of type [int] java.lang.ClassCastException:无法将类型为[java.lang.Integer]的对象添加到类型为[int]的对象的数组中

See, you should be using Integer[] instead of int[] . 看,您应该使用Integer[]而不是int[]

private Integer[] a = new Integer[1];

public Integer[] getA() {
    return a;
}

The setter is by the way unnecessary. 顺便说一句,设置者是不必要的。 It wouldn't be used in this construct. 它不会在此构造中使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM