有条件地设置 ui:param [英] Setting ui:param conditionally

查看:23
本文介绍了有条件地设置 ui:param的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据 bean 值设置 ui:param 并且我认为使用 c:if 是个好主意.所以我在我的页面中输入了以下代码:

I want to set a ui:param depending on a bean value and I thought using c:if was a good idea. So I put in my page the following code:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    xmlns:wai="http://www.id.ethz.ch/wai/jsf"
    template="/view/listView.xhtml">

        <c:if test="#{subscriptionListController.model.listViewName eq 'mySubscriptions'}">
         <ui:param name="title" value="#{msg.subscriptionTitleMySubscriptions}"/>
        </c:if>
        <c:if test="#{subscriptionListController.model.listViewName eq 'paidSubscriptions'}">
         <ui:param name="title" value="#{msg.subscriptionTitlePaidSubscriptions}"/>
        </c:if>
        <c:if test="#{subscriptionListController.model.listViewName eq 'allSubscriptions'}">
         <ui:param name="title" value="#{msg.subscriptionTitleAllSubscriptions}"/>
        </c:if>
        ....

但是参数没有设置...

but the parameter is not set...

如果我打印出 #{subscriptionListController.model.listViewName eq 'mySubscriptions'} 的值,我在相应的情况下为真,在其他两种情况下为假.

If I let print out the value of #{subscriptionListController.model.listViewName eq 'mySubscriptions'} I get true in the corresponding case and false in the other two cases.

一开始我只有两种可能性,并用三元运算符解决了它:

At the beginning I had only 2 possibilities and solved it with the ternary operator:

<ui:param name="title" value="#{subscriptionListController.model.listViewName eq 'mySubscriptions' ? msg.subscriptionTitleMySubscriptions : msg.subscriptionTitlePaidSubscriptions}"/>

它奏效了.但现在我有了更多的可能性...

and it worked. But now I have more possibilities...

我做错了什么?

推荐答案

<ui:composition template>所示,这个页面代表一个模板客户端.

As indicated by <ui:composition template>, this page represents a template client.

之外的任何 都适用于主模板(您在 template 中声明的文件> 属性)并在模板客户端本身中被忽略.如果您打算为模板客户端内部准备变量,您应该将 放在 中.

Any <ui:param> outside <ui:define> applies to the master template (the file which you declared in template attribute) and is ignored inside the template client itself. If you intend to prepare variables for inside the template client, you should put <ui:param> inside <ui:define>.

但是还有一点:的初衷是将变量传递给所引用的文件, ,而不是在当前 facelet 上下文中准备/设置变量.对于在当前 EL 上下文中准备/设置变量的唯一功能要求,您最好使用 JSTL 来完成这项工作.您可以为此使用,但这不是它的初衷,在旧版 MyFaces 中也不能这样工作.

But there's another thing: the original purpose of <ui:param> is to pass variables to the file referenced by <ui:composition template>, <ui:decorate template> or <ui:include src>, not to prepare/set variables inside the current facelet context. For the sole functional requirement of preparing/setting variables in the current EL context, you'd better be using JSTL <c:set> for the job. You can use <ui:param> for this, but this isn't its original intent and didn't work that way in older MyFaces versions.

因此:

<ui:define>
    <c:if test="#{subscriptionListController.model.listViewName eq 'mySubscriptions'}">
        <c:set var="title" value="#{msg.subscriptionTitleMySubscriptions}"/>
    </c:if>
    <c:if test="#{subscriptionListController.model.listViewName eq 'paidSubscriptions'}">
        <c:set var="title" value="#{msg.subscriptionTitlePaidSubscriptions}"/>
    </c:if>
    <c:if test="#{subscriptionListController.model.listViewName eq 'allSubscriptions'}">
        <c:set var="title" value="#{msg.subscriptionTitleAllSubscriptions}"/>
    </c:if>
    ...
</ui:define>

<小时>

与具体问题无关,您可以按如下方式优化它,而无需不可维护的 组,该组只会随着每种订阅类型而增长:


Unrelated to the concrete problem, you can optimize this as follows without the need for an unmaintainable <c:if> group which would only grow with every subscription type:

<ui:define>
    <c:set var="subscriptionTitleKey" value="subscriptionTitle.#{subscriptionListController.model.listViewName}">
    <c:set var="title" value="#{msg[subscriptionTitleKey]}"/>
    ...
</ui:define>

用那些钥匙

subscriptionTitle.mySubscriptions = Title for my subscriptions
subscriptionTitle.paidSubscriptions = Title for paid subscriptions
subscriptionTitle.allSubscriptions = Title for all subscriptions

这篇关于有条件地设置 ui:param的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆