tags:

views:

211

answers:

2

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"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"&gt;
    <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?

+1  A: 

I get the modified values in update method when i change the scope from request to session. Is there a better approach instead of using SessionScoped? SessionScoped == Server Memory,that's why i want to avoid it.

Rant(or frustration?): Really if there was something similar to ObjectDataSource of asp.net, it would have been so much easier. Even for moderate work with datatable, we have to lookup lifecycle issues. Whereas in asp.net ObjectDataSource + ListView combo is enough for most of data + UI related matters plus hardly we have to lookup lifecycle of a control(Infact, asp.net guys hardly ever lookup the docs). Why cant the base JSF specification provide for a control capable of CRUD operations out of the box with perhaps lazy loading capability. isn't it required for each and every application?

nash
+1  A: 

JSF has already updated the model values for you. Just access it the usual way.

public String update() {
    for (Car car : carList) {
        if (/* this item was set to editmode */) {
            System.out.println("new car brand is:" + car.brand);
            System.out.println("new car color is:" + car.color);
        }
    }

    return null;
}

By the way, making model properties non-private is a bad idea. You should make them private and access them by getters/setters. I.e. car.getBrand(), car.getColor() and so on.

BalusC