Last updated by admin 4 years ago
?????? ?? ????? {excerpt:hidden=true}Dynamic Tag Libraries {excerpt}
{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. {excerpt} Grails??JSP?GSP??????????????????????????????? (????????????????)????????Grails?????????????????????????????????????????????????????????????? {excerpt:hidden=true}nullGot a tag that you would like to share with the rest of the Grails developers? Contribute a tag!
null{excerpt}
??Grails???????????????????????????! ??????Contribute a tag ???????????{excerpt:hidden=true} 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. {excerpt} Grails????????????????TLD??????????????????????????????????????????????????????????????????????????????????????????????10??????
{excerpt:hidden=true} Grails 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 {excerpt} Grails?????????JSP??????????????????????????GSP????????????????????????????
????? {excerpt:hidden=true}Simple tags {excerpt}
{excerpt:hidden=true} To create new tags open the "grails-app/taglib/ApplicationTagLib.groovy" file or create a new class ending in "TagLib". To create a simple tag add a new closure property that takes 1 argument which are the attributes of the tag: {excerpt} ??????????????"grails-app/taglib/ApplicationTagLib.groovy"??????????? ??"TagLib"?????????????????????????????????????????????????????????????? ??:def includeJs = { attrs ->
out << "<script src='scripts/${attrs['script']}.js' />"
}<g:includeJs script="myscript" />????? {excerpt:hidden=true}Logical tags {excerpt}
{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: {excerpt} ???????????????????body????2???????????????????????????????:def isAdmin = { attrs, body ->
def user = attrs['user']
if(user != null && checkUserPrivs(user)) {
body()
}
}<g:isAdmin user="${myUser}">
// some restricted content
</g:isAdmin>???????? {excerpt:hidden=true}Iterative tags {excerpt}
{excerpt:hidden=true} And of course you can create iterative tags: {excerpt} ???????????????????????:def 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>
????????????? {excerpt:hidden=true}Markup building in tags {excerpt}
{excerpt:hidden=true} 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: {excerpt} Grails?????????????????????????????????????????????????????????????'mkp'???????????:def dialog = { attrs, body ->
mkp {
div('class':'dialog') {
body()
}
}
}GSP???????????????? {excerpt:hidden=true}Tags as method calls in GSP {excerpt}
{excerpt:hidden=true} GSP tags can also be used in Groovy expressions in the GSP page. For example, the hasErrors tag can be used normally as a tag like this: {excerpt} GSP??????GSP??????Groovy????????????????hasErros????????????????????????:<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???????????? {excerpt:hidden=true}Using Grails tag libs from JSP {excerpt}
{excerpt:hidden=true} To use a Grails taglib definition in JSP you can use the JSP "invokeTag" tag which will call a tag defined in the Grail tag library: {excerpt} JSP??Grails???????????????"invokeTag"???????:<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>
package com.mycompany.taglib; public class IncludeJsTag extends JspInvokeGrailsTagLibTag { public static final String TAG_NAME = "includeJs"; public IncludeJsTag() { super.setName(TAG_NAME); } }
<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" />


