JSF复选框值未发送到bean

时间:2017-06-02 15:56:33

标签: jsf richfaces javabeans

虽然视图可以从bean获取所有值,但是当切换复选框(h:selectBooleanCheckbox)时,bean不会更新。 setSelected()中的Item方法永远不会从视图中调用。

我已经尝试将bean的范围更改为application,使用map而不是selected属性的值以及编写我自己的ajax javascript函数的愚蠢尝试。

我正在使用的应用程序有点遗留,所以我使用的是Tomcat 6,JSF 1.2和Richfaces 3.3.3.Final。

看起来它应该很简单,我想我错过了一些明显的东西,但我已经在这两天了,并且无法弄清楚为什么bean没有更新。

我的观点是:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:c="http://java.sun.com/jstl/core"
    xmlns:fn="http://java.sun.com/jsp/jstl/functions"
    xmlns:a4j="http://richfaces.org/a4j"
    xmlns:rich="http://richfaces.org/rich">
<head>
<title>JSF</title>
</head>
    <body>
        <h:form id="tableForm">
            <rich:dataTable id="table" value="#{managedBean}" var="item" width="100%">
                <rich:column label="Select" align="center" width="40px" >
                    <f:facet name="header">
                        <h:outputText value="Select" />
                    </f:facet>
                    <h:selectBooleanCheckbox value="#{item.selected}" >
                        <a4j:support execute="@this" event="onclick" ajaxSingle="true"/>
                    </h:selectBooleanCheckbox>
                </rich:column>
                <rich:column label="Name" align="center" width="40px">
                    <f:facet name="header">
                        <h:outputText value="Name" />
                    </f:facet>
                    <h:outputText value="#{item.name}" />
                </rich:column>
            </rich:dataTable>
        </h:form>
    </body>
</html>

我在会话范围内有一个托管bean ItemBean:

import org.richfaces.model.ExtendedTableDataModel;
public class ItemBean extends ExtendedTableDataModel<Item>{
    public ItemBean() {super(new ItemProvider());}
}

ItemProvider是:

import org.richfaces.model.DataProvider;

public class ItemProvider implements DataProvider<Item>{

    private List<Item> items = new ArrayList<Item>();

    public ItemProvider(){
        for(int i = 0; i < 10; i++){
            items.add(new Item(i, "Item "+i));
        }
    }

    public Item getItemByKey(Object key) {return items.get((Integer)key);}

    public List<Item> getItemsByRange(int fromIndex, int toIndex) {
        return new ArrayList<Item>(items.subList(fromIndex, toIndex));
    }

    public Object getKey(Item arg0) {return arg0.getId();}

    public int getRowCount() {return items.size();}
}

和项目是:

public class Item {
    private final int id;
    private final String name;
    private boolean selected;

    public Item(int id, String name) {
        this.id = id;
        this.name = name;
        selected = false;
    }

    public int getId(){
        return id;
    }

    public String getName(){
        return name;
    }

    public boolean isSelected(){
        return selected;
    }

    public void setSelected(boolean selected){
        this.selected = selected;
    }
}

2 个答案:

答案 0 :(得分:1)

我认为,您的香草htmlheadbody标签可能会导致此问题。

Ajax-stuff(javascript)有时必须进入页面的<head>部分(以及必需的脚本引用)。因为JSF正在使用组件,所以在将组件添加到底层的Object-Model时,需要插入特定组件所需的javascript。

因此,JSF本身需要管理htmlheadbody标记。

因此,不要将它们创建为vanilla-html,而是使用正确的<h:html><h:body><h:head>标记,这会将此任务移交给JSF。这样,应该生成onclick-event处理程序所需的javascript代码,并且复选框应该按预期工作。

答案 1 :(得分:0)

问题是我没有正确设置我的web.xml。

我错过了将请求路由到richfaces的以下内容:

  <filter>
  <display-name>RichFaces Filter</display-name>
  <filter-name>richfaces</filter-name>
  <filter-class>org.ajax4jsf.Filter</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>richfaces</filter-name>
  <servlet-name>Faces Servlet</servlet-name>
  <dispatcher>REQUEST</dispatcher>
  <dispatcher>FORWARD</dispatcher>
  <dispatcher>INCLUDE</dispatcher>
  </filter-mapping>