0

我使用 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 的新手。

登录.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>

登录Bean.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>
4

1 回答 1

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 的优势。

于 2012-05-09T12:13:50.000 回答