0

好的,我不确定为什么说我的目标不是集合或数组,因为我试图将 selectManyCheckbox 的结果放入整数数组中。它只是一个 1-50 的复选框,用户可以在其中选择多个数字以存储在数组中并稍后显示,但我不断收到错误消息“目标模型类型不是集合或数组”错误报告。这是需要保存在对象数组而不是整数数组中的东西吗?我知道还有其他线程处理同样的问题,但我看到的其他线程通常是使用错误类型的复选框或未存储在数组或集合中的人。任何帮助将非常感激。

<p>
        Pick Your Lotto Numbers
        <h:selectManyCheckbox value="#{lottoBean.numbersPicked}"
                 layout="lineDirection">
        <f:selectItems value="#{lottoBean.numberChoices}"/>
        </h:selectManyCheckbox>
  </p>
  <p>
        <h:commandButton value="Submit"
                         action="next.xhtml"/>
  </p>

还有我的LottoBean 类...

int[]choices = new int[50];
int[]picked;
int[]actual;
int test = 5;

/**
 * Creates a new instance of LottoBean
 */
@SuppressWarnings("empty-statement")
public LottoBean() {
    picked = new int[6];
    actual = new int[6];
}

public void setNumbersPicked(int[] chosen)
{
    for(int i =0; i < 6; i++)
    {
        picked[i] = chosen[i];
    }
}

@SuppressWarnings("empty-statement")
public String getNumbersPicked()
{
    return Arrays.toString(picked);
}

public int[] getNumberChoices()
{
    for (int i = 0; i < 50; i++) 
    {
        choices[i] = i + 1;
    }
    return choices;
}

public String getNumbersDrawn()
{
    Random num = new Random();
    for (int i = 0; i < 6; i++)
    {
        int nextNum = num.nextInt(50);
        int number  = nextNum + 1;
        actual[i] = number;
    }
    return Arrays.toString(actual);
}
}
4

2 回答 2

1

selectManyCheckobox的价值是#{lottoBean.numbersPicked}。这意味着您应该拥有名为numbersPickedwhich is Collectionor的属性Array。数组示例:

private int[] numbersPicked;

public int[] getNumberPicked() {
  return numbersPicked;
}

public void setNumberPicked(int[] numbersPicked) {
  this.numbersPicked = numbersPicked;
}

你应该在你的 backing bean 中有这个。问题是您的 getter 方法返回String

于 2013-03-13T08:10:41.180 回答
0

我最终使用了 LinkedHashMap,这样如果我需要类似的东西,使用除整数以外的任何东西,就很容易插入项目。

public class LottoBean {

private Integer[] numbers;
private int[] actual = new int[6];
private Random generator;

private static Map<Integer,Integer> numbersName;

static
{
    numbersName = new LinkedHashMap<Integer,Integer>();
    for (int i = 1; i < 51; i++)
    {
        numbersName.put(i,i);
    } 
}

/*
 * returns Integer array
 */
public Integer[] getNumbers() 
{
    return numbers;
}

/*
 * Sets array to numbers picked
 */
public void setNumbers(Integer[] numbers) 
{
    this.numbers = numbers;
}

/**
 * Returns map 
 * @return numbersName 
 */
public Map<Integer,Integer> getNumbersName() 
{
    return numbersName;
}

/**
 * Returns the numbers array
 * @return the numbers array in string form
 */
public String getNumbersNameArray()
{
    return Arrays.toString(numbers);
}

/**
 * Generates numbers drawn
 * @return actual the numbers drawn
 */
public String getActualNumbersArray()
{ 

    for (int i = 0; i < 6; i++)
    {
        generator = new Random();
        int number = 1 + generator.nextInt(50);
        actual[i] = number;
    }
    return Arrays.toString(actual);
}

/**
 * Compares numbers picked to numbers drawn
 * @return the number of matching numbers
 */
public int getSame()
{
    int same = 0;  
      for (int i = 0; i < numbers.length; i++) 
      {  
         for(int j = 0; j < actual.length; j++) 
         {  
            if (numbers[i] == actual[j]) 
            {  
               same++;
            }
         }
      }
      return same;
}

}

指数...

<head>
<title>Lotto Numbers</title>
</head>
<body>
<h:form> 
<p>
    Pick six numbers from the list:
</p>
<h:selectManyCheckbox id="chkbox1" value="#{lottoNumbers.numbers}"
required="true" requiredMessage="check exactly six numbers" 
layout="lineDirection" border="1" readonly="false">
<f:selectItems value="#{lottoNumbers.numbersName}"/>
</h:selectManyCheckbox>
<h:message for="chkbox1" style="color:red"/>
<br/><br/>
<h:commandButton value="submit" action="next" />
</h:form>
</body>
</html>

接下来...

<head>
<title>Lotto Numbers</title>
</head>
<body>
<h:form>
<p>
    <h:outputText value="You have selected : #{lottoNumbers.numbersNameArray}" />
</p>
    <h:outputText value="Actual numbers    : #{lottoNumbers.actualNumbersArray}" />
<p>
    <h:inputText value="Matching numbers   : #{lottoNumbers.same}" /> 
</p>
</h:form>
</body>
</html>
于 2013-03-13T18:12:39.950 回答