【问题标题】:JSF CommandButton action not invoked (another example)未调用 JSF CommandButton 操作(另一个示例)
【发布时间】:2017-05-06 06:08:43
【问题描述】:

我取自 https://stackoverflow.com/a/3180885/242042 的示例,只是根据我的需要对其进行了调整。

这是我的豆子:

@ManagedBean
@ViewScoped
public class ParticipantBean implements
    Serializable {

    private boolean edit;

    private List<Participant> list;

    private Participant participant = new Participant();

    @Inject
    private transient ParticipantDAO participantDAO;

    public void add() {

        System.out.println("Calling add");
        participantDAO.save(participant);
        init();
    }

    public void delete(final Participant participant) {

        participant.getAudit().cancel();
        participantDAO.save(participant);
        init();
    }

    public void edit(final Participant participant) {

        this.participant = participantDAO.get(participant.getId());
        edit = true;
    }

    public void fire() {

        System.out.println("fired");
    }

    public List<Participant> getList() {

        return list;
    }

    public Participant getParticipant() {

        return participant;
    }

    @PostConstruct
    public void init() {

        list = participantDAO.getAll();
        participant = new Participant(); // Reset placeholder.
    }

    public boolean isInEdit() {

        return edit;
    }

    public void saveParticipant() {

        System.out.println("Calling save");
        participantDAO.save(participant);
        System.out.println("Done Calling save");
        init();
        edit = false;
    }
}

还有我的 JSF 文件

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
        <title>Really simple CRUD</title>
    </h:head>
    <h:body>
        <h3>List items</h3>
        <h:form rendered="#{not empty participantBean.list}">
            <h:dataTable value="#{participantBean.list}" var="item">
                <h:column><f:facet name="header">ID</f:facet>#{item.id}</h:column>
                <h:column><f:facet name="header">Name</f:facet>#{item.name}</h:column>
                <h:column><h:commandButton value="edit" action="#{participantBean.edit(item)}" /></h:column>
                <h:column><h:commandButton value="delete" action="#{participantBean.delete(item)}" /></h:column>
                <h:column><h:commandButton value="fire" action="#{participantBean.fire}" /></h:column>
            </h:dataTable>
        </h:form>
        <h:panelGroup rendered="#{empty participantBean.list}">
            <p>Table is empty! Please add new items.</p>
        </h:panelGroup>
        <h:panelGroup rendered="#{!participantBean.inEdit}">
            <h3>Add item</h3>
            <h:form>
                <p>Name: <h:inputText value="#{participantBean.participant.name}" /></p>
                <p><h:commandButton value="add" action="#{participantBean.add}" /></p>
                <p><h:commandButton value="fire" action="#{participantBean.fire}" /></p>
            </h:form>
        </h:panelGroup>
        <h:panelGroup rendered="#{participantBean.inEdit}">
            <h3>Edit item #{participantBean.participant.id}</h3>
            <h:form>
                <p>Name: <h:inputText value="#{participantBean.participant.name}" /></p>
                <p><h:commandButton value="save" action="#{participantBean.saveParticipant}" /></p>
                <p><h:commandButton value="add" action="#{participantBean.add}" /></p>
                <p><h:commandButton value="fire" action="#{participantBean.fire}" /></p>
            </h:form>
        </h:panelGroup>

    </h:body>
</html>

所以它非常相似,但我不明白为什么在“编辑”时它不想调用动作。 (即我在 SystemOut.log 上看不到任何内容)

我正在查看答案https://stackoverflow.com/a/13327382/242042 以查看是否有任何优点,但我发现“System.out.println()”事件甚至没有被触发。控制就在那里。

我注意到的一件事是commandButtons 重新加载屏幕,而inEdit

我也消除了“Prime Faces”,并且正在 WebSphere 9.0.0.3 上进行测试。代码在https://github.com/trajano/jee/tree/no-prime-faces

我也尝试过重新排序表单,使编辑块处于这样的表单中,但仍然不会触发操作。

    <h:form rendered="#{not empty participantBean.list}">
        <h:dataTable value="#{participantBean.list}" var="item">
            <h:column><f:facet name="header">ID</f:facet>#{item.id}</h:column>
            <h:column><f:facet name="header">Name</f:facet>#{item.name}</h:column>
            <h:column><h:commandButton value="edit" action="#{participantBean.edit(item)}" /></h:column>
            <h:column><h:commandButton value="delete" action="#{participantBean.delete(item)}" /></h:column>
        </h:dataTable>
        <h:panelGroup rendered="#{participantBean.inEdit}">
           <h3>Edit item #{participantBean.participant.id}</h3>
            <p>Name: <h:inputText value="#{participantBean.participant.name}" /></p>
            <p><h:commandButton value="save" action="#{participantBean.saveParticipant}" /></p>
            <p><h:commandButton value="add" action="#{participantBean.add}" /></p>
        </h:panelGroup>
    </h:form>

我还尝试以这种方式编写edit(),以使原始列表中的参与者具有适当的乐观锁@Version

    public void edit(final Participant participant) {

        this.participant = participant;
        edit = true;
    }

【问题讨论】:

  • edit 方法中没有System.out.println。作为调试参考,您可以更好地使用stackoverflow.com/questions/2118656/…
  • 它保存在不会被触发的编辑面板组中。
  • 我还查看了该参考资料,但看不到任何适用的内容。

标签: jsf


【解决方案1】:

在 JSF 2.2 中,不同的包中有两个注解,名为 @ViewScoped

  • javax.faces.bean.ViewScoped←正确一个
  • javax.faces.view.ViewScoped ← JSF 2.2 引入的错误(这是我写问题时使用的)

根据@Jasper de Vries 的评论将其限制为 JSF 2.2。注释需要更改为

@javax.inject.Named
@javax.faces.view.ViewScoped

【讨论】:

  • 这不是正确或错误的问题。第二个(CDI)对我来说很好。第一个将在 JSF 2.3 中弃用。所以,看看你是否能得到与 CDI 一起工作的东西。另见stackoverflow.com/questions/4347374/…
猜你喜欢
  • 1970-01-01
  • 2012-10-30
  • 1970-01-01
  • 2016-01-12
  • 1970-01-01
  • 1970-01-01
  • 2011-03-09
  • 1970-01-01
  • 2019-01-20
相关资源
最近更新 更多