java Primefaces 数据表选择行和范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16935776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:25:46  来源:igfitidea点击:

Primefaces datatable select row and scope

javajsfprimefacesdatatable

提问by 893

I use PrimeFaces 3.1 and I have to select one single row in a datatable. 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? My controller (in request scope):

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

@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?

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

回答by Makky

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(视图范围)

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

输出

Output

输出

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

如果您需要整个项目作为 WAR/Zip 文件,请告诉我。