简体   繁体   English

在JSF中每包含一个.xhtml页面之后,如何调用同一方法?

[英]How to call the same method after every include of a .xhtml page in JSF?

I have the following part of a .xhtml page: 我有一个.xhtml页面的以下部分:

<ui:composition template="./templates/template.xhtml">
<ui:define name="mainContent">

    <ui:include src="include/includeAbleEditor.xhtml">
        <ui:param name="includeParam" value="MyClass" />

    </ui:include>



    <ui:include src="include/includeAbleEditor.xhtml">
        <ui:param name="includeParam" value="YourClass" />
    </ui:include>

</ui:define>

In the "includeAbleEditor.xhtml" I want to call a method after it was included (In this case this should happend two times). 在“ includeAbleEditor.xhtml”中,我想在包含该方法后调用一个方法(在这种情况下,应该发生两次)。

Now I tried to solve it like this: (metadata tag is part of the includeAbleEditor.xhtml) 现在,我试图像这样解决它:(元数据标记是includeAbleEditor.xhtml的一部分)

<f:metadata>
    <f:event type="preRenderView" listener="#{editor.onload}" />
    <f:attribute name="textFieldId" value="#{includeParam}" />
</f:metadata>

The Problem: 问题:

The method is being called only once. 该方法仅被调用一次。 But it should be called two times. 但是应该调用两次。 Once with the parameter "MyClass" and once with "YourClass". 一次使用参数“ MyClass”,一次使用“ YourClass”。

Do you have any suggestions? 你有什么建议吗?

Thanks a lot! 非常感谢!

There can be only one <f:metadata> in the entire view and it must be in the top level view. 整个视图中只能有一个<f:metadata> ,并且它必须位于顶层视图中。 Unlike eg <f:view> , they don't "extend" each other and all others will be ignored. 与例如<f:view> ,它们不会彼此“扩展”,其他所有都会被忽略。

You actually don't need it here. 您实际上在这里不需要它。 It's only necessary whenever you need to attach <f:viewParam> and/or <f:viewAction> to the specific view. 仅在需要将<f:viewParam>和/或<f:viewAction>到特定视图<f:viewAction>需要。 The <f:event> doesn't require a <f:metadata> . <f:event>不需要<f:metadata> It will just be hooked to the parent UIComponent . 它将仅被挂钩到父UIComponent It was during JSF 2.0/2.1 ages (when <f:viewAction> didn't exist) being abused to have a hook to invoke a listener after <f:viewParam> values are being set. 正是在JSF 2.0 / 2.1时代(当<f:viewAction>不存在时)被滥用以在设置<f:viewParam>值之后具有钩子来调用侦听器。 It's just for self-documentary purposes being placed in the same <f:metadata> as where all <f:viewParam> s are. 它只是出于自我记录的目的,与所有<f:viewParam>放在相同的<f:metadata>中。

So, just get rid of it. 因此,摆脱它。

<f:event type="preRenderView" listener="#{editor.onload(includeParam)}" />

That said, postAddToView is likely a better event to hook this all on. 就是说, postAddToView可能是一个更好的选择。 And to avoid "Duplicate component ID" errors over all place later on, consider wrapping it in <f:subview> or making it a composite. 并且为了避免以后在所有位置出现“重复的组件ID”错误,请考虑将其包装在<f:subview>或将其组合。

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM