我想创建一个Grails资源映射器,将查询字符串附加到actualUrl。到目前为止所有尝试都失败了 - 但也许我会以错误的方式处理事情。示例代码如下;
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中结束。
答案 0 :(得分:0)
您可以使用以下内容将参数附加到*Resources.groovy
中的URL,即在资源被声明的位置(而不是在映射器中):
resource url: '/js/foo.js', wrapper: { baseUrl ->
def query = [l:'en_US']
baseUrl + '?' + query.collect { it }.join('&')
}