JSF 设置 UI:param 或使用在 ui:repeat 中动态更改的值



我想更新一个ui:param值或在ui:repeat循环中使用类似的东西。这个想法如下(只是一个 aprox,不是最终实现,但我想是可以理解的):

<ui:param name="marginLeftNode" value="#{myBean.initialMarginValue}" />
<ui:repeat value="#{myBean.viewWrapper.linkMap.entrySet().toArray()}" var="map">
    <ui:repeat var="link" value="#{map.value}" varStatus="status">
        <li style="margin-left: #{marginLeftNode}%" class="ico-#{link.getStyleCss()}-ayuda">
            <a href="#{link.getUrlLink()}">#{link.getTitle()}</a>
        </li>
        <!-- Code conditions that doesn't works in any way as I've readed in the links after code -->
        <c:if test="#{marginLeftNode gt 4}">
            <ui:param name="marginLeftNode" value="#{myBean.viewWrapper.nodeList.get(status.index).depth}" />
        </c:if>
        <ui:fragment rendered="#{marginLeftNode gt 4}">
            <ui:param name="marginLeftNode" value="#{myBean.viewWrapper.nodeList.get(status.index).depth}" />
        </ui:fragment>
        <!-- End code conditions: these are the two solutions c:if and ui:fragmen I tried -->
    </ui:repeat>
</ui:repeat>

我不能在 ui:repeat 中使用 c:if,因为不起作用(在 <ui:repeat> 中指定元素的条件渲染?<c:if>似乎不起作用),我不能将ui:fragment与ui:param一起使用,因为它也不起作用(JSF中的条件变量定义)

那么,知道如何解决这个问题吗?

首先,如果可以使用JSF,请避免使用JSTL。这里有一个答案,解释了JSTL和JSF是如何在不同的步骤中执行的-> https://stackoverflow.com/a/3343681/4253629

例如,要有条件地重新定义 ui:param,您可以执行以下操作:

<ui:param
    name="marginLeftNode" 
    value="#{marginLeftNode gt 4 ? myBean.viewWrapper.nodeList.get(status.index).depth : marginLeftNode }"/>

可能存在另一种解决方案,但这有效。

问候

更改循环中的ui:param没有实际价值。ui:param的真正目的是将运行时变量传递到模板客户端或包含的文件中。当您的参数传入时,更改它几乎没有价值。如果你想要的只是在变量被传递后有条件地改变它的值,你可以使用 JSTL 的c:set来设置一个页面范围的变量,然后你可以使用它

   <ui:repeat value="#{myBean.viewWrapper.linkMap.entrySet().toArray()}" var="map">
   <ui:repeat var="link" value="#{map.value}" varStatus="status">
    <li style="margin-left: #{marginLeftNode}%" class="ico-#{link.getStyleCss()}-ayuda">
        <a href="#{link.getUrlLink()}">#{link.getTitle()}</a>
    </li>
        <c:if test="#{marginLeftNode gt 4}">
              <c:set var="marginLeftNode" value="#{myBean.viewWrapper.nodeList.get(status.index).depth}"/>   
        </c:if> 

</ui:repeat>

然后,您可以在该视图中的任何位置#{marginLeftNode}访问您的设置变量

最新更新