?????? ?? ????? {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}
null
Got 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' />"
}{excerpt:hidden=true}
To call your tag your from a GSP page use the "g" prefix followed by the tag property name: {excerpt}
GSP???????????????????????"g"????????????????:
<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()
}
}{excerpt:hidden=true}
The tag above checks if the user is an administrator and invokes the body of the tag if he/she is: {excerpt}
??????????????????????????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"
}
}{excerpt:hidden=true}
To call your iterative tag do the following: {excerpt}
???????????????????????:
<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>{excerpt:hidden=true}
Or as a method call, like this: {excerpt}
??????????????????????????:
<span id="title" class="label ${hasErrors(bean:book,field:'title','errors')}">Title</span>{excerpt:hidden=true}
The last argument of the method is taken as the body of the tag. Or you can pass a closure that returns a string: {excerpt}
hasErrors????????????hasErrors???body????????????string??????????????????:
<%=
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>{excerpt:hidden=true}
If you want your tags to appear like normal JSP tags, here's what you need to do: {excerpt}
Grails?????????????JSP?????????????????????????????: {excerpt:hidden=true}
1) Create a new java class that sub classes the Grails org.codehaus.groovy.grails.web.taglib.jsp.JspInvokeGrailsTagLibTag class and calls the "setName()" method in the constructore of your new class passing the name of your tag: {excerpt}
1) 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);
}
}{excerpt:hidden=true}
2) JSP requires declarative tag library definition files (TLD) for each tag, to do this modify the "web-app/WEB-INF/tld/grails.tld" file and add the necessary entries that point to your class: {excerpt}
2) JSP??????????????????????????(TLD)???????????????????"web-app/WEB-INF/tld/grails.tld"?????????1)?????????????????????????:
<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>{excerpt:hidden=true}
3) You can then call your tag from JSP like a normal JSP tag: {excerpt}
3) ???????JSP???????JSP????????????????????????:
<g:includeJs script="myscript" />