Jsf 2 Faces导航在JSF2中不起作用

Jsf 2 Faces导航在JSF2中不起作用,jsf-2,navigation,java-ee-6,faces-config,Jsf 2,Navigation,Java Ee 6,Faces Config,我正在使用JSF2.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="

我正在使用JSF2.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>
在我的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>

这是我的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>

学士演示
Facesservlet
javax.faces.webapp.FacesServlet
1.
Facesservlet
*.html

我缺少什么?

视图ID不应包含
FacesServlet
映射。它应该表示物理文件路径/名称。将
.html
更改为
.xhtml
。 您还应该删除
?faces redirect=true
,而是在
中添加一个

<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>
不再需要臃肿的XML导航案例

此外,如果您的操作方法实际上没有做任何其他事情,那么您也可以将该返回值准确地放在
action
属性中

<h:commandButton ... action="/pages/test/test-write.xhtml?faces-redirect=true" />

更重要的是,如果是简单的页到页导航,请改用
。它对搜索引擎优化更加友好,因为搜索机器人不会为帖子表单编制索引:

<h:link ... outcome="/pages/test/test-write.xhtml" />

视图ID不应包含
FacesServlet
映射。它应该表示物理文件路径/名称。将
.html
更改为
.xhtml
。 您还应该删除
?faces redirect=true
,而是在
中添加一个

<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>
不再需要臃肿的XML导航案例

此外,如果您的操作方法实际上没有做任何其他事情,那么您也可以将该返回值准确地放在
action
属性中

<h:commandButton ... action="/pages/test/test-write.xhtml?faces-redirect=true" />

更重要的是,如果是简单的页到页导航,请改用
。它对搜索引擎优化更加友好,因为搜索机器人不会为帖子表单编制索引:

<h:link ... outcome="/pages/test/test-write.xhtml" />


hi@BalusC,我尝试了这两种方法-我在导航规则中将其更改为.xhtml-它不起作用。我还删除了导航规则并使用了
“/pages/test/test write.xhtml?faces redirect=true”
faces config.xml
web.xml
对吗?如果规则不起作用,那么from view ID就是不匹配。您可以通过检查action方法中的
facesContext.getViewRoot().getViewId()
来了解它。请注意,我更新了我的答案,建议删除
?faces redirect
。此外,我不确定我是否理解你的最后一个问题。这个字符串应该是action方法的
返回值。哦,那么action就不会被调用了。您可以通过在action方法上设置断点或添加sysout/logger行来解决这个问题。顺便说一下,如果action方法只包含导航的返回值,那么您也可以将该返回值准确地放在
action
属性中。更重要的是,如果它是纯页到页的导航,那么使用
而不是w/o重定向。这对搜索引擎优化更加友好。另见,谢谢。。。这就是我最后发现它的原因。。。通过从方法返回文件名,我感到非常愚蠢,您是否将视图和控制器耦合在一起?我的意思是,编写方法的人必须告诉编写.xhtml页面的人如何命名?对于像我这样的家伙,我想这没关系!您好@BalusC,我尝试了这两种方法-我在导航规则中将其更改为.xhtml-它不起作用。我还删除了导航规则并使用了
“/pages/test/test write.xhtml?faces redirect=true”
faces config.xml
web.xml
对吗?如果规则不起作用,那么from view ID就是不匹配。您可以通过检查action方法中的
facesContext.getViewRoot().getViewId()
来了解它。请注意,我更新了我的答案,建议删除
?faces redirect
。此外,我不确定我是否理解你的最后一个问题。这个字符串应该是action方法的
返回值。哦,那么action就不会被调用了。您可以通过在action方法上设置断点或添加sysout/logger行来解决这个问题。顺便说一下,如果action方法只包含导航的返回值,那么您也可以将该返回值准确地放在
action
属性中。更重要的是,如果它是纯页到页的导航,那么使用
而不是w/o重定向。这对搜索引擎优化更加友好。另见,谢谢。。。这就是我最后发现它的原因。。。通过从方法返回文件名,我感到非常愚蠢,您是否将视图和控制器耦合在一起?我的意思是,编写方法的人必须告诉编写.xhtml页面的人如何命名?对于像我这样的家伙,我想这没关系!