JSF : able to do mass update but unable to update a single row in a datatable

Posted by nash on Stack Overflow See other posts from Stack Overflow or by nash
Published on 2010-04-29T06:07:07Z Indexed on 2010/04/29 6:17 UTC
Read the original article Hit count: 416

Filed under:
|

I have a simple data object: Car. I am showing the properties of Car objects in a JSF datatable. If i display the properties using inputText tags, i am able to get the modified values in the managed bean. However i just want a single row editable. So have placed a edit button in a separate column and inputText and outputText for every property of Car. the edit button just toggles the rendering of inputText and outputText. Plus i placed a update button in a separate column which is used to save the updated values. However on clicking the update button, i still get the old values instead of the modified values. Here is the complete code:

public class Car {

    int id;
    String brand;
    String color;

    public Car(int id, String brand, String color) {
        this.id = id;
        this.brand = brand;
        this.color = color;
    }

    //getters and setters of id, brand, color
}

Here is the managed bean:

import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIData;

@ManagedBean(name = "CarTree")
@RequestScoped
public class CarTree {

    int editableRowId;
    List<Car> carList;
    private UIData myTable;

    public CarTree() {

        carList = new ArrayList<Car>();
        carList.add(new Car(1, "jaguar", "grey"));
        carList.add(new Car(2, "ferari", "red"));
        carList.add(new Car(3, "camri", "steel"));
    }

    public String update() {

        System.out.println("updating...");
        //below statments print old values, was expecting modified values
        System.out.println("new car brand is:" + ((Car) myTable.getRowData()).brand);
        System.out.println("new car color is:" + ((Car) myTable.getRowData()).color);


        //how to get modified row values in this method??

        return null;
    }

    public int getEditableRowId() {
        return editableRowId;
    }

    public void setEditableRowId(int editableRowId) {
        this.editableRowId = editableRowId;
    }

    public UIData getMyTable() {
        return myTable;

    }

    public void setMyTable(UIData myTable) {
        this.myTable = myTable;
    }

    public List<Car> getCars() {
        return carList;
    }

    public void setCars(List<Car> carList) {
        this.carList = carList;
    }
}

here is the JSF 2 page:

<?xml version='1.0' encoding='UTF-8' ?>
<!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:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>

        <h:form id="carForm" prependId="false">
            <h:dataTable id="dt" binding="#{CarTree.myTable}" value="#{CarTree.cars}" var="car" >
                <h:column>
                    <h:outputText value="#{car.id}" />
                </h:column>

                <h:column>
                    <h:outputText value="#{car.brand}" rendered="#{CarTree.editableRowId != car.id}"/>
                    <h:inputText value="#{car.brand}" rendered="#{CarTree.editableRowId == car.id}"/>
                </h:column>

                <h:column>
                    <h:outputText value="#{car.color}" rendered="#{CarTree.editableRowId != car.id}"/>
                    <h:inputText value="#{car.color}" rendered="#{CarTree.editableRowId == car.id}"/>
                </h:column>

                <h:column>
                    <h:commandButton value="edit">
                        <f:setPropertyActionListener target="#{CarTree.editableRowId}" value="#{car.id}" />
                    </h:commandButton>
                </h:column>

                <h:column>
                    <h:commandButton value="update" action="#{CarTree.update}"/>
                </h:column>

            </h:dataTable>
        </h:form>
    </h:body>
</html>

However if i just keep the inputText tags and remove the rendered attributes, i get the modified values in the update method. How can i get the modified values for the single row edit?

© Stack Overflow or respective owner

Related posts about java

Related posts about jsf2