未调用JSF CommandButton操作(另一个示例)

时间:2017-05-06 06:08:44

标签: jsf

我从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()”事件甚至没有被触发。控制就在那里。

我注意到的一件事是commandButtonsinEdit

时重新加载了屏幕

我也取消了“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;
    }

1 个答案:

答案 0 :(得分:0)

在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