我无法从视图范围内bean的表中获取所选值。下面的代码不工作,但它工作时,我使用会话或请求作用域。
这是我的JSF页面:<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:p="http://primefaces.org/ui">
<f:view>
<h:head>
<title>Sample JSF/Prime Page</title>
</h:head>
<h:body>
<h:form id="primeForm">
<p:panel id="toggle1" header="Panel 1" collapsed="#{prime.toggle1}" footer=" footer Info if required " toggleable="true" closable="true" >
<p:dataTable id="cars" value="#{prime.listModel}" var="data" paginator="true" style="width:500px"
selection="#{prime.selected}" selectionMode="single">
<p:ajax event="rowSelect" listener="#{prime.onRowSelect}" />
<f:facet name="header">
Select a row to display a message
</f:facet>
<p:column headerText="Model">
#{data.number}
</p:column>
<p:column headerText="Model">
#{data.number}
</p:column>
</p:dataTable>
</p:panel>
</h:form>
</h:body>
</f:view>
</html>
这是我的豆子:
@ManagedBean
@ViewScoped
public class Prime implements Serializable {
ArrayList<UIData> list = new ArrayList<UIData>();
private UIData selected;
private UIDataModel listModel;
private String testVaue;
private ArrayList<String> tmp = new ArrayList<String>();
private HashMap<String, List<String>> pagesMap = new HashMap<String, List<String>>();
private boolean toggle1 = true;
public Prime() {
for (int k = 1; k < 3; k++) {
UIData model =new UIData(""+k, ""+k);
list.add(model);
}
listModel = new UIDataModel(list);
}
public void onRowSelect(SelectEvent event) {
try{
System.out.println("Sellected Value : "+((UIData)event.getObject()).getNumber());
}catch (Exception e) {
e.printStackTrace();
}
}
// Getters and setters.
}
为datatable添加合适的rowKey
属性。主面孔必须有一种唯一的方式来识别你。这就是为什么你应该为你的数据集设置唯一的属性。通常这是数据库主键。在你的例子中,可以是rowKey=#{data.number}