如何保存稍后再次使用的动作参数 [英] How to store parameters for action to be used again later

查看:125
本文介绍了如何保存稍后再次使用的动作参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以排序,搜索和过滤的列表视图。从该列表视图中,用户可以通过多个步骤编辑项目。最后,在编辑和检查更改后,用户返回列表。现在我希望列表能够使用用户之前设置的相同的排序,搜索词和过滤器,并显示正确的结果。

多个参数如何排序(搜索,过滤器)存储和重用时显示列表动作?

可能令人不满意的方式,我想到:




  • 传递所有需要的参数。如果在两个列表操作调用之间涉及多个操作,则很难奏效

  • 保存会话对象中的参数。这似乎需要很多代码来处理多个参数(检查参数是否传递给动作,存储新值,如果参数未传递,请从会话获取旧参数,处理空字符串参数):

      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只能匹配相关的控制器。
}

static final List< String> sessionParams = ['myParam','otherParam','coolParam']

boolean before(){
sessionParams.each {
//如果请求包含param,则设置它在会话中
if(params.containsKey(it)){
session [it] = params [it]
} else {
//否则,从会话中获取值(如果不存在,它将为空)
params [it] = session [it]
}
}

true
}
}

静态 sessionParams 包含参数你想从会话中存储/检索。

如果 params 包含列表中的一个元素,它将存储在 session 在同一个名字下。如果不是,它是从 session (假设它存在)。



在你的控制器中,你现在可以只要像你一样会访问 params.getLong('theParam')>。您也可以使用Grails参数转换:

  def myAction(Long theParam){

}

保存了很多LOC。


I have a list view that can be sorted, searched and filtered. From that list view the user can edit items in multiple steps. Finally after editing and reviewing the changes the user goes back to the list. Now I want the list to use the same sorting, search term and filters that the user set before and show the correct results.

How can multiple paramters (sorting, search, filter) be stored and reused when showing the list action?

Possible unsatisfactory ways that I thought of:

  • pass through all the needed parameters. Does work hardly if there are multiple actions involved between the two list action calls
  • save the parameters in the session object. This seems to require a lot of code to handle multiple parameters (check if parameter was passed to action, store new value, if parameter was not passed, get old parameter from session, handle empty string parameters):

    Long longParameter
    if(params.containsKey('longParameter')) {
        longParameter = params.getLong('longParameter')
        session.setAttribute('longParameter', longParameter)
    } else {
        longParameter = session.getAttribute('longParameter') as Long
        params['longParameter'] = longParameter
    }
    

解决方案

If you want to make it more generic you could use an Interceptor instead.

This could perhaps be generalized like this:

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
    }
}

The static sessionParams holds the parameters you want to store/retrieve from session.

If the params contains an element from the list, it is stored in session under the same name. If not, it is taken from session (given that it exists).

In your controller, you can now just access params.getLong('theParam') like you always would. You could also use Grails parameter conversion:

def myAction(Long theParam) {

}

Lots of LOC saved.

这篇关于如何保存稍后再次使用的动作参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆