خرید بک لینک

Vote count: 0

These are my first steps in JSF. I would like to update a p:dataTable after clicking on a button.

Here is what I have tried so far:

My xhtml page:

<!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:ui="http://java.sun.com/jsf/facelets"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:p="http://primefaces.org/ui">

<h:head></h:head>
<body>
        <h:form>
                <p:commandButton value="Go" id="ajax" update="display"
                        actionListener="#{userListController.findAllUsers()}"
                        styleClass="ui-priority-primary" />

        </h:form>
        <p:dataTable id="display" var="user" value="#{userListModel.users}">
                <p:column headerText="Useame">
                        <h:outputText value="#{user.useame}" />
                </p:column>

                <p:column headerText="Password">
                        <h:outputText value="#{user.password}" />
                </p:column>
        </p:dataTable>
</body>
</html>

My model bean:

package test;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

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

@ManagedBean
@RequestScoped
public class UserListModel implements Serializable {
    private static final long serialVersionUID = 1L;

    private List<User> users;

    @PostConstruct
    public void test() {
        users = new ArrayList<User>();
        users.add(new User("test1", "test"));
        users.add(new User("test2", "test"));
        users.add(new User("test3", "test"));
    }

//    public void setUsers(List<User> users) {
//      this.users = users;
//  }

    public List<User> getUsers() {
        retu users;
    }
}

My controller bean:

package test;

import java.io.Serializable;
import java.util.List;

import javax.aotation.PostConstruct;
import javax.enterprise.inject.Model;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.inject.Inject;

@ManagedBean
@RequestScoped
public class UserListController implements Serializable {

    private static final long serialVersionUID = 1L;

    @Inject
    private UserService userService;

    UserListModel userListModel;

    public void findAllUsers(){
        userListModel = new UserListModel();
        List<User> allUsers = userService.findAllUsers();
        userListModel.getUsers().addAll(allUsers); // NPE here !
    }

    public UserListModel getUserListModel() {
        retu userListModel;
    }


    public void setUserListModel(UserListModel userListModel) {
        this.userListModel = userListModel;
    }
}

My questions:

  • Why do I get a NPE when I click on the button at userListModel.getUsers().addAll(allUsers); ? allUsers is correct, and I initialized users ?

  • I eventually want to remove the @PostConstruct and have an empty dataTable that gets filled when clicking on the button. How to do this ?

What did I miss ?

asked 27 secs ago

برچسب: نویسنده: استخدام کار تاريخ: پنجشنبه 17 تير 1395 ساعت: 18:17

صفحه بندی