0

我使用 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 重新注入一个。

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

4

1 回答 1

2

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)

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)

<!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

Output

If you need the whole project as WAR/Zip file let me knw.

于 2013-06-05T12:15:45.320 回答