And*_*daP 4 navigation jsf primefaces jsf-2
我正在尝试使用JSF 2.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>
Run Code Online (Sandbox Code Playgroud)
索引页面有2个按钮,可以在单击时将您带到不同的页面
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>
Run Code Online (Sandbox Code Playgroud)
例如,以下按钮将带您进入成功页面(/pages/success.xhtml):
<p:commandButton id="addUser" value="Add" action="#{userMB.addUser}" ajax="false"/>
Run Code Online (Sandbox Code Playgroud)
我调试了这个addUser的返回值肯定是"成功".
该页面切换到success.xhtml,因为我看到内容已更改,但浏览器URL指向index.xhtml ....
在启动时:URL是localhost:8080/PROJECT /没有index.xhtml可能是因为我们将其配置为欢迎文件.当我们点击上面的按钮时,URL变为localhost:8080/PROJECT/index.xhtml
我相信我对映射或相对路径搞砸了.我找到了一些建议,唯一的映射应该是*.xhtml,但我真的不明白为什么以及如何处理页面的多个子文件夹.
任何帮助表示赞赏,谢谢
Bal*_*usC 14
页面切换到success.xhtml,因为我看到内容已更改,但浏览器URL指向index.xhtml.
这是正常行为.HTML <form action>指向/index.xhtml和表单将因此被提交到该URL,但在幕后,响应显示的内容/pages/success.xhtml.
如果您想要更改URL,则需要执行重定向.您可以通过添加这样做<redirect/>的<navigation-case>问题.
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/pages/success.xhtml</to-view-id>
<redirect/>
</navigation-case>
Run Code Online (Sandbox Code Playgroud)
与具体问题无关,使用导航案例是soo JSF 1.x. 考虑使用新的JSF 2.0隐式导航功能.这可以使您免于冗长的XML地狱.
public String addUser() {
if (...) {
return "/pages/success.xhtml?faces-redirect=true";
} else {
return "/pages/error.xhtml?faces-redirect=true";
}
}
Run Code Online (Sandbox Code Playgroud)
然而,我建议只添加面部消息,然后通过返回void或null不导航到"成功"或"错误"页面来重新显示同一页面,这也是真正的Web表单的工作方式.当然,除非你正在做一些"Hello World".