tags:

views:

1213

answers:

3

This is a generic way to select data from a table and show the results in an HTML table using JSP taglibs. What is the generic way to do this in Grails? That is, take a few lines of SQL and generate an HTML table from scratch in Grails, including the column names as headers.

<sql:query var="results" dataSource="${dsource}">
    select * from foo
</sql:query>
(# of rows: ${results.rowCount})
<table border="1">
    <!-- column headers -->
    <tr bgcolor=cyan>
        <c:forEach var="columnName" items="${results.columnNames}">
            <th><c:out value="${columnName}"/></th>
        </c:forEach>
    </tr>
    <!-- column data -->
    <c:forEach var="row" items="${results.rowsByIndex}">
        <tr>
            <c:forEach var="column" items="${row}">
                <td><c:out value="${column}"/></td>
            </c:forEach>
        </tr>
    </c:forEach>
</table>
A: 

You could use the taglib as described. You just need to make Grails aware of it.

Ken Gentle
+2  A: 

This question could be rephrased as, how do I write code like a naive ASP.NET developer? (Not all ASP.NET developers are naive, some can be quite good)

  1. Any static method is available to you in a gsp page. You can use any MyDomain.findBy, list(), count you want from page scope.
  2. You could also use a criteria query MyDomain.createCriteria() You can then pass the result to a taglib.

    But all of this seems to violate MVC principles. Your view should just be a view....

What you asked really isn't idomatic to a grails application. I realize it is commonplace in the Microsoft world, execute a query, pass it to a datagrid....But there is no direct analouge in Grails and probably for good reason

DanielHonig
A: 

Well, it would be great if you told us how one can make grails aware of it!