0

I took the example from https://stackoverflow.com/a/3180885/242042 just tweaked it for my needs.

Here's my bean:

@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;
    }
}

And my JSF file

<!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>

So it is quite similar, but what I don't understand is why on the "edit" it does not want to invoke the actions. (i.e. I don't see anything on SystemOut.log)

I was looking at the answer https://stackoverflow.com/a/13327382/242042 to see if there was anything to merit it, but I found that the "System.out.println()" events do not even get fired. The control is there.

One thing I did notice was the commandButtons reload the screen while inEdit

I have eliminated "Prime Faces" as well and I am testing on WebSphere 9.0.0.3. The code is in https://github.com/trajano/jee/tree/no-prime-faces

I also have tried reordering the form such that the edit block is in the form like so but it still the actions do not fire.

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

I also tried having edit() written this way to get the participant that was on the original list that way it will have the proper optimistic lock @Version

    public void edit(final Participant participant) {

        this.participant = participant;
        edit = true;
    }
Community
  • 1
  • 1
Archimedes Trajano
  • 22,850
  • 10
  • 113
  • 154

1 Answers1

0

In JSF 2.2 there are two annotations in different packages named @ViewScoped

  • javax.faces.bean.ViewScoped ← correct one
  • javax.faces.view.ViewScoped ← incorrect one introduced with JSF 2.2 (which was what I was using when I wrote the question)

To limit it to JSF 2.2 as per @Jasper de Vries 's comment. The annotations need to change to

@javax.inject.Named
@javax.faces.view.ViewScoped
Archimedes Trajano
  • 22,850
  • 10
  • 113
  • 154
  • It's not a matter of correct or incorrect. The second one (CDI) works just fine for me. The first one will be deprecated in JSF 2.3. So, see if you can get stuff working with CDI. See also http://stackoverflow.com/questions/4347374/backing-beans-managedbean-or-cdi-beans-named – Jasper de Vries May 06 '17 at 17:41