Last updated by admin 4 years ago
?????
Grails?JSP?GSP???????????????? (+))???grails??????????????????????????Grails?????????????????????????TLD?????????????????????????????????????????????????????10?????Grails ????????????????JSP???????????????????GSP???????JSP???????????{excerpt:hidden=true} Grails has a wide range of of custom tags built in for both JSP and GSP (see the Tag Library Referencehere), however Grails also allows the creation of simple, logical, and iterative custom tags through its simple dynamic tag library mechanism. The benefit of grails' tags is that they require no additional configuration, no updating of TLD descriptors, and can be auto-reloaded at runtime without a server restart. This makes developing tags feel as if you were just developing another part of the view and increases their usefulness tenfold.
nullGrails Tag libraries require a little extra work to integrate into JSP and are more seamlessly integrated into GSP because of its dynamic nature. See the last section for how to use grails tags from JSP
null{excerpt}
?????
??????????????"grails-app/taglib/ApplicationTagLib.groovy"????????"TagLib"?????? ??????????????????????closure)?????????????????????????@Property includeJs = { attrs ->
out << "<script xsrc='scripts/${attrs['script']}.js' />"
}<g:includeJs script="myscript" />code: null@Property includeJs = { attrs -> out << "<script xsrc='scripts/${attrs'script' (+)}.js' />" }code: nullTo call your tag your from a GSP page use the "g" prefix followed by the tag property name:code: null<g:includeJs script="myscript" />code: null{excerpt}????
?????????????????????????????????????????????????????????????????????????????:@Property isAdmin = { attrs, body -> def user = attrs['user'] if(user != null && checkUserPrivs(user)) { body() } }{excerpt:hidden=true} You can also create logical tags by using a closure syntax that takes 2 arguments, the attributes of the tag and the body of the tag as a closure:<g:isAdmin user="${myUser}"> // ???????????? </g:isAdmin>code: null@Property isAdmin = { attrs, body -> def user = attrs'user' (+) if(user != null && checkUserPrivs(user)) { body() } }code: nullThe tag above checks if the user is an administrator and invokes the body of the tag if he/she is:code: null<g:isAdmin user="${myUser}"> // some restricted content </g:isAdmin>code: null{excerpt}????
???????????????????: ???????????:@Property repeat = { attrs, body -> def i = Integer.valueOf( attrs["times"] ) def current = 0 i.times { body( ++current ) // pass the current iteration as the groovy default arg "it" } }<g:repeat times="3"> <p>Repeat this 3 times! Current repeat = ${it}</p> </g:repeat>Markup building in tags
Grails provides a special method that allows building of markup (a common usecase in tags). To do so you invoke the 'mkp' method passing a closure with the markup you want rendered:@Property dialog = { attrs, body -> mkp { div('class':'dialog') { body() } } }Tags as method calls in GSP
GSP ??????GSP?????Groovy??????,hasErrors?????????????????????????????<g:hasErrors bean="${book}" field="title"> <span class='label error'>There were errors on the book title</span> </g:hasErrors>??????????????????????????????????????<span id="title" class="label ${hasErrors(bean:book,field:'title','errors')}">Title</span><%= hasErrors(bean:book,field:'title') { 'errors' } %> ?JSP???Grails????
??JSP???Grails????????Grails???????"invokeTag"????????????????????JSP???????1) ??????java??? org.codehaus.groovy.grails.web.taglib.jsp.JspInvokeGrailsTagLibTag ?????????"setName()" ????????????<g:invokeTag name="includeJs" script="myscript" /> <g:invokeTag name="isAdmin" user="${myUser}"> // some restricted content </g:invokeTag > <g:invokeTag name="repeat" times="3"> <p>Repeat this 3 times! Current repeat = <c:out value="${it}" /></p> </g:invokeTag>2) JSP ???TLD??????????????????????????? "web-app/WEB-INF/tld/grails.tld" ????????package com.mycompany.taglib; public class IncludeJsTag extends JspInvokeGrailsTagLibTag { public static final String TAG_NAME = "includeJs"; public IncludeJsTag() { super.setName(TAG_NAME); } }3) ???????JSP?????????????????:<tag> <name>includeJs</name> <tag-class>com.mycompany.taglib.IncludeJsTag</tag-class> <body-content>JSP</body-content> <variable> <name-given>it</name-given> <variable-class>java.lang.Object</variable-class> <declare>true</declare> <scope>AT_BEGIN</scope> </variable> <dynamic-attributes>true</dynamic-attributes> </tag><g:includeJs script="myscript" />



