SelectManyCheckbox 没有找到它的子组件

SelectManyCheckbox does not find its child components

我需要你的帮助来解决日志中的错误。在我的 jsp 中,我有 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>

和我的豆子:

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

}

日志显示错误并且 jsp 中没有显示复选框:

java.lang.IllegalArgumentException:组件类型 javax.faces.SelectMany(j_id_id2) 的子组件类型应为 UISelectItem/UISelectItems。找到 [Ljava.lang.String

即使我尝试了静态子项,也没有填充它们。

那你能帮忙吗

这是因为您的 SelectItems 值为 String[],请参阅 java docs 关于 JSF 1.2 中 SelectItems 的 value 属性:

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

您还在 SelectItems 中返回了 selectManyCheckbox 的值,这没有意义,您最好多了解一下如何使用 SelectItems。您可以在 selectOneMenu wiki page which is very similar to the selectManyCheckbox, Or in The Java EE 6 Tutorial 中找到许多示例(请注意,此链接是 JSF 2.0,但这可能有助于您理解概念)。

关于你的例子,应该是这样的:

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;

}

最后,也许您是时候考虑迁移到 JSF 2.0 了,它可以让您使用 facelets 而不是 JSP,受益于 Ajax 支持...有关清晰的比较,请参见: What are the main disadvantages of Java Server Faces 2.0?