【问题标题】:Grails - Saving state/Web Flow using ajax componentsGrails - 使用 ajax 组件保存状态/Web 流
【发布时间】: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


【解决方案1】:

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

class SearchService {
    static scope = "session"
    def results

}

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

【讨论】:

  • 这对我来说有点新 - 我的控制器如何调用服务来存储会话?我是否必须将我的搜索逻辑移动到服务中,然后该服务在会话中存在?
  • 所以我将我的逻辑添加到服务中 - 我只需要了解该结果对象是如何在整个应用程序会话期间保持不变的?因为现在,我离开搜索页面然后回来,什么也没有保存。再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-30
  • 1970-01-01
  • 2019-09-29
  • 2021-09-27
  • 2011-11-01
相关资源
最近更新 更多