我有一个可以排序,搜索和过滤的列表 View 。从该列表 View 中,用户可以分多个步骤编辑项目。最后,在编辑和查看更改之后,用户将返回列表。现在,我希望列表使用用户之前设置的相同排序,搜索词和过滤器,并显示正确的结果。
显示列表操作时,如何存储和重用多个参数(排序,搜索,过滤器)?
我认为的可能不令人满意的方式:
Long longParameter
if(params.containsKey('longParameter')) {
longParameter = params.getLong('longParameter')
session.setAttribute('longParameter', longParameter)
} else {
longParameter = session.getAttribute('longParameter') as Long
params['longParameter'] = longParameter
}
最佳答案
如果要使其更通用,可以改用Interceptor
。
也许可以这样概括:
class SessionParamInterceptor {
SessionParamInterceptor() {
matchAll() // You could match only controllers that are relevant.
}
static final List<String> sessionParams = ['myParam','otherParam','coolParam']
boolean before() {
sessionParams.each {
// If the request contains param, then set it in session
if (params.containsKey(it)) {
session[it] = params[it]
} else {
// Else, get the value from session (it will be null, if not present)
params[it] = session[it]
}
}
true
}
}
sessionParams
包含您要在 session 中存储/检索的参数。params
包含列表中的元素,则将其以相同的名称存储在session
中。如果没有,则从session
中获取(假设存在)。params.getLong('theParam')
。您还可以使用Grails参数转换:def myAction(Long theParam) {
}
https://stackoverflow.com/questions/40149490/