tags:

views:

64

answers:

2

Hi, can anybody tell me why this works

<g:each var="n" in="${com.pp.News.list()}">
 <h2>${n.t}</h2>
 <p>${n.tx}</p>
</g:each>

but this doesn't ?

<g:set var="news" value="${com.pp.News.findAllByShow(true,[sort:'prio', order:'desc',max:5])}" />
<g:each var="n" in="news">
   <h2>${n.t}</h2>
    <p>${n.tx}</p>
  </g:each>

Part of the exception is

Exception Message: No such property: t for class: java.lang.String 

How can I make it work?

Thanks

+1  A: 

You should make it work by putting non-UI code in the controller or a service, and passing the data to the views in the model. It's a really bad idea to do database work or other business logic in a GSP/JSP/etc. MVC is about separating concerns.

Burt Beckwith
Thanks for the reply. MVC is my preferred way to work but in this case this code goes inside a template, so there are several calling controllers and I don't want to populate the list in all of them.
xain
+4  A: 

Change

<g:each var="n" in="news">

to

<g:each var="n" in="${news}">

You are iterating over "news" instead of the returned result in the news var.

Rich Kroll