【问题标题】:method is not called from xhtml方法不是从 xhtml 调用的
【发布时间】:2012-09-19 17:10:14
【问题描述】:

每当我单击 h:commandButton 时,不会调用与操作关联的方法。action="#{statusBean.update}" 不起作用,不会调用更新。

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";

    }
}

【问题讨论】:

标签: jsf jpa jsf-2 jpa-2.0


【解决方案1】:

你需要一个@ManagedBean 类注解和一个范围注解,例​​如

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class StatusBean {
    ...
}

【讨论】:

  • 我的状态 Bean 是请求范围的。我已经使用 facesconfig.xml 来设置 bean 的范围。
【解决方案2】:

我遇到了同样的问题,然后我使用 a4j:jsFunction 并解决了问题

<a4j:jsFunction name="goToupdate" action="#{statusBean.update()}" </a4j:jsFunction>

现在你的 h:commandButton 看起来像这样

&lt;h:commandButton value="comment" onclick="goToupdate()"&gt;&lt;/h:commandButton&gt;

【讨论】:

    【解决方案3】:

    使用&lt;p:commandButton&gt; 代替&lt;h:commandButton&gt;

    例如&lt;p:commandButton value="comment" action="#{statusBean.update}" process="@this" &gt;&lt;/p:commandButton&gt;

    并将您的 bean 放在视图范围内

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-05
      • 2013-05-08
      • 2020-05-09
      • 1970-01-01
      • 1970-01-01
      • 2013-01-12
      • 1970-01-01
      • 2021-04-12
      相关资源
      最近更新 更多