سؤال

Okay, I am not sure why it is saying that my target is not a collection or array because I am trying to put the results of a selectManyCheckbox into an integer array. It is simply a checkbox of 1-50 where the user can select multiple numbers to be stored in an array and displayed later but I keep getting the error message "Target model Type is no a Collection or Array" error reported. Is this something that needs to be saved in an Object array instead of an integer array? I know there are other threads dealing with this same issue but the others that I saw was generally someone using the wrong type of checkbox or someone not storing in an array or collection. Any help would be really appreciated.

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

And myLottoBean class...

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);
}
}
هل كانت مفيدة؟

المحلول

You selectManyCheckobox value is #{lottoBean.numbersPicked}. This means that you should have property named numbersPicked which is Collection or Array. Example with array:

private int[] numbersPicked;

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

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

You should have this in your backing bean. Problem is that your getter method returns String.

نصائح أخرى

I ended up using a LinkedHashMap, that way if I ever need something similar that uses anything other than integers it will be easy to plug items in.

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

}

index...

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

and next...

<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>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top