Grails - 使用ajax组件保存状态/ Web流

时间:2012-03-12 17:37:34

标签: grails

如果我有一个基于某些ajax组件生成搜索结果的页面以及对控制器方法的调用,那么保存该页面状态(使用搜索结果)的最佳方法是什么,以便我可以在任何页面返回在会话中指向再次查看该页面?

1)搜索,获得结果 2)在另一页面上进行一些更改 3)返回搜索结果,查看与1相同的页面。

我读过关于网络流程(感觉不必要且比我需要的更正式)和AJAX保存状态,但似乎也错了。还看到Grails没有开箱即用的功能(这对于Web框架来说似乎很奇怪)。

修改

如何为该会话保留searchResults列表,该列表由我的控制器的调用表填充?我想我只是错过了我的逻辑和会话之间的桥梁。

class SearchService {

boolean transaction = false
static scope = 'session'
def searchResults

private Contact[] updateContactSearchList(String ns, Company c, String si, String res) {

    def nameSearch = ns
    def company = Company.get(c?.id)
    def showInactives = si
    def reset = res

    if(res == "true") {
        render(template:'searchResults', model: [searchResults:"", total: 0])
        return
    }

    if(showInactives == "on" && nameSearch == "" && company != null) {

        searchResults = Contact.withCriteria {
            eq('company', company)
            and {
                eq('isActive', false)
            }
        }
       searchResults.sort{it.lastName}
       return searchResults 
    }
    else if(showInactives == "on" && nameSearch == "" && company == null) {

        searchResults = Contact.withCriteria {
            eq('isActive', false)
        }
       searchResults.sort{it.lastName}
       return searchResults 
    }
    else if(showInactives == "on" && nameSearch != "" && company != null) {

       searchResults = Contact.withCriteria {
            eq('company', company)
            and {
                eq('isActive', false)
            }
            or {
                ilike('firstName', '%' + nameSearch + '%')
                ilike('lastName', '%' + nameSearch + '%')
                ilike('fullName', '%' + nameSearch + '%')
            }
        }
       searchResults.sort{it.lastName}
       return searchResults 
    }
    else if(showInactives == "on" && nameSearch != "" && company == null) {

        searchResults = Contact.withCriteria {
            eq('isActive', false)
            or {
                ilike('firstName', '%' + nameSearch + '%')
                ilike('lastName', '%' + nameSearch + '%')
                ilike('fullName', '%' + nameSearch + '%')
            }
        }
       searchResults.sort{it.lastName}
       return searchResults

    }

    else if(showInactives == null && nameSearch == "" && company != null) {

        searchResults = Contact.withCriteria {
            eq('isActive', true)
            and {
                eq('company', company)
            }
        }
       searchResults.sort{it.lastName}
       return searchResults

    }
    else if(showInactives == null && nameSearch == "" && company == null) {

        searchResults = Contact.withCriteria {
            eq('isActive', true)
        }
       searchResults.sort{it.lastName}
       return searchResults

    }
    else if(showInactives == null && nameSearch != "" && company != null) {

        searchResults = Contact.withCriteria {
            eq('isActive', true)
            and {
                eq('company', company)
            }
            or {
                ilike('firstName', '%' + nameSearch + '%')
                ilike('lastName', '%' + nameSearch + '%')
                ilike('fullName', '%' + nameSearch + '%')
            }
        }
       searchResults.sort{it.lastName}
       return searchResults

    }
    else if(showInactives == null && nameSearch != "" && company == null) {

        searchResults = Contact.withCriteria {
            eq('isActive', true)
            or {
                ilike('firstName', '%' + nameSearch + '%')
                ilike('lastName', '%' + nameSearch + '%')
                ilike('fullName', '%' + nameSearch + '%')
            }
        }
       searchResults.sort{it.lastName}
       return searchResults

    }
    else {
        //log error
    }
}

}

1 个答案:

答案 0 :(得分:0)

我建议创建一个会话范围的服务,例如:

class SearchService {
    static scope = "session"
    def results

}

这将处理对搜索引擎的请求并将它们存储在“结果”对象中。然后,Web组件的javascript应该直接访问和呈现此对象,而无需重新提交查询 - 前提是您需要相同的结果。