如何在c:if中使用布尔变量

Pat*_*iks 9 java jsf jstl

我在JSF中使用此代码:

<c:if test="#{sV.done.booleanValue()}">
     <option value="#{sV.id}" selected="selected">#{sV.text}</option>
</c:if>
<c:if test="#{not sV.done.booleanValue()}">
     <option value="#{sV.id}">#{sV.text}</option>
</c:if>
Run Code Online (Sandbox Code Playgroud)

sv是我的包含数据(POJO)类,做的是一个Boolean变量,我要显示option与标签selected,如果属性sV.donetrue.但我做不到.不知道我哪里错了.

否则可能会出现问题c,因为c:forEach在我的案例中,在同一页面中有一段时间没有工作?这可能是原因吗?哪里错了?

每次显示没有选中的选项标签attribute.

Roh*_*ain 7

试试这个: -

<c:if test="${sV.done == true}">...</c:if>  // or
<c:if test="${sV.done eq true}">...</c:if>  // or
<c:if test="${sV.done}">...</c:if>   // or
Run Code Online (Sandbox Code Playgroud)

而对于否定(如果sV.done为假): -

<c:if test="${! sV.done}">...</c:if>    /// OR
<c:if test="${not sV.done}">...</c:if>  /// OR
<c:if test = "${sV.done != true}">...</c:if>     // OR
<c:if test = "${sV.done ne true}">...</c:if>  // OR
Run Code Online (Sandbox Code Playgroud)

有关运营商的更多信息,请查看此链接: - JSTL if


Bal*_*usC 5

如果您使用的是EL 2.2,那么您的语法很好.那么,没有一个JSTL <c:xxx>标签被解释?您需要导入JSTL核心taglib.目前还不清楚您正在使用哪种视图技术和JSTL版本,因此这里是JSP和Facelets的导入示例.

使用JSTL 1.0的JSP:

<%@taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
Run Code Online (Sandbox Code Playgroud)

使用JSTL 1.1/1.2的JSP:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Run Code Online (Sandbox Code Playgroud)

Facelets 1.x与JSTL 1.1/1.2:

<html ... xmlns:c="http://java.sun.com/jstl/core">
Run Code Online (Sandbox Code Playgroud)

Facelets 2.x与JSTL 1.2:

<html ... xmlns:c="http://java.sun.com/jsp/jstl/core">
Run Code Online (Sandbox Code Playgroud)

也可以看看:


具体问题无关,您是否考虑过使用JSF UISelectOne组件而不是<option>自己摆弄元素?您可以在我们的h:selectOneMenu维基页面中找到一些具体示例.


Pat*_*iks -2

正如我在问题中提到的,似乎其他一些组件/库正在干扰 c:if 的工作,它在任何情况下都不起作用。我尝试了上面给出的所有建议。感谢大家的回复。