2013-03-13 45 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>