Primefaces数据表选择行和范围

时间:2013-06-05 09:05:30

标签: java jsf primefaces datatable

我使用PrimeFaces 3.1,我必须在数据表中选择一行。 我使用了Primefaces展示中的示例来执行此操作,但只有当我的支持bean在范围会话中而不是在视图中时它才有效,这意味着当用户返回此页面时我必须执行一些额外的工作来删除它,我的代码有什么问题? 我的控制器(在请求范围内):

@ManagedBean
@RequestScoped
public class Ctrlr implements Serializable{
    @ManagedProperty(value = "#{myDataModel}")
    private MyDataModel dataModel;
    ...
    public void onSelectRow() {
    //do something
    }
}

backingBean(我必​​须使用会话范围才能正常工作):

@ManagedBean
@ViewScoped
public class MyDataModel extends ListDataModel<Bean> implements SelectableDataModel<Bean>, Serializable {
    ...
}

我的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"/>
        ...

我在调试中检查过,似乎每次重建控制器(每个请求)时,注入的属性都是新属性,而不是从viewScope重新注入一个属性。

如果有人可以帮我避免使用会话范围?

1 个答案:

答案 0 :(得分:2)

我认为你可能会过多地关注本教程。

见下面一个简单的工作示例:

汽车类

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)

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;
    }

    }

<强> 视图(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

如果您需要整个项目作为WAR / Zip文件让我知道。