Joe*_*erg 1 navigation faces-config java-ee-6 jsf-2
我正在使用JSF 2.0
这是我的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>
Run Code Online (Sandbox Code Playgroud)
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";
}
Run Code Online (Sandbox Code Playgroud)
在我的test.xhtml文件中
<?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>
Run Code Online (Sandbox Code Playgroud)
这是我的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>
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
视图ID不应包含FacesServlet
映射.它应该代表物理文件路径/名称.更改.html
到.xhtml
.你也应该删除?faces-redirect=true
,而是添加<redirect />
到<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>
Run Code Online (Sandbox Code Playgroud)
顺便说一句,这是旧的JSF 1.x风格.你知道新的JSF2隐式导航吗?你可以回来"/pages/test/test-write.xhtml?faces-redirect=true"
.
public String test() {
return "/pages/test/test-write.xhtml?faces-redirect=true";
}
Run Code Online (Sandbox Code Playgroud)
不再需要臃肿的XML导航案例.
此外,如果您的操作方法实际上没有做任何其他操作,那么您也可以将该确切的返回值放在action
属性中.
<h:commandButton ... action="/pages/test/test-write.xhtml?faces-redirect=true" />
Run Code Online (Sandbox Code Playgroud)
更重要的是,如果它是纯粹的页面到页面导航,而不是使用<h:link>
.由于搜索机器人不对POST表单编制索引,因此它更具SEO性能:
<h:link ... outcome="/pages/test/test-write.xhtml" />
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7885 次 |
最近记录: |