简体   繁体   中英

How to access getColumnNames from JSP in EL?

I'm using the JSTL SQL tag library to perform small SQL queries from within a JSP.

I normally retrieve the query results in EL by using rowsByIndex and then iterating over the returned collection:

<c:forEach var="row" items="${myQuery.rowsByIndex}">
    <p>${row[0]}, ${row[1]}, ${row[2]}, ${row[3]}</p>
</c:forEach>>

I want to do something similar for the column names.

I see that there is a method on the result interface:

public String[] getColumnNames();

and I'm trying to figure out how to access this from EL.

I'm trying to do something like:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
...
  <table>
    <tr>
        <c:forEach var="columnName" items="${helloQuery.getColumnNames}">
            <th><c:out value="${columnName}" /></th>
        </c:forEach>
    <tr>
  </table>
...

But no joy..

EL relies on the Javabeans specification and its naming conventions when it comes to accessing properties. You basically only need to specify the property name (even though it's virtual) and EL will turn it into a getter call when the value needs to be returned, or into a setter call when the value needs to be set (in JSF #{} syntax only though).

So, ${helloQuery.getColumnNames} is basically trying to invoke getGetColumnNames() method on the class represented by ${helloQuery} . But there is none and this would result in a PropertyNotFoundException . You need to change it to ${helloQuery.columnNames} in order to invoke the getColumnNames() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM