选中项时,selectManyCheckbox元素不会复制到数组

时间:2013-03-13 01:25:31

标签: java arrays jsf facelets selectmanycheckbox

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

<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>

myLottoBean类......

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);
}
}

2 个答案:

答案 0 :(得分:1)

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

private int[] numbersPicked;

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

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

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

答案 1 :(得分: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>