[英]Primefaces datatable select row and scope
I use PrimeFaces 3.1 and I have to select one single row in a datatable. 我使用PrimeFaces 3.1,我必须在数据表中选择一行。 I used the example in the Primefaces showcase to do it but it works only when my backing bean is in scope session, not in view, and it means that I have to do some extra jobs to remove it when the user come back on this page, what is wrong in my code? 我使用了Primefaces展示中的示例来执行此操作,但只有当我的支持bean在范围会话中而不是在视图中时它才有效,这意味着当用户返回此页面时我必须执行一些额外的工作来删除它,我的代码有什么问题? My controller (in request scope): 我的控制器(在请求范围内):
@ManagedBean
@RequestScoped
public class Ctrlr implements Serializable{
@ManagedProperty(value = "#{myDataModel}")
private MyDataModel dataModel;
...
public void onSelectRow() {
//do something
}
}
The backingBean (I have to use session scope to have a correct working): backingBean(我必须使用会话范围才能正常工作):
@ManagedBean
@ViewScoped
public class MyDataModel extends ListDataModel<Bean> implements SelectableDataModel<Bean>, Serializable {
...
}
And my xhtml page: 我的xhtml页面:
<p:dataTable var="bean"
id="tableResults"
selectionMode="single"
selection="#{ctrlr.selectedBean}"
value="#{myDataModel}"
rowKey="#{bean.id}">
<p:ajax event="rowSelect"
listener="#{ctrlr.onSelectRow()}"
update=":searchForm:details:detail"/>
...
I checked in debug, and it seems that each time the controller is rebuilt (each request), the property injected is a new one, instead of re-injecting the one from the viewScope. 我检查了调试,似乎每次重建控制器(每个请求)时,注入的属性是一个新属性,而不是从viewScope重新注入一个。
If someone can help me to avoid the usage of the session scope? 如果有人可以帮我避免使用会话范围?
I think you are may be following the tutorial too much. 我想你可能会过多地关注这个教程。
See below an simple working example: 见下面一个简单的工作示例:
Car class 汽车课
package test_war.test_war;
public class Car {
private String name;
private Double price;
private int year;
public String getName() {
return name;
}
public Car(String name, Double price, int year) {
super();
this.name = name;
this.price = price;
this.year = year;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
}
BackBean (Viewscoped) BackBean(Viewscoped)
package test_war.test_war;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ManagedBean(name = "testBean")
@ViewScoped
public class TestBean implements Serializable {
private static final long serialVersionUID = 1L;
private List<Car> cars;
private Car selectedCar;
public Car getSelectedCar() {
return selectedCar;
}
public void setSelectedCar(Car selectedCar) {
this.selectedCar = selectedCar;
}
@PostConstruct
public void init() {
cars = new ArrayList<Car>();
cars.add(new Car("test1", 111.11, 2011));
cars.add(new Car("test2", 711.11, 2012));
cars.add(new Car("test3", 511.11, 2001));
cars.add(new Car("test4", 411.11, 2000));
}
public List<Car> getCars() {
return cars;
}
public void setCars(List<Car> cars) {
this.cars = cars;
}
}
View(main.xhtml) 查看(main.xhtml)
<!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"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>A Simple JavaServer Faces 2.0 View</title>
</h:head>
<h:body>
<h:form id="form">
<p:dataTable id="cars" var="car" value="#{testBean.cars}"
rowKey="#{car.name}" selection="#{testBean.selectedCar}"
selectionMode="single">
<f:facet name="header">
Click "View" button after selecting a row to see details
</f:facet>
<p:column headerText="Name">
#{car.name}
</p:column>
<p:column headerText="Year">
#{car.year}
</p:column>
<p:column headerText="Price">
#{car.price}
</p:column>
<f:facet name="footer">
<p:commandButton id="viewButton" value="View" icon="ui-icon-search"
update=":form:display" oncomplete="carDialog.show()" />
</f:facet>
</p:dataTable>
<p:dialog id="dialog" header="Car Detail" widgetVar="carDialog"
resizable="false" width="200" showEffect="clip" hideEffect="fold">
<h:panelGrid id="display" columns="2" cellpadding="4">
<h:outputText value="Name:" />
<h:outputText value="#{testBean.selectedCar.name}" />
<h:outputText value="Year:" />
<h:outputText value="#{testBean.selectedCar.year}" />
<h:outputText value="Price:" />
<h:outputText value="#{testBean.selectedCar.price}" />
</h:panelGrid>
</p:dialog>
</h:form>
</h:body>
</html>
Output 产量
If you need the whole project as WAR/Zip file let me knw. 如果您需要整个项目作为WAR / Zip文件让我知道。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.