如何在Grails搜索中返回域对象的属性?

时间:2015-07-29 13:38:26

标签: grails model-view-controller gorm

我正在编写一个相当小的项目。搜索页面由g:选择由域对象字段填充,如下所示:

<g:form action="list" method="GET">
Last Name <g:select id="lastName" name="searchedLName" from="${StudentEntry.list(sort:lastName, order:desc)}"
        noSelection="['null':'None']" optionValue="lastName" value="${params.searchedLName}"/><br/> 
Submitted By <g:select id="submitter" name="searchedSubmitter" from="${UserAuth.list(sort:userName, order:desc)}"
        noSelection="['null':'None']" optionValue="userName" value="${params.searchedSubmitter}"/><br/>
<button id="submitIt" type="submit">Search</button>

以下是域对象的片段

class StudentEntry {
     String firstName
     String lastName
     String submittedBy

String toString() {
    lastName + ", " + firstName
}

这是一些引导数据

def entry1 = new StudentEntry {
     firstName: "John"
     lastName: "Smith"
     submittedBy: "Jane Doe"
}

def entry2 = new StudentEntry {
     firstName: "James"
     lastName: "Jones"
     submittedBy: "Jane Doe"
}

以下是StudentEntryController列表的代码

def list(params) {
    def result

    println "Searched Last Name: " + params?.searchedLName
    def theLastName = params?.searchedLName
    println "Searched Last Name: " + theLastName

    println "Searched Submitter: " + params?.searchedSubmitter
    def theSubmitter = params?.searchedSubmitter
    println "Searched Submitter: " + theSubmitter

    if(params) {
       result = StudentEntry.findAllWhere(lastName: theLastName, submitter: theSubmitter)
//render...

当我运行搜索时,println语句输出与所选值关联的域对象的StudentEntry类的toString方法。例如,如果我搜索lastName =“Jones”,则println将输出Searched Last Name: Jones, James。当我想查看James Jones的条目时,我的render方法返回一个空列表。

将对象的属性而不是对象本身传递给findAllWhere方法的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

我似乎忽略了将要搜索的字段指定为g:select中的optionKey属性。 println语句现在正在输出所需的数据。