2013-03-13 40 views
0

好吧,我不知道为什么它说我的目标不是一个集合或数组,因为我试图将一个selectManyCheckbox的结果放入一个整数数组。它只是一个1-50的复选框,用户可以选择多个数字存储在数组中并稍后显示,但我一直收到错误消息“目标模型类型不是集合或数组”错误报告。这是否需要保存在Object数组中而不是整数数组中?我知道还有其他线程处理这个相同的问题,但我看到的其他线程通常是使用错误类型的复选框或某人没有存储在数组或集合中的人。任何帮助将非常感激。selectManyCheckbox元素不复制到阵列时检查项目

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

回答

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

+0

好吧,我看到后,最终改写了整个事情。我觉得这样会更容易。我想我想我有一个数组作为参数,所以它会好起来的,但显然没有奏效。至于getter方法返回一个字符串,这就是我想要的。我将发布下面现在正在工作的代码。 – 2013-03-13 18:05:28

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>