Pregunta

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.

¿Fue útil?

Solución

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";
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top