如何显示 HQL 结果以在 grails 中查看。?
How to display HQL result to view in grails.?
我有两个简单的 类 员工和部门,我想使用 HQL 按部门列出员工,然后显示在视图中。
首先是域类员工和部门
class Staff {
String fullName
String dateOfBirth
static belongsTo = [department: Department]
}
class Department {
String department
static hasMany = [staff: Staff]
}
部门有海、陆、空等实例。
这里有一个StaffController.groovy(仅以listbysea动作为例)
def listbysea() {
params.max = Math.min(params.max ? params.int('max') : 10, 100)
//Query
def staffList = Staff.executeQuery("SELECT s.fullName from Staff s join s.department d WHERE d.department = 'Sea')
[staffInstance: staffList, staffInstanceTotal: staffList.size()]
}
这是我的listbysea.gsp
<table class="table table-striped table-hover table-bordered">
<thead>
<tr>
<g:sortableColumn property="fullName" title="${message(code: 'staff.fullName.label', default: 'Full Name')}" />
<g:sortableColumn property="dateOfBirth" title="${message(code: 'staff.dateOfBirth.label', default: 'Date of Birth')}" />
</tr>
</thead>
<tbody>
<g:each in="${staffList}" status="i" var="staffInstance">
<tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
<td style="vertical-align: middle;"><g:link action="show" id="${staffInstance.id}"> ${fieldValue(bean: staffInstance, field: "fullName")}</g:link></td>
<td style="vertical-align: middle;"> ${fieldValue(bean: staffInstance, field: "dateOfBirth")}</td>
</tr>
</g:each>
</tbody>
</table>
但是,table 中没有显示任何数据,我不确定查询是否确实没有产生任何结果,或者是视图的问题。所以我问我在将查询结果返回到视图时做的正确吗?我什至试过这个查询
def staffList = Staff.executeQuery("SELECT new map(s.fullName as fullName, d.department as department)\
FROM Staff as s, Department as d \
WHERE s.department = d HAVING s.department = ('Sea')")
但是还是没有显示结果
感谢任何提示。
您在控制器中的变量名与 gsp 文件使用的不匹配。
在您使用的控制器中staffInstance
:
def staffList = Staff.executeQuery("SELECT s.fullName from Staff s join s.department d WHERE d.department = 'Sea')
[staffInstance: staffList, staffInstanceTotal: staffList.size()]
但是在gsp中你使用staffList
:
<g:each in="${staffList}" status="i" var="staffInstance">
尝试将控制器更改为:
[staffList: staffList, staffInstanceTotal: staffList.size()]
我有两个简单的 类 员工和部门,我想使用 HQL 按部门列出员工,然后显示在视图中。
首先是域类员工和部门
class Staff {
String fullName
String dateOfBirth
static belongsTo = [department: Department]
}
class Department {
String department
static hasMany = [staff: Staff]
}
部门有海、陆、空等实例。
这里有一个StaffController.groovy(仅以listbysea动作为例)
def listbysea() {
params.max = Math.min(params.max ? params.int('max') : 10, 100)
//Query
def staffList = Staff.executeQuery("SELECT s.fullName from Staff s join s.department d WHERE d.department = 'Sea')
[staffInstance: staffList, staffInstanceTotal: staffList.size()]
}
这是我的listbysea.gsp
<table class="table table-striped table-hover table-bordered">
<thead>
<tr>
<g:sortableColumn property="fullName" title="${message(code: 'staff.fullName.label', default: 'Full Name')}" />
<g:sortableColumn property="dateOfBirth" title="${message(code: 'staff.dateOfBirth.label', default: 'Date of Birth')}" />
</tr>
</thead>
<tbody>
<g:each in="${staffList}" status="i" var="staffInstance">
<tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
<td style="vertical-align: middle;"><g:link action="show" id="${staffInstance.id}"> ${fieldValue(bean: staffInstance, field: "fullName")}</g:link></td>
<td style="vertical-align: middle;"> ${fieldValue(bean: staffInstance, field: "dateOfBirth")}</td>
</tr>
</g:each>
</tbody>
</table>
但是,table 中没有显示任何数据,我不确定查询是否确实没有产生任何结果,或者是视图的问题。所以我问我在将查询结果返回到视图时做的正确吗?我什至试过这个查询
def staffList = Staff.executeQuery("SELECT new map(s.fullName as fullName, d.department as department)\
FROM Staff as s, Department as d \
WHERE s.department = d HAVING s.department = ('Sea')")
但是还是没有显示结果
感谢任何提示。
您在控制器中的变量名与 gsp 文件使用的不匹配。
在您使用的控制器中staffInstance
:
def staffList = Staff.executeQuery("SELECT s.fullName from Staff s join s.department d WHERE d.department = 'Sea')
[staffInstance: staffList, staffInstanceTotal: staffList.size()]
但是在gsp中你使用staffList
:
<g:each in="${staffList}" status="i" var="staffInstance">
尝试将控制器更改为:
[staffList: staffList, staffInstanceTotal: staffList.size()]