SelectManyCheckbox找不到其子组件

时间:2015-02-22 20:17:04

标签: jsf jsf-1.2

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

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

}

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

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

即使我尝试过静态儿童,也不会填充它们。

你能帮忙吗

1 个答案:

答案 0 :(得分:0)

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

  

指向ListI或SelectItem数组的值绑定表达式   包含这些选项信息的实例。

您还会在SelectItems中返回selectManyCheckbox的值,这是没有意义的,您应该更多地了解如何使用SelectItems。您可以在selectOneMenu wiki page中找到许多与selectManyCheckbox非常相似的示例,或者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;

}

Finnaly,也许现在是时候考虑迁移到JSF 2.0了,它可以让你使用facelets而不是JSP,从Ajax支持中获益......对于明确的比较,请参阅:What are the main disadvantages of Java Server Faces 2.0?