简体   繁体   中英

Why JSF2(PrimeFaces3) Ajax Property Listener is not triggering?

I have a datatable with each row having Edit and Delete buttons. It is almost similar to http://www.primefaces.org/showcase-labs/ui/datatableRowSelectionByColumn.jsf datatable.

When I click on Edit button, if I display the selected row values using <h:outputText> the values are being displayed properly. But If I want to display it in a input text field using <h:inputText value="#{userCRUDMB.selectedUser.userName}"/> it is throwing error saying selectedUser is resolved as null .

If I only use <h:outputText ...> then userCRUDMB.selectedUser() method is getting called properly. But If I use <h:inputText ...> in the Dialog, the setter method is not at all getting called.

I am using Mojarra 2.1.6, PrimeFaces 3.0, Apache Tomcat7.0.32.

Any idea why it is happening?

Code: Code is same as http://www.primefaces.org/showcase-labs/ui/datatableRowSelectionByColumn.jsf , except in the dialogue box instead of displaying the text using I am trying to display in input textbox using .

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>

Thanks, Siva

If you are using ajax, the request scope ist not the "ideal" scope since the bean will be recreated for each request. This can be the reason that selectedUser is null. Use the view scope ( @ViewScoped ) instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM