1

我无法从 View 范围内的 bean 表中获取所选值。以下代码不起作用,但当我使用 Session 或 Request 范围时它起作用。

这是我的 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.
}
4

1 回答 1

1

为数据表添加适当的rowKey属性。Primefaces 必须有一种方法来唯一标识您的行。这就是为什么您应该设置一个对您的数据集唯一的属性。通常这是数据库主键。例如,在您的情况下,它可能是rowKey=#{data.number}.

于 2013-01-04T15:14:56.703 回答