2017-06-02 60 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