【发布时间】:2012-03-13 03:56:34
【问题描述】:
如果我有一个基于某些 ajax 组件和对控制器方法的调用生成搜索结果的页面,那么保存该页面状态(带有搜索结果)以便我可以随时返回的最佳方法是什么指向会话以再次查看该页面?
1) 搜索,获取结果 2)在另一个页面上进行一些更改 3) 返回搜索结果,查看与左1相同的页面。
我读过关于 web 流(感觉没有必要而且比我需要的更正式)和 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
}
}
}
【问题讨论】:
-
grails 的 Redis 插件可能是一个很好的举措,如果你想缓存这样的东西(无论是出于性能目的,还是在你的情况下,出于历史目的似乎更是如此) .看看:grails.org/plugin/redis
标签: grails