URL应该在JSF导航中更改

时间:2012-05-09 11:25:12

标签: jsf myfaces

我使用Apache Myfaces 2.1(使用JSF 1.x)制作了一个Login.xhtml

当我在login.xhtml上提供了用户名和密码,并且有一个提交按钮。

当我点击再见按钮时,控件转到wsListing.xhtml,但浏览器中的URL保持为

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

我希望将其更改为

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

我已附上相同的代码,请给我一些解决方案,我是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";
    }
}

面-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>

1 个答案:

答案 0 :(得分:2)

您需要发送重定向。将<redirect />添加到<navigation-case />

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

对于具体问题,

无关,MyFaces 2.1是一个JSF 2.1实现。你为什么强迫webapp以JSF 1.2方式运行?你错过了很多JSF 2.x的优势。