sp123
sp123

Reputation: 45

Grails- groovy sql each row to be displayed in gsp view

I want to display the result of sql each row from the service code to my gsp view.

My Service code is:

def health()  
{  
    def schemaList      = [:]  
    groovy.sql.Sql sql = new groovy.sql.Sql(dataSource);  
    sql.eachRow("SELECT SOURCE, count(1) as COUNT from fact group by SOURCE");  
    ArrayList returnResults = []  
    sqlStatement.eachRow(sqlString)
    {  
        returnResults<<it.toRowResults()   
    }   
    sqlStatement.close()   
    return[returnMap:returnResults]  
}

My Controller Code is:

def stats =  {    
   def health = AccessLogService.heath()  
   render (template:'healthview', model:[health:health])
}

My gsp view is as follows:

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <meta name="layout" content="admin" />
  <title>Health</title>
</head>

<body>
  <SCRIPT language="JavaScript">
  </SCRIPT>
  <br />
  <br />
  <font style='font-size:14px;font-weight:bold;'>Health Condition</font>
  <div id='overall'>
    <g:if test="${health.size() > 0}">
      <table border="1">
        <thead>
          <tr>
            <th>Source</th>
            <th>Count</th>
          </tr>
        </thead>
        <tbody>
          <g:each in="${health}" status="i" var="thisRecord">
            <tr>
              <td>${thisRecord.SOURCE}</td>
              <td>${thisRecord.COUNT}</td>
            </tr>
          </g:each>
        </tbody>
      </table>
    </g:if>
  </div>
</body>
</html>

I am not able to see the results of my query in gsp view? Where I am going wrong.

Upvotes: 0

Views: 1087

Answers (2)

injecteer
injecteer

Reputation: 20707

you are trying to get the wrong key of your model.

you service returns a hash [returnMap:returnResults] so your controller renders the model: [health:health] -> [health:[returnMap:returnResults]].

thus in your gsp you should refer to health.returnMap to see the list:

 <g:if test="${health.returnMap}">
   ...
      <g:each in="${health.returnMap}" status="i" var="thisRecord">
        <tr>
          <td>${thisRecord.SOURCE}</td>
          <td>${thisRecord.COUNT}</td>
        </tr>
      </g:each>
   ...
 </g:if>

UPDATE:

the code looks strange... this is how it should be:

ArrayList returnResults = []
sql.eachRow("SELECT SOURCE, count(1) as COUNT from fact group by SOURCE"){
  returnResults << it.toRowResults()
}

Upvotes: 1

dynamo
dynamo

Reputation: 1153

Where is the sqlStatement variable declared in your service? I think that is the error.
And an advice you need to debug your program. for example, Test if the service returns result by:

  • running your app debug mode

  • using log.debug

  • using println

or if you are doing these and have seen any erorrs on your console post that here.

Upvotes: 0

Related Questions