gpt4 book ai didi

Grails-groovy sql 每一行都显示在 gsp View 中

转载 作者:行者123 更新时间:2023-12-02 15:25:38 24 4
gpt4 key购买 nike

我想将服务代码中每一行的 sql 结果显示到我的 gsp View 中。

我的服务代码是:

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

我的 Controller 代码是:
def stats =  {    
def health = AccessLogService.heath()
render (template:'healthview', model:[health:health])
}

我的gsp View 如下:
<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>

我无法在 gsp View 中看到我的查询结果?我要去哪里错了。

最佳答案

您正在尝试获取模型的错误 key 。

你的服务返回一个哈希 [returnMap:returnResults]所以你的 Controller 渲染模型:[health:health] -> [health:[returnMap:returnResults]] .

因此,在您的 gsp 中,您应该引用 health.returnMap查看列表:

 <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>

更新:

代码看起来很奇怪......它应该是这样的:
ArrayList returnResults = []
sql.eachRow("SELECT SOURCE, count(1) as COUNT from fact group by SOURCE"){
returnResults << it.toRowResults()
}

关于Grails-groovy sql 每一行都显示在 gsp View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28010568/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com