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:
- better overview
- easier access to services and other helper classes
- easier to maintain
- encapsulate your login with serivces for reusability
- ...
if you want to this in your gsp you can use a hql query, e.g.:
Author.executeQuery("select a from Author as a")
精彩评论