我尝试创建一个编辑页面来更新实体。在页面中,我的控制器可以在每个字段上获取id和渲染值,但在单击“提交”以更新实体后,#{adminController.post.postID}
将变为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";
}
我试图在updatePost中打印出一些东西。它显示:
16:39:27,620 INFO [stdout](默认任务-6)内容:null
16:39:27,620 INFO [stdout](默认任务-6)ID:0
在edit_content.xhtml中,它显示id不为0.我不知道为什么在将参数传递给方法后它变为0。