java JSF:有条件地呈现列表项 (<li>)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7465298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:05:15  来源:igfitidea点击:

JSF: conditionally render a list item (<li>)

javajsfrendering

提问by George Armhold

I just inherited a project implemented in JSF. I have the following code which looks fine in Chrome, but Firefox renders the borders on the "empty" list items:

我刚刚继承了一个在 JSF 中实现的项目。我有以下代码在 Chrome 中看起来不错,但 Firefox 在“空”列表项上呈现边框:

<ul>
    <li><a href="/home">Home</li>
    <li>
        <s:link view="/signup.xhtml" rendered="#{someCondition}">Sign Up</s:link>
    </li>
    <!-- etc... -->
</ul>

Which ends up looking like:

最终看起来像:

enter image description here

在此处输入图片说明

Is there a JSF tag to conditionally render the <li>?

是否有 JSF 标记来有条件地呈现<li>?

回答by mrembisz

If you do it as in @CoolBeans example, you will get a <span> around your <li>. In some cases it might disrupt your layout, besides you don't really want an extra tag under <ul>. To get rid of it, use <ui:fragment rendered="#{condition}" />around your item instead of <h:panelGroup>.

如果您按照@CoolBeans 示例进行操作,您将在 <li> 周围得到一个 <span>。在某些情况下,它可能会破坏您的布局,此外您真的不希望 <ul> 下有一个额外的标签。要摆脱它,请<ui:fragment rendered="#{condition}" />在您的项目周围使用而不是 <h:panelGroup>。

Also you can use style attribute to hide an item:

您也可以使用 style 属性来隐藏项目:

<li style="display: #{condition ? 'list-item' : 'none'};" />

回答by CoolBeans

No liis vanilla html, not a jsf component.

不,li是 vanilla html,而不是 jsf 组件。

You can hackit by putting a <h:panelGroup />around the lielement and setting the renderedproperty on it.

您可以通过在元素周围放置 a并在其上设置属性来破解它。<h:panelGroup />lirendered

ie.

IE。

 <h:panelGroup rendered="#{someCondition}">
   <li>
      <s:link view="/signup.xhtml">Sign Up</s:link>
   </li>
 </h:panelGroup>

Another option is to use <f:verbatim rendered="#{someCondition}" >, but keep in mind that it has been deprecated in JSF 2.0.

另一种选择是使用<f:verbatim rendered="#{someCondition}" >,但请记住,它在 JSF 2.0 中已被弃用。

回答by Antoine Fontaine

You could also use the core JSTL library:

您还可以使用核心 JSTL 库:

<c:if test="#{someCondition}">
    <li>
        <s:link view="/signup.xhtml">Sign Up</s:link>
    </li>
</c:if>

I think using this core tag makes the code easier to understand than using the panelGroup tag.

我认为使用这个核心标签比使用 panelGroup 标签更容易理解代码。