createLink expression in <g:select> tag
Grails newbie - I'm trying to create URIs for the value attributes in my select markup (iterating over each object of a domain class). I tried using the createLink tag in my tag like so:
<g:select name="project.id" from="${Project.list(sort:'start', order:'desc')}" optionValue="${createLink(controller:'project',action:'show')}/${it.id}" noSelection="['null': 'select project']/>
Obviously I get a GSP exception, which explains that "/[mycontroller]/[myac开发者_运维百科tion]/null" is not a property of the object.
Is there any other way of constructing these URI's inside a <g:select>
(e.g. <option value="/my/uri/">
without resorting to a loop and constructing the values "manually"?
It can be easily done by adding an extra method to Project domain:
class Project {
static transients = ['optionValue']
String getOptionValue() {
def g = ApplicationHolder.application.mainContext.getBean(
'org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib')
return g.createLink(controller:'project',action:'show', id: id)
}
}
and using it as:
<g:select name="project.id" from="${Project.list(sort:'start', order:'desc')}" optionValue="optionValue" noSelection="['null': 'select project']" />
精彩评论