Reference Manual for Gracelets - 2.0.0.RC2SourceForge.net Logo
6.2 - Appendix : Variables
The below variables have precedence over normal seam context resolution. In other words a variable will be resolved to a seam context as long as it does not clash with one of the below mentioned variables.

Variables available for view scripts and libraries
NameTypeDescription
servletinterface javax.servlet.ServletContextThe current servlet context
facesclass javax.faces.context.FacesContextThe current faces context
elclass javax.el.ELResolverThe current Expression Language resolver
efclass javax.el.ExpressionFactoryThe current Expression Factory
elCtxclass javax.el.ELContextThe current Expression Language context
viewIdclass java.lang.StringThe view id of the current view
pathclass java.lang.StringThe current file system path corresponding to the current view
parentPathclass gracelets.util.FileWalkerAn easy navigational object for getting relative files to the current view
servletRootclass gracelets.util.FileWalkerAn easy navigational object for getting relative files to the servlet context
actionURLclass java.lang.StringThe url corresponding to the current view used for example with forms or links for post backs
externalclass javax.faces.context.ExternalContextThe current JSF external context
requestclass java.lang.ObjectThe current request object (an instanceof HttpServletRequest for http requests)
responseclass java.lang.ObjectThe current response object (an instanceof HttpServletResponse for http responses)
paraminterface java.util.MapThe current map that holds the posted variables for this request
paramNamesinterface java.util.CollectionA collection holding the names of the posted variables for this request
varCtxinterface java.util.MapUsed to resolve context variables that have name clashes with local variables
headersinterface java.util.MapA header name -> collection map. Generally each collection will only contain one value.
cookiesinterface java.util.MapA cookie name -> Cookie object map.
useragentclass java.lang.StringA simple convenience method allowing one to be able to quickly evaluate the user agent.
*Contextinterface gracelets.api.context.VariableContextAny variable that starts with the name of a scope in the current setup and ends with Context will give direct access to that scope

Example Variable Usage

Variable: servlet (See also: Javadoc page)

For example, to get the file system root of your web application you can do the following:

Toggle Line Numbers
1 def root = servlet.getRealPath("/") 

Variable: faces (See also: Javadoc page)

To force the current view to be rendered, you could do the following:

Toggle Line Numbers
1 faces.renderResponse() 

Variable: el (See also: Javadoc page)

To force el resolution on an object you could call the following:

Toggle Line Numbers
1 el.getValue(elCtx, someObject, "someProperty") 

Variable: ef (See also: Javadoc page)

To create a value expression object you could call the following:

Toggle Line Numbers
1 ef.createValueExpression(elCtx, "#{someExpression}", Object.class) 

Variable: elCtx (See also: Javadoc page)

To get the current variable mapper:

Toggle Line Numbers
1 def vm = elCtx.variableMapper 

Variable: viewId

This can be helpful for debugging or other purposes where you need to know what the view id is:
Toggle Line Numbers
1 log.info("Current view: $viewId") 

Variable: path

This can be helpful when needing information about the file system location of the current view:
Toggle Line Numbers
1 println "Current source for this view:" 
2 print new File(path).text 

Variable: parentPath

If you need to access files based off where your current view is located, this can be helpful. For instance if you wanted to access some xml file next to the view you could do this:

Toggle Line Numbers
1 def xml = parentPath."someFile.xml".text 

Variable: servletRoot

If you need to access files based off where your web application is located, this can be helpful. For instance if you wanted to access some xml off a certain directory in your web app:

Toggle Line Numbers
1 def xml = servletRoot."WEB-INF".conf."someFile.xml".text 

Variable: actionURL

When you need to post back and maybe want to write your own forms you can easily get the post back url with this variable (this can be especially helpful when writing library components, in particular form renderers):

Toggle Line Numbers
1 xh.form(action: actionURL, method: "post") { ... } 

Variable: external (See also: Javadoc page)

If you don't want to deal directly with the servlet implementation, you can use the faces external context object available via this variable. For example, you could get remote user info by using the following:

Toggle Line Numbers
1 print { "Current remote user: $external.remoteUser" } 

Variable: request

This can be used to access the servlet request object directly (the same as external.request). For example, to get the request headers you could do this:

Toggle Line Numbers
1 request.headerNames.each { println "$it: " + request.getHeader(it); } 

Variable: response

This can be used to access the servlet response object directly (the same as external.response). For example, to add a response header you could do this:

Toggle Line Numbers
1 response.addHeader("Pragma", "no-cache") 

Variable: param

This can be used to access posted variables for the current request. On some pages you may prefer to use only use get's together with passed parameters, maybe to show some document, you could do this:

Toggle Line Numbers
1 def data = someController.getDocument(Integer.parseInt(param['id'])).data 

Variable: paramNames

This can be used to access posted variables names for the current request. To loop through the posted variable names you could do the following:

Toggle Line Numbers
1 paramNames.each { par -> print par + "=" + param[par] } 

Variable: varCtx

When a local variable interferes with something in the variable context, or even if one of the above variables does, you can force variable context resolution by using this variable. The following would assign the context variable 'myLocalVar' to the value variable instead of getting the local variable:

Toggle Line Numbers
1 def myLocalVar = 100 
2 def value = varCtx.myLocalVar 

Variable: headers

You can easily debug the request headers with this variable:

Toggle Line Numbers
1  
2 headers.each { header -> 
3     println "Header: $header.key" 
4     header.value.each { println "Value: $it" } 
5 } 
6                  

Variable: cookies

You can easily check for the existence of a cookie with this variable:

Toggle Line Numbers
1  
2 if (!cookies.myCompanyCookie) { 
3     response.addCookie("myCompanyCookie", "someValue") 
4 } 
5                  

Variable: useragent

You can check/anaylize the User-Agent (browser) by simply referencing this variable:

Toggle Line Numbers
1  
2 if (useragent =~ /MSIE/) { 
3     ... 
4 } 
5                  

Variable: *Context

You can force access to set/get a variable in a particular scope. The available scopes depend on the framework you are using (etc, Seam, Spring). Gracelets comes with a set of default scopes (view, request, conversation (default), session, application) that you can use. So for instance if you wanted to hold state in a variable in the current view context, you could make sure it is stored in that scope by doing the following:

Toggle Line Numbers
1 viewContext.someVar = 'someValue'