私は単純なデータオブジェクトを持っています:車。JSFデータテーブルでCarオブジェクトのプロパティを表示しています。inputTextタグを使用してプロパティを表示すると、管理対象Beanで変更された値を取得できます。ただし、1行を編集可能にしたいだけです。そのため、編集ボタンを別の列に配置し、Carのすべてのプロパティに対してinputTextとoutputTextを配置しました。編集ボタンは、inputTextとoutputTextのレンダリングを切り替えるだけです。さらに、更新された値を保存するために使用される別の列に更新ボタンを配置しました。ただし、更新ボタンをクリックしても、変更された値ではなく古い値が表示されます。完全なコードは次のとおりです。
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
}
管理対象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;
}
}
これがJSF2ページです。
<?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>
ただし、inputTextタグを保持し、レンダリングされた属性を削除すると、updateメソッドで変更された値を取得します。単一行編集の変更された値を取得するにはどうすればよいですか?