在页面上处理 Java 表达式语言

发布于 2024-10-04 05:48:49 字数 1618 浏览 2 评论 0 原文

我正在开发一个项目的前端,该项目为我提供了可以使用的 Java 表达式语言标签。在一种情况下,我需要查看它是否返回一个数组或仅返回一位数据,但我不知道如何使用它。

示例:

该页面

<p>${WebAppContext.buildings[0].location.name}</p>

将输出类似以下内容:

<p>Acme</p>

问题是,如果该建筑物中有更多内容,我需要输出更多内容:

类似于(伪代码):

if isArray(${WebAppContext.buildings}){
 foreach(${WebAppContext.buildings} as foo){
    //iterate over whatever is in the array
}
}

所以我可以输出类似以下内容:

<p>${WebAppContext.buildings[0].location.name}</p>
<p>${WebAppContext.buildings[1].location.name}</p>

我询问了负责生成此内容的 Java 人员代码,他们说“Dunnokindofbusyrightnowbuhbye”。所以我希望你们能有一些见解。

除了将代码粘贴在页面中之外,我不知道如何使用这种 Java 表达式语言(我什至必须查找它才能知道它到底叫什么)。所以任何建议/资源都会有帮助。


编辑:

我已经尝试了以下操作,但没有得到任何结果:

<c:forEach var='building' items='${WebAppContext.buildings}'>
  <p>${building.location.name}</p>
</c:forEach>

在页面的源代码中它只是显示:

<c:forEach var='meeting' items='[Lorg.foo.bar.baz.bat.serviceapi.webserviceobject.xsd.BuildingsWSO;@3823ff8'>
  <p></p>
</c:forEach>

我承认,我对 Java 表达式语言一无所知,我不知道理解为什么 items='' 会像它一样被翻译,尽管我可以看到它遵循我们使用的设置中的路径。 现在当我使用:

<p>${WebAppContext.buildings[0].location.name}</p>
<p>${WebAppContext.buildings[1].location.name}</p>

我得到:

<p>Krustylu Studios</p>
<p>Springfield Nuclear Power Plant</p>

I am working on the frontend of a project that gives me Java Expression Language tags to work with. In one instance I need to see if it is returning an array or just one bit of data and I don't know how to work with it.

Example:

The page has

<p>${WebAppContext.buildings[0].location.name}</p>

which will output something like:

<p>Acme</p>

Problem is I need to output more if there is more in that buildings bit:

Something like (in pseudocode):

if isArray(${WebAppContext.buildings}){
 foreach(${WebAppContext.buildings} as foo){
    //iterate over whatever is in the array
}
}

so I can output something like:

<p>${WebAppContext.buildings[0].location.name}</p>
<p>${WebAppContext.buildings[1].location.name}</p>

I asked the Java people responsible for generating this code and they said "Dunnokindofbusyrightnowbuhbye." so I'm hoping you folks will have some insight.

Beyond sticking the code in the page I don't have a clue how to work with this Java Expression Language (I even had to look it up to see what the heck it is called). So any advice/resources would be helpful.


EDIT:

I've tried the following and am not getting any results:

<c:forEach var='building' items='${WebAppContext.buildings}'>
  <p>${building.location.name}</p>
</c:forEach>

In the source of the page it just shows:

<c:forEach var='meeting' items='[Lorg.foo.bar.baz.bat.serviceapi.webserviceobject.xsd.BuildingsWSO;@3823ff8'>
  <p></p>
</c:forEach>

I'll admit that, not knowing anything about Java Expression Language, I don't understand why the items='' gets translated like it does though I can see that it follows a path in the setup we use.
Now when I use:

<p>${WebAppContext.buildings[0].location.name}</p>
<p>${WebAppContext.buildings[1].location.name}</p>

I get:

<p>Krustylu Studios</p>
<p>Springfield Nuclear Power Plant</p>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

杀手六號 2024-10-11 05:48:49

我不认为 EL 支持这种高级功能;您可以尝试使用 JSTL c:forEach 标签来迭代您的列表。

I don't think that kind of advanced functionality is supported by EL; you can try using the JSTL c:forEach tag to iterate over your list.

我偏爱纯白色 2024-10-11 05:48:49

如果您看到 时您从浏览器查看页面源代码,这意味着 标记未被处理。

确保您已在 JSP 页面中声明了标记库:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

prefix="c" 是您获取 c: 部分的位置: forEach> ...如果您说 prefix="foo" 则标记将为

If you're seeing <c:forEach var='meeting' items='[Lorg.foo.bar.baz.bat.serviceapi.webserviceobject.xsd.BuildingsWSO;@3823ff8'> when you view page source from your browser, that implies that the <c:forEach> tag isn't being processed.

Make sure you've declared the tag library in the JSP page:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

The prefix="c" is where you get the c: part of <c:forEach> ... if you were to say prefix="foo" the tag would then be <foo:forEach>

握住我的手 2024-10-11 05:48:49

这应该是可能的:

<c:forEach var='building' items='${WebAppContext.buildings}'>
  <p>${building.location.name}</p>
</c:forEach>

现在,您将如何检查 WebAppContext.buildings 是否实际上是一个数组?使用 JSTL 没有直接简单的方法可以做到这一点,除非您有能力扩展本地自定义 EL 函数套件以从 JSTL 中使用。

This should be possible:

<c:forEach var='building' items='${WebAppContext.buildings}'>
  <p>${building.location.name}</p>
</c:forEach>

Now, how will you check to see whether WebAppContext.buildings is actually an array? There's no direct easy way to do that with JSTL unless you have the power and ability to extend a local custom suite of EL functions to use from JSTL.

画骨成沙 2024-10-11 05:48:49

如果您确实需要知道某个对象是否是数组,您可以创建自定义 JSP 函数。

public static boolean isArray(final Object o) {
    return o instanceof Object[];
}

然后只需将其映射到 TLD,例如:

<function>
    <description>
        Checks if the supplied object is an array.
    </description>
    <name>isArray</name>
    <function-class>com.example.JspFunctions</function-class>
    <function-signature>boolean isArray(java.lang.Object)</function-signature>
    <example>
        ${f:isArray(someVar)}
    </example>
</function>

If you really need to know if an object is an array, you could create a custom JSP function.

public static boolean isArray(final Object o) {
    return o instanceof Object[];
}

Then just map it in a TLD such as:

<function>
    <description>
        Checks if the supplied object is an array.
    </description>
    <name>isArray</name>
    <function-class>com.example.JspFunctions</function-class>
    <function-signature>boolean isArray(java.lang.Object)</function-signature>
    <example>
        ${f:isArray(someVar)}
    </example>
</function>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文