1

我不熟悉使用omnifaces。从我收集到的一点点来看,在处理 flashscope 中的对象时使用 postInvokeAction 比 jsf preRenderView 事件更好。但我注意到的是 listener 方法被调用了两次!我觉得 preInvokeAction 类似于前阶段侦听器,而 postInvokeAction 类似于 PhaseID.INVOKE_APPLICATION 的后阶段侦听器,因此应该只为相应的事件调用一次。这个对吗?请向我解释。

我目前在 Mojarra 2.1.17 和 Omnifaces 1.3 上运行。

感谢您期待您的回复!

布局1.html

    <h:body>
    <p style="color: blue; font-size: 12pt;">1. This is the main content of the file.</p>
    <ui:insert name="body_contents"/>
    <p style="color: blue; font-size: 12pt;">This is the the remaining part of the document...   in layout1</p>
    </h:body>

示例页面2.html

<h:body>
    <f:view>
        <f:metadata>
            <f:viewParam name="dummy_var" value="#{sampletest.val_test}"/>
            <f:event type="postInvokeAction" listener="#{sampletest.frompostinvokeaction}" />
            <f:event type="preRenderView" listener="#{sampletest.fromprerenderview}" />
        </f:metadata>
    </f:view>
    <ui:composition template='/layout1.html'>
        <ui:define name="title_on_head">
            <style type="text/css">
                .pkssd{
                    min-width: 340px; min-height: 30px; background: appworkspace; color: blue; font-size: 11pt; font-style: italic;
                }
            </style>
        </ui:define>
        <ui:define name="body_contents">
            <p class="pkssd">This is the active content...</p>
        </ui:define>
    </ui:composition>
</h:body>

SampleTest.java

@ManagedBean(name="sampletest")
@ViewScoped
public class SampleTest {
private String val_test;    public void frompostinvokeaction(){
    System.out.println("frompostinvokeaction: val_test: " + val_test);    }
public void fromprerenderview(ComponentSystemEvent cse){
    System.out.println("fromprerenderview : val_test: " + val_test);
}
}
4

1 回答 1

0

您使用主/客户端模板的方式并不完全正确。它完全不符合规范,可能导致了未指定的行为。

正确的方法是这样的:

/WEB-INF/layout1.html

<!DOCTYPE html>
<html lang="en"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
>
    <f:view>
        <ui:insert name="metadata" />
        <h:head>
            ...
            <ui:insert name="title_on_head" />
            ...
        </h:head>
        <h:body>
            ...
            <ui:insert name="body_contents" />
            ...
        </h:body>
    </f:view>
</html>

/samplepage2.html

<ui:composition template="/WEB-INF/layout1.html">
    <ui:define name="metadata">
        <f:metadata>
            <f:viewParam name="dummy_var" value="#{sampletest.val_test}"/>
            <f:event type="postInvokeAction" listener="#{sampletest.frompostinvokeaction}" />
            <f:event type="preRenderView" listener="#{sampletest.fromprerenderview}" />
        </f:metadata>
    </ui:define>

    <ui:define name="title_on_head">
        <style type="text/css">
            .pkssd{
                min-width: 340px; min-height: 30px; background: appworkspace; color: blue; font-size: 11pt; font-style: italic;
            }
        </style>
    </ui:define>

    <ui:define name="body_contents">
        <p class="pkssd">This is the active content...</p>
    </ui:define>
</ui:composition>

(是的,这是完整的文件,外面什么都没有<ui:composition>

也可以看看:

于 2013-02-27T13:59:39.373 回答