简体   繁体   中英

JSF checkbox value not sent to bean

While the view can get all values from the bean, when a checkbox ( h:selectBooleanCheckbox ) is toggled the bean is not updated. The setSelected() method in Item is never called from the view.

I've tried changing the scope of the bean to application, using a map instead of a value for the selected property and a foolish attempt at writing my own ajax javascript function.

The application I'm working with is a bit legacy so I'm using Tomcat 6, JSF 1.2 and Richfaces 3.3.3.Final.

It seems like it should be simple, I think I'm missing something obvious, but I've been at this for two days and can't figure out why the bean isn't updated.

My view is:

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

I have a managed bean ItemBean in session scope:

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

ItemProvider is:

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

and Items are:

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

I would assume, that your vanilla html , head and body tags might cause this.

Ajax-stuff (javascript) sometimes has to go into the <head> section of the page (along with required script references). Because JSF is working with components, it needs to insert the javascript required for a certain component at the time the component is added to the underlaying Object-Model.

Therefore JSF itself needs to manage the html , head and body tags.

So, instead of creating them as vanilla-html, use the proper <h:html> , <h:body> and <h:head> tags, which will handover this task to JSF. This way, the required javascript code for your onclick-event handler should be generated and the checkbox should work as expected.

The issue was that I hadn't set up my web.xml properly.

I was missing the following which routes the request to 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM