如何使用映射器将自定义查询字符串附加到grails资源? [英] How do I append custom query strings to grails Resources using a mapper?

查看:111
本文介绍了如何使用映射器将自定义查询字符串附加到grails资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个将查询字符串附加到actualUrl的Grails资源映射器。迄今为止所有的尝试都失败了 - 但也许我会以错误的方式去做事。示例代码如下;

I would like to create a Grails resources mapper that appends a query string to the actualUrl. All attempts thus far have failed - but maybe I'm going about things the wrong way. Example code follows;

import org.grails.plugin.resource.mapper.MapperPhase
import org.apache.commons.logging.LogFactory

class GlobalizationResourceMapper {

    def log = LogFactory.getLog(this.class)
    def phase = MapperPhase.RENAMING
    def operation = "globalizationquerystring"
    def priority = -1

    static defaultExcludes = ['**/*.png', '**/*.gif', '**/*.jpg', '**/*.jpeg', '**/*.gz', '**/*.zip']
    static defaultIncludes = ['**/*.js','**/*.gsp']

    /**
     * Cache the resource based on current language in use. Do this by appending a query string on the resource URL indicating language.
     */
    def map(resource, config) {
        def query = [l:'en_US']
        resource.actualUrl = resource.actualUrl + '?' + query.collect { it }.join('&')
        if (log.debugEnabled) log.debug "Modified URL: ${resource.actualUrl}"
        log.info "Modified URL: ${resource.actualUrl}"
    }
}

但任何请求的资源都会以重定向循环和404结束。

The actual URL gets changed appropriately, but any resource requested with it ends up in a redirect loop and 404's.

推荐答案

您可以将参数追加到 * Resources.groovy 即在资源被声明的地方(而不是在映射器中)使用:

You can append the params to the URL in *Resources.groovy i.e. at the point where the resource is declared (rather than in the mapper) using:

resource url: '/js/foo.js', wrapper: { baseUrl ->          
  def query = [l:'en_US']
  baseUrl + '?' + query.collect { it }.join('&')
}

这篇关于如何使用映射器将自定义查询字符串附加到grails资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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