Groovy Grails - 为后续请求存储对象 [英] Groovy Grails - store object for subsequent requests

查看:108
本文介绍了Groovy Grails - 为后续请求存储对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个web应用程序,前端向服务器发送ajax请求以获取数据。在控制器中,我有以下逻辑:

I have a web application and front-end makes ajax request to server in order to get data. In controller I have following logic:

def data = []

def method() {
  def objects = []
  ...
  from params determine if it is a first request
  ...
  if (firstRequest) {
    objects = someService.getObjectFromDB()
    data = objects
  } else {
    actions with data object
  } 

但问题是,对于2+请求 data 是一个空列表,尽管在第一个请求期间我填充它与需要的信息。如何使用 data 对象是2+请求?

But the problem is that for the 2+ requests data is an empty list despite the fact that during the first request I populate it with needed information. How can I use data object is 2+ requests?

推荐答案

存储用户会话中的数据对象如下:

Store the data object in the user sessions like:

session.data = objects

而且当你输入方法时,数据检查已经存在了。

And when you enter the method check of the data is already there..

if (!session?.data) {
    // first request
    objects = someService.getObjectFromDB()
    session.data = objects
} else {
    // retrieve data from session
    def oldData = session.data
    // do something
}

这可能不是最好的解决方案,因为您会在会话中存储大量信息,因此请尽量限制为最小值。

This is probably not the best solution because you will store a lot of information in the session so try to restrict that to a minimal.

这篇关于Groovy Grails - 为后续请求存储对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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