0

I have a maven/web application with JSF Framework.

This is the code for the button:

<h:form>
     <h:commandButton id="button1" value="Sign in" class="btn btn-info navbar-btn navbar-right"/> 
</h:form>

and this is from the navigation file(faces-configx.xml):

<navigation-case>
            <from-outcome>button1</from-outcome>
            <to-view-id>/pages/signin.xhtml</to-view-id>
</navigation-case>

When I click the button, it doesnt work, it doesnt take me to the signin.xhtml page. Why is that? What is wrong with my code?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
CJR
  • 3,174
  • 6
  • 34
  • 78

2 Answers2

3

You're using Post-Redirect-Get pattern. When doing this, the JSF lifecycle, specifically in phase 5 Invoke Application, it will check that the return of the action is an empty string since you haven't defined an output from the action attribute in your <h:commandButton>:

<h:commandButton id="button1" value="Sign in" action="" />
                                              ^-------^
                                              not written, JSF assumes this

JSF will search this outcome in faces-config.xml and fire the navigation rule, if exists.

With this in mind, you have 3 alternatives:

  1. Set the outcome to a valid navitagion rule:

    <navigation-rule>
        <from-view-id>*</from-view-id>
        <navigation-case>
            <from-outcome>signin</from-outcome>
            <to-view-id>logout.xhtml</to-view-id>
        </navigation-case>
    </navigation-rule>
    

    And in Facelets:

    <h:commandButton value="Sign in" action="signin" />
    
  2. Create a proper managed bean which has a method that returns a String that matches with the proper navigation case:

    @ManagedBean
    @RequestScoped
    public class FooBean {
        public String signin() {
            return "signin";
        }
    }
    

    And in Facelets:

    <h:commandButton value="Sign in" action="#{fooBean.signin}" />
    
  3. A more proper solution for this case: don't use Post-Redirect-Get pattern, instead use direct navigation (Redirect-Get) through <h:button> (no form required).

    <h:button value="Sign in" outcome="signin" />
    

    Code above assumes signin.xhtml file exists in the same path of the file where this code is placed. This is:

    - webapp <-- root folder of the web pages of the app
      - another_folder <-- some folder
      + file.xhtml <-- file that contains <h:button>
      + signin.xhtml <-- file to navigate
    
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • Thank you so much! Lets say, that I want to use alternative 1. How do I use `class="btn btn-info navbar-btn navbar-right"` with the `` ? – CJR Oct 16 '14 at 20:52
  • @Carlton I omitted adding CSS since it's not part of the main problem. Just add the necessary CSS to your HTML and JSF components with no problems. – Luiggi Mendoza Oct 16 '14 at 20:53
  • I tried as you told me, alternativ 1. But the button still wont work? Why is that? Could there be something elsewhere that prevents the button to navigate me to another page? – CJR Oct 16 '14 at 21:00
  • Yes, thanks, my faces-config.xml file is exactly like that. The navigation between other pages(which I can access from my navbar or footer) is working just fine. But I can not make the navigation form the button to work. I tried adding `` around the commandButton, and then it worked. Do I need to have form for commandButton? – CJR Oct 16 '14 at 21:18
  • @Carlton I never meant to tell you to use `` without the ``. Any `UICommand` like `` and `` **needs** to be placed inside a ``, otherwise it won't work. I wrongly assumed you already know this. Also, in the `` example, I explicitly post that no form is required to make it work. – Luiggi Mendoza Oct 16 '14 at 21:20
  • Thank you so much for your time and help, I really do appreciate it. I am new to webprogramming, this is our first course in that area so I keep forgetting these small things, hehe. Sorry for that. Once again, thank you so much hope you are having a great day! – CJR Oct 16 '14 at 21:22
  • You're welcome. Keep practicing, that's the key to remember this small things :) – Luiggi Mendoza Oct 16 '14 at 21:23
-1

I believe you're not using the commandButton correctly.

I think this question can help you:

Difference between h:button and h:commandButton

Community
  • 1
  • 1
Vitor Santos
  • 581
  • 4
  • 15