JSF2.0导航无法正常工作

JSF2.0导航无法正常工作,jsf,jsf-2,navigation,primefaces,Jsf,Jsf 2,Navigation,Primefaces,我正在尝试开始使用JSF2.0。我正在尝试一个基本的导航示例,但它不能正常工作 文件结构如下所示: 我已在web.xml中配置映射: <!-- Change to "Production" when you are ready to deploy --> <context-param> <param-name>javax.faces.PROJECT_STAGE</param-name> <param-value>Dev

我正在尝试开始使用JSF2.0。我正在尝试一个基本的导航示例,但它不能正常工作

文件结构如下所示:

我已在web.xml中配置映射:

<!-- Change to "Production" when you are ready to deploy -->
<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
</context-param>

<!-- Welcome page -->
<welcome-file-list>
    <welcome-file>index.xhtml</welcome-file>
</welcome-file-list>

<!-- JSF mapping -->
<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

javax.faces.PROJECT_阶段
发展
index.xhtml
Facesservlet
javax.faces.webapp.FacesServlet
Facesservlet
*.xhtml
索引页面有两个按钮,当你点击时,它们会把你带到不同的页面

faces-config.xml文件定义了一些导航情况:

<!-- Configuration of navigation rules -->  
<navigation-rule>
    <from-view-id>/index.xhtml</from-view-id> 
    <navigation-case>
        <from-outcome>success</from-outcome>
        <to-view-id>/pages/success.xhtml</to-view-id>
    </navigation-case>
     <navigation-case>
        <from-outcome>error</from-outcome>
        <to-view-id>/pages/error.xhtml</to-view-id>
    </navigation-case>
</navigation-rule>

/index.xhtml
成功
/pages/success.xhtml
错误
/pages/error.xhtml
例如,以下按钮应将您带到成功页面(/pages/success.xhtml):


我已经调试了这个程序,addUser的返回值肯定是“success”

页面切换到success.xhtml,因为我看到内容已更改,但浏览器URL指向index.xhtml

启动时:URL是localhost:8080/PROJECT/,没有index.xhtml,可能是因为我们将其配置为欢迎文件。当我们点击上面的按钮时,URL变成localhost:8080/PROJECT/index.xhtml

我相信我把映射或相对路径搞砸了。我发现了一些建议,唯一的映射应该是带有*.xhtml的映射,但我并不真正理解为什么以及如何处理页面的多个子文件夹


感谢您的帮助,请在导航规则中尝试此项

<navigation-rule>
<from-view-id>/index.jsf</from-view-id> 
<navigation-case>
    <from-outcome>success</from-outcome>
    <to-view-id>/pages/success.jsf</to-view-id>
</navigation-case>
 <navigation-case>
    <from-outcome>error</from-outcome>
    <to-view-id>/pages/error.jsf</to-view-id>
</navigation-case>

/index.jsf
成功
/pages/success.jsf
错误
/pages/error.jsf

页面切换到success.xhtml,因为我看到内容已更改,但浏览器URL指向index.xhtml

这是正常的行为。HTML
指向
/index.xhtml
,因此表单将完全提交到该URL,但在幕后,响应显示
/pages/success.xhtml
的内容

如果您想更改URL,则需要执行重定向。您可以通过将
添加到有问题的
中来实现这一点

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

然而,我建议只添加faces消息,然后通过返回
void
null
重新显示相同的页面,而不是导航到“成功”或“错误”页面,这也是真正的web表单的工作方式。当然,除非你正在做一些“Hello World”的工作。

不,不起作用:找不到与from view id“/index.xhtml”for action“#{userMB.addUser}”匹配的导航案例,结果是“success”,你没有以任何方式解释它为什么以及如何解决OP的具体问题。请在以后的回答中这样做。另外,这根本不能解决OP的具体问题。太好了!使用重定向标记,URL也会更改。我不知道你所说的“只是添加faces消息,然后通过返回void或null来重新显示相同的页面”是什么意思。也许我可以找到一个链接,链接到更详细的解释?非常感谢您的时间不客气。你可能会发现这些文章中的具体例子很有见地:和。
<navigation-case>
    <from-outcome>success</from-outcome>
    <to-view-id>/pages/success.xhtml</to-view-id>
    <redirect/>
</navigation-case>
public String addUser() {
    if (...) {
        return "/pages/success.xhtml?faces-redirect=true";
    } else {
        return "/pages/error.xhtml?faces-redirect=true";
    }
}