【问题标题】:JSTL <c:when> and <choose> tagsJSTL <c:when> 和 <choose> 标签
【发布时间】:2014-10-22 20:42:27
【问题描述】:

我是第一次进行 JSTL 编码,我在使用 if else 条件(c:when 和 c:choose)时遇到了一些问题

我需要的if else嵌套的伪代码是

if(${not empty properties['userrgba']}){
    <div style = "background-color: ${properties['userrgba']};">
}


    else if({not empty properties['userColor']})
    {
        if({not empty properties['userColorGradient']}){
        <div style = "background-color: ${properties['userColor']};
                      background: -webkit-gradient(linear, left top, left bottom, color-stop(50%, ${properties['userColorGradient']}));" >
        }
        else{
        <div style = "background-color: ${properties['userColor']};">
        }
} //END ELSE IF

else{
<div class = ${properties['color']}>
} //END ELSE

我的 JSTL if 循环是

<c:choose>
<c:when test="${not empty properties['userrgba']}">
    <div style = "background-color: ${properties['userrgba']};">
</c:when>
<c:otherwise>
    <c:choose>
        <c:when test="${not empty properties['userColor']}">
            <c:choose>
                <c:when test="${not empty properties['userColorGradient']}">
                    <div style = "background-color: ${properties['userColor']};
                    background: -webkit-gradient(linear, left top, left bottom, color-stop(50%, ${properties['userColorGradient']}));">
                </c:when>
                <c:otherwise>
                    <div style = "background-color: ${properties['userColor']};">
                </c:otherwise>
            </c:choose>
        </c:when>
    </c:choose>
<c:otherwise> //<-- this should be </c:otherwise>
<c:otherwise>
    <div class = ${properties['color']}>
</c:otherwise>

我得到的错误是

/path/to/file/file_name.jsp(43,0) The end tag "&lt;/c:choose" is unbalanced

第 43 行是我最后一次做的地方。我思考了很多并进行了很多更改,但是每次发生类似错误时都是某行(与选择标签的关闭或何时标签有关)

有什么办法可以解决吗?

谢谢!

【问题讨论】:

    标签: jsp jstl jsp-tags


    【解决方案1】:

    更好地缩进你的代码,这样这些事情开始和结束的地方就很明显了。不正确缩进是不可能找到问题的。

    以下是重新缩进的原始代码。

    <c:choose>
        <c:when test="${not empty properties['userrgba']}">
            <div style = "background-color: ${properties['userrgba']};">
        </c:when>
        <c:otherwise>
            <c:choose>
                <c:when test="${not empty properties['userColor']}">
                    <c:choose>
                        <c:when test="${not empty properties['userColorGradient']}">
                            <div style = "background-color: ${properties['userColor']};
                            background: -webkit-gradient(linear, left top, left bottom, color-stop(50%, ${properties['userColorGradient']}));">
                        </c:when>
                        <c:otherwise>
                            <div style = "background-color: ${properties['userColor']};">
                        </c:otherwise>
                    </c:choose>
                </c:when>
            </c:choose>
        <c:otherwise> //<-- this should be </c:otherwise>
        <c:otherwise>
            <div class = ${properties['color']}>
        </c:otherwise>
    </c:choose>
    

    当然,现在显而易见的问题是:在同一个选择中同时包含两个其他选项是否有意义?

    【讨论】:

    • 感谢您建议缩进编辑,我更新了我的原始帖子。我不确定我在哪里出错了。这是我第一次做jstl。 " //"。我做了这个改变,但我现在得到一个不同的错误。

      来自 /path/to/fine/filename.jsp 中 c 的 TagLibraryValidator 的验证错误消息

      54:“c:choose”标签中“c:otherwise”标签后的“c:otherwise”非法.

    • @user3861559, c:choose 在 C/C++/Java 中类似于 case(即 switch case),c:otherwise 类似于 default:你只能有一个 @987654327 @ 每个开关盒。
    • @user3861559,问题是你的伪代码甚至是错误的。一个 if-statmenet 不能有两个 else 块。不是 else 本身(而不是 else if)。你最后的else 是错误的:删除它。
    • 嗨!我的错。我的意思是在那里有一个 if else 标签。我让它工作了。谢谢你的帮助。 pastebin.com/2n0jK7H0
    猜你喜欢
    • 2011-11-02
    • 2012-12-13
    • 2011-09-27
    • 1970-01-01
    • 2011-11-18
    • 1970-01-01
    • 1970-01-01
    • 2010-11-15
    • 1970-01-01
    相关资源
    最近更新 更多