Chinese Dynamic Tag Libraries

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.
null
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
null
{excerpt}

?????

??????????????"grails-app/taglib/ApplicationTagLib.groovy"????????"TagLib"?????? ??????????????????????closure)?????????????????????????

@Property includeJs = { attrs ->
    out << "<script xsrc='scripts/${attrs['script']}.js' />"
}
????GSP?????????????"g"?????????????
<g:includeJs script="myscript" />
{excerpt:hidden=true}&nbsp; 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:
code: null
@Property includeJs = { attrs -> out << "<script xsrc='scripts/${attrs'script' (+)}.js' />" }
code: null
To 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()
     }
}
????????????????????????????????:
<g:isAdmin user="${myUser}">
    // ????????????
</g:isAdmin>
{excerpt:hidden=true}&nbsp; 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:
code: null
@Property isAdmin = { attrs, body -> def user = attrs'user' (+) if(user != null && checkUserPrivs(user)) { body() } }
code: null
The 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"
    }
}
&nbsp; ???????????:
<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'
} %>

&nbsp;?JSP???Grails????

??JSP???Grails????????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>
?????????????????JSP???????

1) ??????java??? org.codehaus.groovy.grails.web.taglib.jsp.JspInvokeGrailsTagLibTag ?????????"setName()" ????????????

package com.mycompany.taglib;
public class IncludeJsTag extends JspInvokeGrailsTagLibTag {
   public static final String TAG_NAME = "includeJs";
   public IncludeJsTag() {
       super.setName(TAG_NAME);
   }
}
2) JSP ???TLD??????????????????????????? "web-app/WEB-INF/tld/grails.tld" ????????
<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>
3) ???????JSP?????????????????:
<g:includeJs script="myscript" />