当属性名称本身是动态变量时如何获取bean属性的值

发布于 2024-09-13 06:30:05 字数 1331 浏览 6 评论 0原文

我正在尝试编写一个自定义 JSPX 标记,该标记从给定列表中的每个对象读取给定 bean 属性的值,并将该属性的名称作为 JSP 属性传递给该标记。该标记看起来像这样:

<jsp:root xmlns:c="http://java.sun.com/jsp/jstl/core"
        xmlns:jsp="http://java.sun.com/JSP/Page"
        version="2.0">
    <jsp:output omit-xml-declaration="yes"/>

    <jsp:directive.attribute name="items" type="java.lang.Iterable"
        required="true" description="The items whose properties are to be read"
        rtexprvalue="true"/>
    <jsp:directive.attribute name="propertyName" type="java.lang.String"
        required="true" description="The name of the bean property to read"
        rtexprvalue="true"/>

    <c:forEach items="${items}" var="item">
        <!-- This is the bit that doesn't work -->
        <jsp:getProperty name="item" property="${propertyName}" />
    </c:forEach>

</jsp:root>

问题是 jsp:getProperty 标记的 property 属性似乎不接受表达式,只接受文字值。所以这可以工作,但对我来说没有用(因为我直到运行时才知道属性名称):

<jsp:getProperty name="item" property="firstName" />

我得到的错误是:

org.apache.jasper.JasperException: org.apache.jasper.JasperException:
PWC6054: Cannot find any information on property '${propertyName}' in
a bean of type 'com.example.FooBar'

感谢您的帮助。

I'm trying to write a custom JSPX tag that reads the value of a given bean property from each object in a given list, with the name of that property passed to the tag as a JSP attribute. The tag would look something like this:

<jsp:root xmlns:c="http://java.sun.com/jsp/jstl/core"
        xmlns:jsp="http://java.sun.com/JSP/Page"
        version="2.0">
    <jsp:output omit-xml-declaration="yes"/>

    <jsp:directive.attribute name="items" type="java.lang.Iterable"
        required="true" description="The items whose properties are to be read"
        rtexprvalue="true"/>
    <jsp:directive.attribute name="propertyName" type="java.lang.String"
        required="true" description="The name of the bean property to read"
        rtexprvalue="true"/>

    <c:forEach items="${items}" var="item">
        <!-- This is the bit that doesn't work -->
        <jsp:getProperty name="item" property="${propertyName}" />
    </c:forEach>

</jsp:root>

The problem is that the property attribute of the jsp:getProperty tag doesn't seem to accept an expression, only a literal value. So this would work, but is no use to me (as I don't know the property name until runtime):

<jsp:getProperty name="item" property="firstName" />

The error I get is:

org.apache.jasper.JasperException: org.apache.jasper.JasperException:
PWC6054: Cannot find any information on property '${propertyName}' in
a bean of type 'com.example.FooBar'

Thanks for any help.

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

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

发布评论

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

评论(1

暮光沉寂 2024-09-20 06:30:06

如果要使用动态属性名称,请使用大括号表示法。

<c:forEach items="${items}" var="item">
    ${item[propertyName]}
</c:forEach>

另请参阅:

If you want to use dynamic property names, use the brace notation.

<c:forEach items="${items}" var="item">
    ${item[propertyName]}
</c:forEach>

See also:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文