我有一个数据表,每一行都有“编辑”和“删除”按钮。
它几乎类似于http://www.primefaces.org/showcase-labs/ui/datatableRowSelectionByColumn.jsf数据表。

单击“编辑”按钮时,如果使用<h:outputText>显示选定的行值,则这些值将正确显示。
但是,如果我想使用<h:inputText value="#{userCRUDMB.selectedUser.userName}"/>在输入文本字段中显示它,则会抛出错误,提示selectedUser is resolved as null

如果我仅使用<h:outputText ...>,则userCRUDMB.selectedUser()方法将被正确调用。
但是,如果我在对话框中使用<h:inputText ...>,则根本不会调用setter方法。

我正在使用Mojarra 2.1.6,PrimeFaces 3.0,Apache Tomcat7.0.32。

知道为什么会这样吗?

代码:代码与http://www.primefaces.org/showcase-labs/ui/datatableRowSelectionByColumn.jsf相同,除了在对话框中而不是使用来显示文本外,我正在尝试使用在输入文本框中显示文本。

public class User
{
    private Integer userId;
    private String userName;
    private String password;
    private String firstname;
    private String lastname;
    private String email;
    private Date dob;
    private String gender;

    //setters/getters
}



package com.sivalabs.primefacesdemo.managedbeans;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import com.sivalabs.primefacesdemo.model.User;
import com.sivalabs.primefacesdemo.model.UserDataModel;
import com.sivalabs.primefacesdemo.service.SpringContainer;
import com.sivalabs.primefacesdemo.service.UserService;


@ManagedBean(name="UserCRUDMB")
@RequestScoped
public class UserCRUDMB
{

    private UserDataModel userDataModel;
    private User selectedUser;
    private User[] selectedUsers;

    public UserCRUDMB()
    {
        List<User> users = new ArrayList<User>();
        for (int i = 0; i < 15; i++) {
            User user = new User();
            user.setUserId(i);
            user.setUserName("userName"+i);
            users.add(user);
        }
        this.userDataModel = new UserDataModel(users);
    }

    public UserDataModel getUserDataModel() {
        return userDataModel;
    }

    public void setUserDataModel(UserDataModel userDataModel) {
        this.userDataModel = userDataModel;
    }

    public User[] getSelectedUsers() {
        return selectedUsers;
    }

    public void setSelectedUsers(User[] selectedUsers) {
        this.selectedUsers = selectedUsers;
    }

    public User getSelectedUser() {
        System.out.println("get-->"+selectedUser);
        return selectedUser;
    }

    public void setSelectedUser(User selectedUser) {
        System.out.println("set--->"+selectedUser);
        this.selectedUser = selectedUser;
    }
}



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

<h:head>
</h:head>
<body>
    <h:form id="form">
        <h:outputText value="PrimeFaces Demo - ShowUsers" />

        <p:dataTable value="#{UserCRUDMB.userDataModel}" var="userObj"
                     selection="#{UserCRUDMB.selectedUsers}"
                     widgetVar="usersTbl">

            <f:facet name="header">UserManagement</f:facet>
            <p:column selectionMode="multiple"></p:column>
            <p:column headerText="UserId">#{userObj.userId}</p:column>
            <p:column headerText="UserName">#{userObj.userName}</p:column>

            <p:column>
                <p:commandButton value="Edit"
                                 oncomplete="userEditDlg.show()"
                                 update="form:userEditTbl">
                    <f:setPropertyActionListener target="#{UserCRUDMB.selectedUser}" value="#{userObj}"></f:setPropertyActionListener>
                </p:commandButton>

            </p:column>
            <p:column>
                <p:commandButton value="Delete" action="#{UserCRUDMB.deleteUser}"
                                 update="usersTbl"
                                 ajax="true">
                    <f:setPropertyActionListener target="#{UserCRUDMB.selectedUser}" value="#{userObj}"></f:setPropertyActionListener>
                </p:commandButton>

            </p:column>

            <f:facet name="footer">
                <p:commandButton value="Delete Selected" action="#{UserCRUDMB.deleteUsers}"
                                 update="usersTbl"
                                 ajax="true">
                </p:commandButton>
            </f:facet>
        </p:dataTable>
        <p:dialog widgetVar="userEditDlg" header="User Edit Form" hideEffect="explode"
                                minHeight="200" minWidth="300">
            <h:panelGrid columns="2" id="userEditTbl" >
                <h:outputLabel value="UserId" />
                <h:outputText value="#{UserCRUDMB.selectedUser.userId}"/>

                <h:outputLabel value="UserName" />
                <h:inutText value="#{UserCRUDMB.selectedUser.userName}"/>

            </h:panelGrid>

        </p:dialog>
    </h:form>

</body>
</html>


谢谢,
西瓦

最佳答案

如果使用的是Ajax,则请求范围不是“理想”范围,因为将为每个请求重新创建bean。这可能是selectedUser为空的原因。请改用视图范围(@ViewScoped)。

09-11 09:44