开发者

how do we use sql queries in <g:each> in gsp page

开发者 https://www.devze.com 2023-03-24 07:24 出处:网络
Is it possible to to iterate the sql query with g:each <g:each in=\"${Books.list()}\"> instead of this how to use sql query like

Is it possible to to iterate the sql query with g:each

 <g:each in="${Books.list()}">

instead of this how to use sql query like

 select author,publisher from books,publisher where book_name="" and book_title=""开发者_JAVA技巧...


For the sake of good design lets say it's not possible.

You should stick with the principles of the MVC pattern and keep this code in your controller or domain class.

Your controller might look like this...

def list = {
    def results = Book.executeQuery(sql);
    [results:results]
}

Your view (gsp) might look like this....

<ul>
    <g:each in="${results}" var="result">
      <li>${result}</li>
    </g:each>
</ul>


it is not recommanded to this in gsp. instead you should perform data processing within your controller and then pass the result to your gsp. advantages:

  1. better overview
  2. easier access to services and other helper classes
  3. easier to maintain
  4. encapsulate your login with serivces for reusability
  5. ...

if you want to this in your gsp you can use a hql query, e.g.:

Author.executeQuery("select a from Author as a")
0

精彩评论

暂无评论...
验证码 换一张
取 消