SQL查询结果不在Grails视图中显示 [英] SQL Query result not displaying in Grails view

查看:79
本文介绍了SQL查询结果不在Grails视图中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了几个例子,但我似乎无法得到显示查询工作的最后部分。我没有收到任何错误,但它不显示任何东西,即使查询在SQL中工作,我知道它是有效的。我无法弄清楚我错过了什么。任何帮助不胜感激。



我的控制器 -

 导入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]
}
}
}

我的观点 -

 < p> 
结果:
< tr>行 - < td> $ {results.name}< / td>< / tr>
< / g:每个>
< / p>

感谢您的回应,我尝试了这两种方法,但在视图中仍然没有得到任何结果。

 < g:each in =$ {results}> 
< tr> $ {it.NAME}< / tr>
< / g:每个>


< tr>名称 - $ {r.'NAME'}< / tr>
< / g:每个>

谢谢Joshua!我在视图上使用了您的建议,并且我还必须注释掉其他操作名称'nameList',我认为我正在尝试创建嵌套操作并且不需要它。它现在可以工作了!

  def index(){
// def nameList = {
def db =新的Sql(dataSource)
def results = db.rows(SELECT NAME FROM MIT_TEST_NAME)
//results.each{row - > log.debug.name}
return [results:results]
db.close()
//}
}


解决方案

这里有两个问题。首先,您没有正确使用< g:each> 标签,其次您的属性名称根据您的SQL语句不正确。

 < g:each in =$ {results} var =r> 
< tr>
< td> $ {r.'NAME'}< / td>
< / tr>
< / g:每个>

正如您在上面看到的,您必须为该标签赋予一个变量名称(在这种情况下)知道如何解决循环范围内的单个元素。我选择了 r 作为例子。其次,您会注意到,当您访问列名时,它使用单引号,并匹配您的SQL语句中返回的列的大小写。



这应该可以解决您的问题。

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()
    //}     
}

解决方案

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.

这篇关于SQL查询结果不在Grails视图中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆