使用f:setPropertyActionListener将对象从JSF页面传递到您的后端。当您使用可重复使用的组件(例如数据表)时,此标签特别有用
无需使用原始Javascript,可以使用
这是一个示例示例:
汽车类:
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; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; }}
CarTree类:
import java.util.ArrayList;import java.util.List;import javax.faces.bean.ManagedBean;import javax.faces.bean.RequestScoped;@ManagedBean(name = "CarTree")@RequestScopedpublic class CarTree { List<Car> carList; Car selectedCar; public Car getSelectedCar() { return selectedCar; } public void setSelectedCar(Car selectedCar) { this.selectedCar = selectedCar; } public List<Car> getCars() { return carList; } public void setCars(List<Car> carList) { this.carList = carList; } 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")); }}
JSF页面:
<?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 id="mainBody"> <h:form id="carForm"> <h:dataTable value="#{CarTree.cars}" var="car"> <h:column> <h:outputText value="#{car.id}"/> </h:column> <h:column> <h:outputText value="#{car.brand}"/> </h:column> <h:column> <h:outputText value="#{car.color}"/> </h:column> <h:column> <h:commandButton value="Show Car Detail" > <f:setPropertyActionListener target="#{CarTree.selectedCar}" value="#{car}"/> <f:ajax render=":carForm:carDetails" /> </h:commandButton> </h:column> </h:dataTable> <h:panelGroup id="carDetails" layout="block" > <h:outputText value="#{CarTree.selectedCar.id}" /> <h:outputText value="#{CarTree.selectedCar.brand}" /> <h:outputText value="#{CarTree.selectedCar.color}" /> </h:panelGroup> </h:form> </h:body></html>
希望这可以帮助。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)