我正在尝试编写一个自定义 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>
问题是property
的属性jsp:getProperty
标签似乎不接受表达式,只接受文字值。所以这可以工作,但对我来说没有用(因为我直到运行时才知道属性名称):
<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'
谢谢你的帮助。
如果要使用动态属性名称,请使用大括号表示法。
<c:forEach items="${items}" var="item">
${item[propertyName]}
</c:forEach>
也可以看看:
- Jakarta EE 教程 - 表达式语言 - 引用对象属性 https://eclipse-ee4j.github.io/jakartaee-tutorial/#referencing-object-properties-or-collection-elements
- Jakarta EE 教程 - 表达式语言 - EL 表达式示例 https://eclipse-ee4j.github.io/jakartaee-tutorial/#examples-of-el-expressions
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)