好的,我不知道为什么它说我的目标不是一个集合或数组,因为我试图把一个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>
和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);
}
}
您的selectManyCheckobox
值为#{lottoBean.numbersPicked}
。这意味着您应该拥有名为numbersPicked
的属性,即Collection
或Array
。数组的例子:
private int[] numbersPicked;
public int[] getNumberPicked() {
return numbersPicked;
}
public void setNumberPicked(int[] numbersPicked) {
this.numbersPicked = numbersPicked;
}
你应该有这个在你的后台bean。问题是你的getter方法返回String
。
我最终使用了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>