【问题标题】:JSTL <c:out> where the element name contains a space characterJSTL <c:out> 其中元素名称包含空格字符
【发布时间】:2009-03-19 03:50:55
【问题描述】:

我有一个可用的值数组,但不幸的是,一些变量名包含一个空格。我无法弄清楚如何在页面中简单地输出这些。我知道我没有很好地解释这一点(我是 JSP 设计者,而不是 Java 编码器)所以希望这个示例能够说明我正在尝试做的事情:

<c:out value="${x}"/>

输出到页面(人工包装)为:

{width=96.0, orderedheight=160.0, instructions=TEST ONLY. This is a test.,
 productId=10132, publication type=ns, name=John}

我可以通过使用输出名字

<c:out value="${x.name}"/>

没问题。问题是当我尝试获取“发布类型”时......因为它有一个空格,我似乎无法让&lt;c:out&gt; 显示它。

我试过了:

<!-- error parsing custom action attribute: -->
<c:out value="${x.publication type}"/>
<!-- error occurred while evaluating custom action attribute: -->
<c:out value="${x.publication+type}"/>
<!-- error occurred while parsing custom action attribute: -->
<c:out value="${x.'publication type'}"/>
<!-- error occurred while parsing custom action attribute: -->
<c:out value="${x.publication%20type}"/>

我知道真正的解决方案是正确格式化变量名(即:不带空格),但我有一段时间无法更新代码。这可以做到吗?非常感谢任何帮助。

【问题讨论】:

    标签: java jsp jstl


    【解决方案1】:

    你试过了吗:

    <c:out value="${x['publication type']}"/>
    

    我假设 Map 是其背后的 Java 类型。

    【讨论】:

    • 美丽。感谢你的回答!不到 10 分钟。哇。
    • 很高兴为您提供帮助。正如您所了解的,Java Maps 在 JSTL 中有两种不同的访问机制。但是对于一个包含空格的 String 的 Map 键,你必须使用 [] 访问方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-06
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 2015-06-05
    • 1970-01-01
    • 2015-08-10
    相关资源
    最近更新 更多