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

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

问题描述

我想创建一个 Grails 资源映射器,将一个查询字符串附加到 actualUrl.迄今为止所有的尝试都失败了——但也许我的处理方式是错误的.示例代码如下;

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

实际的 URL 会适当更改,但使用它请求的任何资源都以重定向循环和 404 结束.

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

推荐答案

您可以将参数附加到 *Resources.groovy 中的 URL 中,即在资源声明的位置(而不是在映射器)使用:

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天全站免登陆