0

I have made a Login.xhtml by using Apache Myfaces 2.1 (using JSF 1.x)

When I have given my username and password on login.xhtml and one submit button is there.

When I click on goodbye button, the control goes to wsListing.xhtml but the URL in the browser remain as

http://hostid:8080/TestClient/faces/login.xhtml

I want it to be changed as

http://hostid:8080/TestClient/faces/wsListing.xhtml

I have attached the code for the same, please suggest me some solution, I am new to JSF.

Login.xhtml

  <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui" xmlns:lang="en">
<f:view>
    <head>
<title>Test Client Login</title>
    </head>
    <h:form id="loginForm">
        <table>
        <tr>
                <h:outputLabel for="username">
                    <h:outputText id="usernameLabel" value="Enter Username:" />
                </h:outputLabel>
                <h:inputText id="username" value="#{loginBean.username}"
                    required="true">
                    <f:validateLongRange minimum="1" maximum="500" />
                </h:inputText>
        </tr>
        <tr>        
                <h:outputLabel for="password">
                    <h:outputText id="passwordLabel" value="Enter Password:" />
                </h:outputLabel>
                <h:inputText id="password" value="#{loginBean.password}"
                    required="true">
                    <f:validateLongRange minimum="1" maximum="500" />
                </h:inputText>
        </tr>
        <tr>    

        <h:commandButton id="goodbyeCommand" type="submit" value="Goodbye"
            action="#{loginBean.goodbye}" immediate="true" />

        </tr>   
</table>
    </h:form>
</f:view>


</html>

LoginBean.java

package com.example;

/**
 * Managed Bean for Login
 * 
 */
public class LoginBean {

    private String username;
    private String password;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String goodbye() {
        return "success";
    }
}

faces-config.xml

<?xml version="1.0"?>
<faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xi="http://www.w3.org/2001/XInclude" 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_1_2.xsd">
<managed-bean>
        <description>Login Bean</description>
        <managed-bean-name>loginBean</managed-bean-name>
        <managed-bean-class>com.example.LoginBean</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>
<navigation-rule>
        <description>Navigation from the hello page.</description>
        <from-view-id>/login.xhtml</from-view-id>
        <navigation-case>
            <from-outcome>success</from-outcome>
            <to-view-id>/wsListing.xhtml</to-view-id>
        </navigation-case>
    </navigation-rule>
</faces-config>
Sunny Gupta
  • 6,929
  • 15
  • 52
  • 80

1 Answers1

2

You need to send a redirect. Add <redirect /> to the <navigation-case />.

<navigation-case>
    <from-outcome>success</from-outcome>
    <to-view-id>/wsListing.xhtml</to-view-id>
    <redirect />
</navigation-case>

Unrelated to the concrete problem, MyFaces 2.1 is a JSF 2.1 implementation. Why are you forcing the webapp to run in JSF 1.2 modus? You miss so many JSF 2.x advantages.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    Ah okay. If you were using JSF 2.x, you would just have used `` without that whole navigation case. – BalusC May 09 '12 at 12:19
  • Check its own site. http://myfaces.apache.org/core21/index.html You're only forcing it to run in JSF 1.2 modus because your `faces-config.xml` is declared that way. – BalusC May 09 '12 at 13:03
  • You can do so, but that's not necessary. You can force JSF2 to run in JSF 1.x modus like as you already did. Note that Weblogic 10.0.x itself already ships with JSF 1.x. – BalusC May 09 '12 at 13:31
  • @BalusC....is a generic solution to above question?I got confused with the answer given by Saskia de Jong in this question.http://www.coderanch.com/t/211613/JSF/java/why-same-URL-shows-different – mdp Nov 07 '12 at 05:34
  • @Uppi: a redirect creates a new HTTP request which requires a bit of trickery if you'd like to prepare some data for the next page before the redirect has taken place. – BalusC Nov 07 '12 at 10:34