0

Hello I am building a website using JSF 2.0.

I am trying to set up a commandButton so that after i press it it does some stuff with a method then navigates to a page dependent on what is returned from the method.

Until now i have only been using the JSF 2.0 action navigation but from what I have read it is not possible to have two actions so you must use a faces-config.xml file.

I have tried creating one and making a navigation rule but i cannot get it working. Think it is maybe a problem declaring it in my web.xml. I tried to keep in the original param values but the code would not compile so it is commented out:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
    <!--
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
    -->

    <param-name>javax.faces.CONFIG_FILES</param-name> 
  <param-value>/WEB-INF/faces-config.xml</param-value>

</context-param>
<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
</web-app>


<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>

My faces-config file is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">

   <navigation-rule> 
<from-view-id>/tabletsHome.xhtml</from-view-id> 
<navigation-case> 
  <from-outcome>product1</from-outcome> 
  <to-view-id>/inputForm.xhtml</to-view-id> 
</navigation-case> 
<navigation-case> 
  <from-outcome>product2</from-outcome> 
  <to-view-id>/inputForm.xhtml</to-view-id> 
</navigation-case> 

Also after the navigation takes you to the next page is there a way to pass information to this page depending on what the person has clicked on. For example a product number?

Thank you in advance for any help you can offer.

EHarpham
  • 602
  • 1
  • 17
  • 34
  • Example here http://stackoverflow.com/questions/3605238/how-do-you-pass-view-parameters-when-navigating-from-an-action-in-jsf2 – Sully May 03 '12 at 20:02

1 Answers1

1

I'm not sure how web.xml/faces-config.xml are related to this. As to your context param problem, just create another <context-param>. But this won't solve your problem. As to your navigation problem, you can just have a single action method which returns a different outcome.

public String submit() {
    if (...) {
        return "view1";
    }
    else {
        return "view2";
    }
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thankyou for the fast reply. As i understood from this post: http://stackoverflow.com/questions/4304132/hcommandbutton-multiple-actions-download-file-and-render-ajax-table, it was not possible to do multiple actions from a commandbutton. I want to run a javamail method and then have the page navigate to a different page which i thought was two actions so i would need to define a faces-config.xml even though i am using jsf 2.0? – EHarpham May 03 '12 at 20:29
  • I think the word `action` is ambiguous here. Based on your question I interpreted it as a JSF managed bean action method. But you apparently interpret it as a HTTP request (like as the other question/answer is handling). Why exactly do you need to send 2 HTTP requests for this simple case? Or is this just your misinterpretation? – BalusC May 03 '12 at 20:33
  • After previously using action=somePage for navigation it is not possible to add two action commands? Seems a lot more work to edit two files if you have to add a lot of navigations. I now have it working after creating the faces-config.xml. Thank you very much. – EHarpham May 03 '12 at 21:02
  • Since JSF 2.0, the faces config file is not necessary anymore for navigation case configuration. The returned outcome value will now implicitly be treated as ``. So in your particular case, you could just `return "inputForm";`. It will then go to `inputForm.xhtml`. – BalusC May 03 '12 at 21:06