2012-03-21 47 views
1

真正的工作,我在我的测试中使用JSF 2.0面临导航不JSF2

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

@ManagedBean(name="testController") 
@SessionScoped 
public class TestController implements Serializable { 

    private static final long serialVersionUID = -3244711761400747261L; 

    public String test() { 


     return "write?faces-redirect=true"; 
} 

。 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> 

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

我在想什么?

回答

9

视图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> 

顺便说一句,这是旧JSF 1.x的风格。你知道新的JSF2隐式导航吗?你可以返回"/pages/test/test-write.xhtml?faces-redirect=true"

public String test() { 
    return "/pages/test/test-write.xhtml?faces-redirect=true"; 
} 

不再需要臃肿的XML导航案例。此外,如果您的操作方法实际上没有做其他任何事情,那么您也可以将action属性中的确切返回值代替。

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

更有甚者,如果是普通的页面到页面导航,而使用<h:link>代替。它更搜索引擎友好的searchbots不索引POST形式:

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

喜@BalusC,我都尝试 - 我改变了它在navigationrule到.xhmtl - 这是行不通的。我也删除了导航规则并使用''/pages/test/test-write.xhtml?faces-redirect=true''是'faces-config.xml'和'web.xml'吗? – Joerg 2012-03-21 16:22:12

+1

如果规则不起作用,那么从视图ID只是不匹配。您可以通过在action方法中检查'facesContext.getViewRoot()。getViewId()'来了解它。请注意,我更新了我的答案,并建议删除'?faces-redirect'。此外,我不确定我是否理解你的最后一个问题。该字符串应该是操作方法的“返回”值。 – BalusC 2012-03-21 16:24:24

+1

哦,这个动作根本不会被调用。您可以通过在操作方法上设置断点或通过添加sysout/logger行来计算出来。顺便说一下,如果action方法不包含除naviation的返回值之外的任何内容,那么您也可以将'action'属性中的确切返回值替换。更重要的是,如果它是纯粹的页面到页面导航,而是使用''而不用重定向。这对SEO更友好。另请参阅http://stackoverflow.com/questions/4317684/when-should-i-use-houtputlink-instead-of-hcommandlink – BalusC 2012-03-21 16:54:40