resource
Purpose
Creates a link that can be used where necessary (for example in an href, javascript, ajax call etc.)Examples
Example controller for an application called "shop":Example usages for the "shop" app:<g:resource dir="css" file="main.css" /> == /shop/css/main.css
<g:resource dir="css" file="main.css" absolute="true"/> == http://portal.mygreatsite.com/css/main.css
<g:resource dir="css" file="main.css" base="http://admin.mygreatsite.com"/> == http://admin.mygreatsite.com/css/main.css
Example as a method call in GSP only:<link type="text/css" href="${resource(dir:'css',file:'main.css')}" />
Results in:<link type="text/css" href="/shop/css/main.css" />
Description
Attributes
base
(optional) - Sets the prefix to be added to the link target address, typically an absolute server URL. This overrides the behaviour of the absolute
property, if both are specified.x≈
contextPath
(optional) - the context path to use (relative to the application context path). Defaults to "" or path to the plugin for a plugin view or template.
dir
(optional) - the name of the directory within the grails app to link to
file
(optional) - the name of the file within the grails app to link to
absolute
(optional) - If set to "true" will prefix the link target address with the value of the grails.serverURL property from Config, or http://localhost:<port> if no value in Config and not running in production.
plugin
(optional) - The plugin to look for the resource in
Source
Show Source
def resource = { attrs ->
def writer = out
writer << handleAbsolute(attrs)
def dir = attrs['dir']
if(attrs.plugin) {
writer << pluginManager.getPluginPath(attrs.plugin) ?: ''
}
else {
if(attrs.contextPath != null) {
writer << attrs.contextPath.toString()
}
else {
def pluginContextPath = pageScope.pluginContextPath
if(dir != pluginContextPath)
writer << pluginContextPath ?: ''
}
}
if(dir) {
writer << (dir.startsWith("/") ? dir : "/${dir}")
}
def file = attrs['file']
if(file) {
writer << (file.startsWith("/") || dir?.endsWith('/') ? file : "/${file}")
}
}