1

I have a question about jsf framework. This is my controller:

controller

package controller;

@ManagedBean
@ViewScoped
#public class Controller implements Serializable {

    private boolean blView;

    @PostConstruct
    public void init() {
        blView = true;
    }

    public void add() {
        System.out.println("This is method add");
        blView = false;
    }

    public void back() {
        System.out.println("This is method back");
        blView = true;
    }

    public boolean isBlView() {
        return blView;
    }

    public void setBlView(boolean blView) {
        this.blView = blView;
    }

}

File index.xhtml

 <h:form id="frmView" >
        <p:panel id="pnlControl">
            <p:commandButton id="p1" actionListener="#{controller.back()}" immediate="true" rendered="#{!controller.blView}"
                             process="@this" value="Back" update=":frmView:pnlControl :frmView:pnlAdd"/>

          <p:commandButton id="p2" actionListener="#{controller.add()}" immediate="true" rendered="#{controller.blView}" 
                             value="Add" process="@this" update=":frmView:pnlControl :frmView:pnlAdd"/>

     </p:panel>

     <p:panel id="pnlAdd" visible="#{!controller.blView}">
            <p:panelGrid columns="2" >
                <f:facet name="header" >
                    Info
                </f:facet>

            <h:outputLabel value="Name: " />
                <h:inputText value="Hello Viet Nam." />

       </p:panelGrid>

   </p:panel>

So, when I added or deleted attribute actionListener="#controller.back()}", the method back() are not enforced when I click on the button. Thank advance.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user3750552
  • 123
  • 1
  • 9
  • 1
    To reach the culprit closer, try removing the `immediate` attribute just as an additional exercise to see, if it makes a difference. (And a pair of parentheses after a method name in `actionListener` is unnecessary in this case). – Tiny Oct 14 '15 at 03:43
  • i removed the immediate attribute and a pair of parentheses after a method name in actionListener. But the problem don't resolve. when i remove rendered="#{!controller.blView}" by render="true" then the method back() will enforce. – user3750552 Oct 14 '15 at 03:51
  • 1
    I copy/pasted the code in a test environment. I saw both the methods being invoked correctly. Did you import the view scoped annotation correctly, `javax.faces.bean.ViewScoped` in this case? – Tiny Oct 14 '15 at 04:15
  • Yes... Thank you so much... I had import wrong library javax.faces.view.ViewScoped instead of library javax.faces.bean.ViewScoped . Thank you!!! – user3750552 Oct 14 '15 at 04:22
  • 1
    The JSF managed bean turns out to be request scoped in that case with that combination of annotations in which case the CDI annotation `javax.faces.view.ViewScoped` would simply be ignored as if it were not present at all leaving the managed bean with the only annotation `@ManagedBean` which defaults to `@javax.faces.bean.RequestScoped`. – Tiny Oct 14 '15 at 04:32
  • Yes. I tried to find error in my example, but i can't believe i add wrong library. I read about annotation. However, i need to learn more about JSF framework. Thank you. Because i can continue with my project. – user3750552 Oct 14 '15 at 04:51

0 Answers0