8

指定されたリスト内の各オブジェクトから指定されたBeanプロパティの値を読み取るカスタムJSPXタグを作成しようとしています。そのプロパティの名前は、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'

助けてくれてありがとう。

4

1 に答える 1

16

動的プロパティ名を使用する場合は、中括弧表記を使用してください。

<c:forEach items="${items}" var="item">
    ${item[propertyName]}
</c:forEach>
于 2010-08-04T01:58:36.720 に答える