Last updated by admin 3 years ago
?? ?? ?????
Grails? JSP? GSP?? ??? ? ?? ??? ??? ??? ??? ???? ???(?? ????? ???? ? ?????), Grails? ??? ?? ?? ????? ????? ???? ???? ?? ?? ??? ??(simple custom tags), ?? ??? ??(logical custom tags), ?? ??? ??(iterative custom tags)? ?? ?? ????.Grails ??? ???? ??? ?? ??, TLD? ??? ??? ???, ?? ??? ?? ???? ???? ??? ??? ??? ????. ??? ?? ?? ????? ?? ?? ?? ???? ???? ? ?? ????? ???? ???? ??? ??? ? ????.Grails ?? ?????? JSP?? ????? ??? ???? ??? ?????. Grails ?? ?????? ????? ???? ?? JSP ??? GSP? ? ? ?????. Grails ?? ?????? JSP?? ???? ??? ????? ??? ??? ?????.
?? ??(Simple tags)
? ??? ???? "grails-app/taglib/ApplicationTagLib.groovy" ??? ??? ?????, ??? "TagLib"? ??? ? ???? ???? ???. ?? ??? ???? ???? ??? ??? ?? ??? ??? ??? ???? ???. ? ??? ??? ?? ?????:def includeJs = { attrs ->
out << "<script xsrc='scripts/${attrs['script']}.js' />"
}<g:includeJs script="myscript" />?? ??(Logical tags)
?? ??? ???? ? ?? ??? ?? ??? ??? ???? ???. ? ?? ??? ??? ?? ????, ? ?? ??? ??? ??(body)?? ?? ??? ? ?? ??? ???.def isAdmin = { attrs, body ->
def user = attrs['user']
if(user != null && checkUserPrivs(user)) {
body()
}
}<g:isAdmin user="${myUser}">
// ???? ? ? ?? ??? ???.
</g:isAdmin>?? ??(Iterative tags)
?? ??? ??? ? ?? ?????:def repeat = { attrs, body ->
def i = Integer.valueOf( attrs["times"] )
def current = 0
i.times {
body( ++current ) // ?? ?? ??(current)? Groovy? ?? ??? "it"?? ??
}
}<g:repeat times="3"> <p>3? ??! ?? ?? ?? = ${it}</p> </g:repeat>
??? ???? ??? ???
Grails? ??? ???? ???? ?? ? ??? ???? ??? ???? ?????. ???? ??? ?? ??? ???? ?? ?? ? ?????. ???? ???? 'mkp' ???? ????? ??? ????? ?? ????? ?? ???? ????? ???:def dialog = { attrs, body ->
mkp {
div('class':'dialog') {
body()
}
}
}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 ?? ?????? ??? JSP? "invokeTag" ??? ???? Grails? ?? ?????? ????? ???.<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" />


