Reputation: 545
I have followed several examples, but I can't seem to get the final part of displaying the query to work. I am not getting any errors, but it is not displaying anything even though the query works in SQL and I know its valid. I can't figure out what I'm missing. Any help greatly appreciated.
My controller -
import groovy.sql.Sql
class V1Controller {
def dataSource
def index() {
def nameList = {
def db = new Sql(dataSource)
def results = db.rows("SELECT NAME FROM table")
//results.each{row -> log.debug .name}
[results:results]
}
}
}
My view -
<p>
Results:
<g:each in="${results}">
<tr>Row - <td>${results.name}</td></tr>
</g:each>
</p>
Thanks for the responses, I have tried these two ways and am still not getting any results in the view.
<g:each in="${results}">
<tr>${it.NAME}</tr>
</g:each>
<g:each in="${results}" var="r">
<tr>Name - ${r.'NAME'}</tr>
</g:each>
Thanks Joshua! I used your recommendation on the view, and I also had to comment out the other action name 'nameList', I think realized I was trying to create a nested action and didn't need it. It works now!
def index() {
//def nameList = {
def db = new Sql(dataSource)
def results = db.rows("SELECT NAME FROM MIT_TEST_NAME")
//results.each{row -> log.debug .name}
return [results:results]
db.close()
//}
}
Upvotes: 0
Views: 477
Reputation: 24776
There are two issues here. First you aren't using the <g:each>
tag correctly, and secondly your property name isn't correct according to your SQL statement.
<g:each in="${results"} var="r">
<tr>
<td>${r.'NAME'}</td>
</tr>
</g:each>
As you can see above you have to give the tag a variable name (in this case) so it knows how to address the individual element in scope of the loop. I picked r
just for an example. Secondly, you will notice that when you access the column name it's in single quotes and matches the case of the column returned in your SQL statement.
This should resolve your issues.
Upvotes: 1