JSF: Bulk update capable, but cannot update one row in datatable

I have a simple data object: Car. I am showing properties of Car objects in JSF datatable. If I display the properties using inputText tags, I can get the changed values ​​in the managed bean. However, I just want one line to be editable. Therefore, we placed the edit button in a separate column and inputText and outputText for each Car property. the edit button just toggles rendering of inputText and outputText. Also, I have placed the refresh button in a separate column that is used to save the updated values. However, when I click the refresh button, I still get the old values ​​instead of the changed values. Here's 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 page 2:

<?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 changed values ​​in the update method. How can I get the modified values ​​to edit one line?

+2


a source to share


2 answers


I am getting changed values ​​in the update method when the scope changes from request to session. Is there a better approach instead of using SessionScoped? SessionScoped == Server memory, so I want to avoid it.



Rant (or disappointment?): Indeed, if there was something similar to the asp.net ObjectDataSource it would be much easier. Even to moderately work with data related to data, we have to look for a life cycle. Whereas in asp.net ObjectDataSource + ListView the combo is enough for most data + UI questions and we hardly have to look for the control lifecycle (Infact, the asp.net guys almost never look for docs). Why use the underlying JSF specification for a control capable of doing CRUD operations out of the box, possibly with lazy loading capability. is not required for every application?

+1


a source


JSF has already updated the model values ​​for you. Just use it in 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, creating model properties is not private

a bad idea. You have to make them private

and access them using getters / setters. That is car.getBrand()

, car.getColor()

etc.

+1


a source







All Articles