1

私は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>

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ファイルで

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

私は何が欠けていますか?

4

1 に答える 1

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>代わりに使用してください。検索ボットは POST フォームをインデックス化しないため、SEO フレンドリーです。

<h:link ... outcome="/pages/test/test-write.xhtml" />
于 2012-03-21T16:08:26.930 に答える