0

我的问题是我无法在简单的 Jsf 应用程序中呈现 h:selectManyCheckbox 组件。尽管 h:selectBooleanCheckbox 和 h:commandButton 在输出中正常显示,但 selectManyCheckbox 不会。什么缺少以下代码来渲染组件?

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:ui="http://java.sun.com/jsf/facelets">

<h:form>
    <h:selectManyCheckbox value="#{hello.customers}"></h:selectManyCheckbox><br />
    <h:selectBooleanCheckbox value="#{hello.foo}" /><br/>
    <h:commandButton action="response.xhtml" value="Click me" />
</h:form>
</html>

豆类:

@ManagedBean
@SessionScoped
public class Hello implements Serializable{

private static final long serialVersionUID = 1L;
private List<String> customers;
private boolean foo;

public Hello(){
    customers = new ArrayList<String>();
    customers.add("Cust1");
    customers.add("Cust3");
    customers.add("Cust2");
    customers.add("Cust4");
    //foo = true;
}

public List<String> getCustomers() {
    return customers;
}

public boolean isFoo() {
    return foo;
}

}
4

1 回答 1

2

检查下面的修改代码

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">

<h:form>
 <h:selectManyCheckbox value="#{hello.customerSelected}">
    <f:selectItems value="#{hello.customers}" />
</h:selectManyCheckbox><br />
<h:selectBooleanCheckbox value="#{hello.foo}" /><br/>
<h:commandButton action="response.xhtml" value="Click me" />
</h:form>
</html>

在 Bean 类中再添加一个字段来存储检查元素的结果。

@ManagedBean
@SessionScoped
public class Hello implements Serializable {

private static final long serialVersionUID = 1L;
public String[] customerSelected;
private List<String> customers;
private boolean foo;

public Hello() {
    customers = new ArrayList<String>();
    customers.add("Cust1");
    customers.add("Cust3");
    customers.add("Cust2");
    customers.add("Cust4");
    // foo = true;
}

public List<String> getCustomers() {
    return customers;
}

public boolean isFoo() {
    return foo;
}

String[] getCustomerSelected() {
    return customerSelected;
}

void setCustomerSelected(String[] customerSelected) {
    this.customerSelected = customerSelected;
}

}
于 2013-03-05T10:26:54.533 回答