c:何时和c:如果不起作用 [英] c:when and c:if don't work

查看:65
本文介绍了c:何时和c:如果不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以从JSF2页面访问存储在支持bean中的变量,诸如此类

I can access my variable stored in a backing bean from my JSF2 page, so things like

<h:outputText value="#{myBean.myValue}"/>

工作,该值( int 顺时针)打印就可以了. 但是,当尝试在c:if和/或c:when标签中的条件表达式中使用此值时,它永远不会等于任何东西...所以:

work, and the value (which is an int btw.) prints okay. However, when trying to use this value in conditional expressions within c:if and/or c:when tags it never equals anything... so:

<c:if test="#{myBean.myValue == 1}">
    <c:set var="myVar" value="true"/>
</c:if>

<c:choose>
    <c:when test="#{myBean.myValue > 1}">
        <c:set var="myVar" value="true"/>
    </c:when>
</c:choose>

甚至

#{myBean.myValue eq '1'} 

#{myBean.myValue == '1'}

即使值确实为1或> 1,

也永远不会取值为true.

will never evaluate to true, even if the value is indeed 1 or >1.

令人讨厌的是,比较在标签的 rendered 属性中起作用!因此:

Annoyingly the comparison works in the rendered attribute of a tag! So this:

<h:outputText value="whatever" rendered="#{myBean.myValue == 1}"/>

很好!

这是怎么回事?

更新:

这有效:

public int getMyValue() {
    return 1;
}

但这不是:

@Column(name = "orderstatus")
public int getOrderStatus() {
return orderStatus;
}

int的值正确打印,但是1 == 1为false.

The int's value is printed correctly, but 1 == 1 is false.

实体bean是否以特殊方式处理?我可以使用它们在UI上显示其价值吗?

Are entity beans handled in a special way? Can I use them for displaying their value on the UI?

UPDATE2:

<h:outputText value="#{order.adcOrderStatus.orderStatus.class}"/>

打印java.lang.Integer.

prints java.lang.Integer.

UPDATE3:

这是完整的代码:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:c="http://java.sun.com/jsp/jstl/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:cc="http://java.sun.com/jsf/composite">
    <cc:interface>
        <cc:attribute name="orderStatus" required="true"/>
    </cc:interface>
    <cc:implementation>
        <c:choose>
            <c:when test="#{cc.attrs.orderStatus == 1}" >
                <c:set var="unknownStatus" value="false"/>
            </c:when>
            <c:when test="#{cc.attrs.orderStatus == 2}" >
                <c:set var="unknownStatus" value="false"/>
            </c:when>
            <c:when test="#{cc.attrs.orderStatus == 3}" >
                <c:set var="unknownStatus" value="false"/>
            </c:when>
            <c:when test="#{cc.attrs.orderStatus == 99}" >
                <c:set var="unknownStatus" value="false"/>
            </c:when>
            <c:otherwise>
                <c:set var="unknownStatus" value="true"/>
            </c:otherwise>
        </c:choose>
        <h:graphicImage url="#{resource['img/icons/flag_yellow.png']}" rendered="#{cc.attrs.orderStatus == 1}"/>
        <h:outputText value="Created" rendered="#{cc.attrs.orderStatus == 1}"/>
        <h:graphicImage url="#{resource['img/icons/flag_orange.png']}" rendered="#{cc.attrs.orderStatus == 2}"/>
        <h:outputText value="Stopped" rendered="#{cc.attrs.orderStatus == 2}"/>
        <h:graphicImage url="#{resource['img/icons/flag_green.png']}" rendered="#{cc.attrs.orderStatus == 3}"/>
        <h:outputText value="Active" rendered="#{cc.attrs.orderStatus == 3}"/>
        <h:graphicImage url="#{resource['img/icons/flag_red.png']}" rendered="#{cc.attrs.orderStatus == 99}"/>
        <h:outputText value="Failed" rendered="#{cc.attrs.orderStatus == 99}"/>
        <h:graphicImage url="#{resource['img/icons/question_mark.png']}" rendered="#{unknownStatus}"/>
        <h:outputText value="Unknown" rendered="#{unknownStatus}"/>
    </cc:implementation>
</html>

使用int值调用时可以使用.但这不起作用:

It works when called with an int value. But this doesn't work:

    <p:dataTable value="#{cc.attrs.orders}" var="order">
        <p:column>
            <f:facet name="header">
                <h:outputText value="Status"/>
            </f:facet>
            <mytag:orderStatus orderStatus="#{order.adcOrderStatus.orderStatus}"/>
        </p:column>
    </p:dataTable>

这将显示正确的值:

<h:outputText value="#{order.adcOrderStatus.orderStatus.class}"/>

推荐答案

您是否按照以下说明声明了JSTL核心taglib?

Did you declare JSTL core taglib as follows?

<html xmlns:c="http://java.sun.com/jsp/jstl/core">

如果未声明或声明不正确,它们将不会被解析,并最终在生成的HTML输出中变成纯香草.您可以通过在浏览器中打开页面,右键单击该页面并选择查看源代码来确认.您不应在其中看到任何JSTL标记.

If not or incorrectly declared, they it simply won't be parsed and end up plain vanilla in generated HTML output. You can confirm this by opening the page in browser, rightlicking it and choosing View Source. You should not see any JSTL tag in there.

更新:根据您的更新,这与以下事实有关:JSTL在视图的构建时间运行,而JSF在视图的呈现时间运行.在您的特定情况下,如果#{cc.attrs.orderStatus}仅在视图的渲染期间可用,则所有操作都将失败.例如,当它表示迭代组件的当前迭代项时,例如<h:dataTable>.

Update: as per your update, it's related to the fact that JSTL runs during build time of the view and JSF during render time of the view. In your particular case, this all would fail if #{cc.attrs.orderStatus} only available during render time of the view. For example, when it represents the currently iterated item of an iterating component, such as <h:dataTable>.

您最好按照以下方式重写复合组件,以改用rendered属性:

You'd better rewrite the composite component as follows to use the rendered attribute instead:

<h:panelGroup rendered="#{cc.attrs.orderStatus == 1}">
    <h:graphicImage url="#{resource['img/icons/flag_yellow.png']}" /> Created
</h:panelGroup>
<h:panelGroup rendered="#{cc.attrs.orderStatus == 2}">
    <h:graphicImage url="#{resource['img/icons/flag_orange.png']}" /> Stopped
</h:panelGroup>
<h:panelGroup rendered="#{cc.attrs.orderStatus == 3}">
    <h:graphicImage url="#{resource['img/icons/flag_green.png']}" /> Active
</h:panelGroup>
<h:panelGroup rendered="#{cc.attrs.orderStatus == 99}">
    <h:graphicImage url="#{resource['img/icons/flag_red.png']}"  /> Failed
</h:panelGroup>
<h:panelGroup rendered="#{cc.attrs.orderStatus != 1 && cc.attrs.orderStatus != 2 && cc.attrs.orderStatus != 3 && cc.attrs.orderStatus != 99}">
    <h:graphicImage url="#{resource['img/icons/question_mark.png']}" /> Unknown
</h:panelGroup>

另请参见:

  • JSF2 Facelets中的JSTL ...有意义吗?
  • See also:

    • JSTL in JSF2 Facelets... makes sense?
    • 这篇关于c:何时和c:如果不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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