0

I tried to create an edit page to update an entity. In the page, my controller can get id and render values on each field, but after click submit to update the entity, the #{adminController.post.postID} becomes 0.

edit_content.xhtml

<h:form>
  <h:panelGrid>
    <h:outputLabel value="Title" for="title" style="font-weight:bold" />
    <p:inputText id="title" value="#{adminController.post.title}" style="width:750px;"></p:inputText>
    <h:outputLabel value="Date" for="date" style="font-weight:bold" />
    <p:calendar id="date" value="#{adminController.post.postDate}" />
    <h:outputLabel value="Content" for="content" style="font-weight:bold" />
    <p:editor id="content" value="#{adminController.post.content}"></p:editor>
    <h:outputLabel value="Publish" style="font-weight:bold" />
    <p:inputSwitch value="#{adminController.post.status}" style="width:100px;"/>
  </h:panelGrid>
  <p:commandButton style="float:right;" action="#{adminController.updatePost(adminController.post.postID)}" value="Update" />
  <p:button style="float:right;" outcome="admin?faces-redirect=true" value="Back" />
</h:form>

AdminController.java

@Named
@RequestScoped
public class AdminController implements Serializable {

private static final long serialVersionUID = 324242342343L;

    @EJB
    private PostManager postManager;
    private List<DB_Post> posts = null;
    private String title;
    private Date postDate;
    private String content;
    private boolean status;
    private DB_Post post;

    @PostConstruct
    public void init() {
        this.posts = postManager.getALLPost();
        post = new DB_Post();
    }

    public String updatePost(int postID) {
        System.out.println("Content:" + content);
        post = new DB_Post();
        System.out.println("ID: " + postID);
        post = postManager.getPostById(postID);
        System.out.println("2. Post ID: " + post.getPostID());
        post.setContent(content);
        post.setStatus(status);
        post.setPostDate(postDate);
        post.setTitle(title);
        postManager.updatePost(post);
        return "admin?faces-redirect=true";
    }

I tried to print out something in the updatePost. It shows:

16:39:27,620 INFO [stdout] (default task-6) Content:null
16:39:27,620 INFO [stdout] (default task-6) ID: 0

In edit_content.xhtml, it shows id is not 0. I don't know why it becomes 0 after pass the argument into the method.

cheffe
  • 9,345
  • 2
  • 46
  • 57
Jasoniem9246
  • 93
  • 2
  • 10
  • Eroor message: 16:39:27,624 WARNING [javax.enterprise.resource.webcontainer.jsf.lifecycle] (default task-6) #{adminController.updatePost(adminController.post.postID)}: java.lang.NullPointerException: javax.faces.FacesException: #{adminController.updatePost(adminController.post.postID)}: java.lang.NullPointerException – Jasoniem9246 Jul 22 '15 at 21:59
  • This can only be answered if you show how exactly you implemented *"In the page, my controller can get id"*. To the point, you need to pass exactly that ID as a HTTP request parameter (not as an EL method argument!). – BalusC Jul 23 '15 at 05:36
  • post = new DB_Post(); You are initilizing your post with empty object. I assume postID will always evaluate to 0. – Parkash Kumar Jul 23 '15 at 05:42
  • @ParkashKumar: the id printed is fromthe methodcall, not the newly created object – Kukeltje Jul 23 '15 at 06:14
  • But, newly created object is also an empty instance declared with new. The id would be zero on the edit screen as well before the call of the method. – Parkash Kumar Jul 23 '15 at 06:18

0 Answers0