每當我單擊h:commandButton時,與該操作關聯的方法都不會被調用。 action="#{statusBean.update}"
不工作,更新沒有被調用。方法不從xhtml調用
1)這是我的XHTML頁面
<!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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head></h:head>
<h:body>
<h:form >
<p:dataList value="#{statusBean.statusList}" var="p">
<h:outputText value="#{p.statusId}-#{p.statusmsg}"/><br/>
<p:inputText value="#{statusBean.comment.comment}"/>
<h:commandButton value="comment" action="#{statusBean.update}"></h:commandButton>
</p:dataList>
</h:form>
</h:body>
</html>
2)這是我statusBean
package com.bean;
import java.util.List;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.servlet.http.HttpSession;
import com.entity.Album;
import com.entity.Comment;
import com.entity.Status;
import com.entity.User;
public class StatusBean {
Comment comment;
Status status;
private EntityManager em;
public Comment getComment() {
return comment;
}
public void setComment(Comment comment) {
this.comment = comment;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
public StatusBean(){
comment = new Comment();
status=new Status();
EntityManagerFactory emf=Persistence.createEntityManagerFactory("FreeBird");
em =emf.createEntityManager();
}
public String save(){
FacesContext context = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) context.getExternalContext().getSession(true);
User user = (User) session.getAttribute("userdet");
status.setEmail(user.getEmail());
System.out.println("status save called");
em.getTransaction().begin();
em.persist(status);
em.getTransaction().commit();
return "success";
}
public List<Status> getStatusList(){
FacesContext context = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) context.getExternalContext().getSession(true);
User user=(User) session.getAttribute("userdet");
Query query = em.createQuery("SELECT s FROM Status s WHERE s.email='"+user.getEmail()+"'", Status.class);
List<Status> results =query.getResultList();
return results;
}
public String update(){
System.out.println("Update Called...");
//comment.setStatusId(Integer.parseInt(statusId));
em.getTransaction().begin();
em.persist(comment);
em.getTransaction().commit();
return "success";
}
}
看到這個職位: http://stackoverflow.com/questions/11426433/commandbutton-in-datalist-inside-tab-of-accordionpanel-is -pos posible-primefaces 我覺得你有同樣的問題。 –