Faces导航在JSF2中无法正常工作 [英] Faces Navigation not really working in JSF2
问题描述
我正在使用JSF 2.0
I'm using JSF 2.0
这是我的faces-config.xml
this is my faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- This file is not required if you don't need any extra configuration. -->
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
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_2_0.xsd">
<navigation-rule>
<from-view-id>/pages/test/test.html</from-view-id>
<navigation-case>
<from-outcome>write</from-outcome>
<to-view-id>/pages/test/test-write.html</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
TestController.java
The TestController.java
@ManagedBean(name="testController")
@SessionScoped
public class TestController implements Serializable {
private static final long serialVersionUID = -3244711761400747261L;
public String test() {
return "write?faces-redirect=true";
}
在我的test.xhtml文件中
in my test.xhtml file
<?xml version="1.0" encoding="UTF-8"?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
template="/WEB-INF/templates/default.xhtml">
<ui:define name="content">
<h:form>
<h:commandButton action="#{testController.test()}" value="test" />
</h:form>
</ui:define>
</ui:composition>
这是我的web.xml
and this is my web.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee" 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-app_3_0.xsd"
version="3.0">
<display-name>Bachelor Demo</display-name>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
我想念什么?
推荐答案
视图ID不应包含FacesServlet
映射.它应该代表物理文件的路径/名称.将.html
更改为.xhtml
.
您还应该删除?faces-redirect=true
,而将<redirect />
添加到<navigation-case>
.
The view IDs should not contain the FacesServlet
mapping. It should represent the physical file path/name. Change .html
to .xhtml
.
You should also remove ?faces-redirect=true
and instead add a <redirect />
to the <navigation-case>
.
<navigation-rule>
<from-view-id>/pages/test/test.xhtml</from-view-id>
<navigation-case>
<from-outcome>write</from-outcome>
<to-view-id>/pages/test/test-write.xhtml</to-view-id>
<redirect />
</navigation-case>
</navigation-rule>
顺便说一下,这是旧的JSF 1.x样式.您是否知道新的JSF2隐式导航?您可以返回"/pages/test/test-write.xhtml?faces-redirect=true"
.
By the way, this is the old JSF 1.x style. Are you aware of the new JSF2 implicit navigation? You could just return "/pages/test/test-write.xhtml?faces-redirect=true"
.
public String test() {
return "/pages/test/test-write.xhtml?faces-redirect=true";
}
不再需要XML肿的XML导航用例.
No need for bloated XML navigation cases anymore.
此外,如果您的操作方法实际上没有执行任何其他操作,那么您也可以将返回值恰好放在action
属性中.
Further, if your action method is really not doing anything else, then you can also just put exactly that return value in the action
attribute instead.
<h:commandButton ... action="/pages/test/test-write.xhtml?faces-redirect=true" />
即使是普通的逐页导航,也请使用<h:link>
. SEO更友好,因为搜索引擎不会索引POST表单:
Even more, if it's plain page-to-page navigation, rather use <h:link>
instead. It's more SEO friendly as searchbots don't index POST forms:
<h:link ... outcome="/pages/test/test-write.xhtml" />
这篇关于Faces导航在JSF2中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!