[英]method is not called from xhtml
Whenever I am clicking the h:commandButton,the method associated with the action is not called. 每当我单击h:commandButton时,都不会调用与该动作关联的方法。
action="#{statusBean.update}"
is not working, the update is not being called. action="#{statusBean.update}"
不起作用,未调用更新。
1) Here is my xhtml page 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)Here is my statusBean 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";
}
}
You need a @ManagedBean
annotation for your class and a scope annotation, eg 您需要为类添加
@ManagedBean
注释和范围注释,例如
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ManagedBean
@ViewScoped
public class StatusBean {
...
}
I was facing same problem then i used a4j:jsFunction and problem was solved 我遇到了同样的问题,然后我使用了a4j:jsFunction并解决了问题
<a4j:jsFunction name="goToupdate" action="#{statusBean.update()}" </a4j:jsFunction>
now your h:commandButton look like this 现在您的h:commandButton看起来像这样
<h:commandButton value="comment" onclick="goToupdate()"></h:commandButton>
use <p:commandButton>
instead of <h:commandButton>
使用
<p:commandButton>
代替<h:commandButton>
for eg. 例如
<p:commandButton value="comment" action="#{statusBean.update}" process="@this" ></p:commandButton>
and put your bean in view scope 并将您的bean放在视图范围内
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.