2017-06-02 76 views
0

尽管视图可以从bean中获取所有值,但当复选框(h:selectBooleanCheckbox)被切换时,bean不会更新。永远不会从视图中调用Item中的setSelected()方法。JSF复选框值未发送到bean

我试着将bean的作用域更改为应用程序,使用地图而不是所选属性的值以及编写我自己的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; 
    } 
} 

回答

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> 
1

我会假设,你的香草html,headbody标签可能会导致这种情况。

Ajax-stuff(javascript)有时必须进入页面的<head>部分(以及所需的脚本引用)。由于JSF正在使用组件,因此它需要在组件添加到下层Object-Model时插入某个组件所需的JavaScript。

因此JSF本身需要管理html,headbody标签。

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

+0

感谢您的答复,不幸的是,问题是,我已经错过了在web.xml配置。在JSF中工作时,在您的答案中使用标记是否是最佳做法?看起来好像是这样。 – Sam