0

I need your help is solving the error in the log. In my jsp, I am having selectmanycheckbox:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page deferredSyntaxAllowedAsLiteral="true" %> 
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
        <h:form>
            <h:selectManyCheckbox value="#{com.favoriteCar2}">
                <f:selectItems value="#{com.favoriteCar2Value}" />

        </h:selectManyCheckbox>

        <br/>


        <h:selectManyCheckbox value="#{com.favoriteCar3}">
            <f:selectItems value="#{com.favoriteCar3Value}" />
        </h:selectManyCheckbox>

        <h:commandButton value="Submit" action="results" />
        <h:commandButton value="Reset" type="reset" />

    </h:form>

and mybean:

    import java.io.Serializable;
    import java.util.Arrays;
    import java.util.LinkedHashMap;
    import java.util.Map;

    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.SessionScoped;


@ManagedBean
@SessionScoped

public class com implements Serializable{

    private static final long serialVersionUID = 7134492943336358840L;

        public String[] favoriteCar1;
                public String[] favoriteCar2;
                public String[] favoriteCar3;
                public String[] favoriteCar4;

                public String[] getFavoriteCar2Value()
                {
                        favoriteCar2 = new String [5];
                        favoriteCar2[0] = "116";
                        favoriteCar2[1] = "118";
                        favoriteCar2[2] = "X1";
                        favoriteCar2[3] = "Series 1 Coupe";
                        favoriteCar2[4] = "120";

                        return favoriteCar2;
                }

                public String getFavoriteCar2InString()
                {
                        return Arrays.toString(favoriteCar2);
                }


                private static Map<String, Object> car3Value;
                static
                {
                        car3Value = new LinkedHashMap<String, Object>();
                        car3Value.put("Car3 - 316", "BMW 316");
                        car3Value.put("Car3 - 318", "BMW 318");
                        car3Value.put("Car3 - 320", "BMW 320");
                        car3Value.put("Car3 - 325", "BMW 325");
                        car3Value.put("Car3 - 330", "BMW 330");
                }

                public Map<String, Object> getFavoriteCar3Value()
                {
                        return car3Value;
                }
                public String getFavoriteCar3InString() {
                        return Arrays.toString(favoriteCar3);
                }

}

The log is showing the error and no checkbox is shown in the jsp:

java.lang.IllegalArgumentException: Expected a child component type of UISelectItem/UISelectItems for component type javax.faces.SelectMany(j_id_id2). Found [Ljava.lang.String

Even I tried static children and it is not populating them.

So can you please help

Tarik
  • 4,961
  • 3
  • 36
  • 67
maas maas
  • 19
  • 1
  • 5

1 Answers1

0

It's because your SelectItems value is a String[], see the java docs regarding the value attribute of SelectItems in JSF 1.2 :

Value binding expression pointing at a List or array of SelectItem instances containing the information for these options.

You are also returning the value of your selectManyCheckbox in the SelectItems which doesn't make sense, you should better learn more about how to use SelectItems. You can find many examples in the selectOneMenu wiki page which is very similar to the selectManyCheckbox, Or in The Java EE 6 Tutorial (Note that this links are JSF 2.0 but that may help you to understand the concept).

Regarding your example, that should be something like this:

private List<SelectItem> favoriteCar2Value; 


//  (we will add only a getter, setter is not necessary)
public List<SelectItem> getFavoriteCar2Value() {

     favoriteCar2Value = new ArrayList<SelectItem>();

     favoriteCar2Value.add(new SelectItem("116", "116 label"));
     favoriteCar2Value.add(new SelectItem("118", "118 label"));
     favoriteCar2Value.add(new SelectItem("X1", "X1 label"));
     favoriteCar2Value.add(new SelectItem("Series 1 Coupe", "Series 1 Coupe label"));
     favoriteCar2Value.add(new SelectItem("120", "120 label"));

     return favoriteCar2Value;

}

Finnaly, maybe it's time for you to consider migrating to JSF 2.0 which may let you working with facelets instead of JSP, benefit from Ajax support... For a clear comparative see: What are the main disadvantages of Java Server Faces 2.0?

Community
  • 1
  • 1
Tarik
  • 4,961
  • 3
  • 36
  • 67