【问题标题】:URL should change in JSF navigationJSF 导航中的 URL 应更改
【发布时间】:2012-05-09 11:25:11
【问题描述】:

我使用 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";
    }
}

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>

【问题讨论】:

    标签: jsf myfaces


    【解决方案1】:

    您需要发送重定向。将&lt;redirect /&gt; 添加到&lt;navigation-case /&gt;

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

    【讨论】:

    • 啊,好吧。如果您使用的是 JSF 2.x,那么您将只使用 &lt;h:button value="Goodbye" outcome="wsListing" /&gt; 而没有整个导航案例。
    • 检查自己的网站。 myfaces.apache.org/core21/index.html 您只是强制它以 JSF 1.2 方式运行,因为您的 faces-config.xml 是以这种方式声明的。
    • 您可以这样做,但这不是必需的。您可以像以前那样强制 JSF2 在 JSF 1.x 模式下运行。请注意,Weblogic 10.0.x 本身已经随 JSF 1.x 一起提供。
    • @BalusC....是 上述问题的通用解决方案吗?我对 Saskia de Jong 在这个问题中给出的答案感到困惑。coderanch.com/t/211613/JSF/java/why-same-URL-shows-different
    • @Uppi:重定向会创建一个新的 HTTP 请求,如果您想在重定向发生之前为下一页准备一些数据,这需要一些技巧。
    猜你喜欢
    • 2013-04-10
    • 2015-07-16
    • 2019-02-28
    • 2020-02-10
    • 2011-11-22
    • 2011-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多