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.