开发者

How can I restrict custom taglib tag apperence to one per page?

开发者 https://www.devze.com 2023-03-06 18:37 出处:网络
For example when i want <custo开发者_如何转开发m:footer/> to be impossible to appear multiple times on one page.Every time the tag is called, set an attribute in the page context. If the attribu

For example when i want <custo开发者_如何转开发m:footer/> to be impossible to appear multiple times on one page.


Every time the tag is called, set an attribute in the page context. If the attribute is already present, that means the tag has already been called for the current page, so throw an exception. Something like this (untested)

class MyTagLib {

   def doIt = { attrs, body ->

       if (pageScope.invoked) {
           // throwTagError is a built-in method that is available in all tag libs
           thowTagError "this tag can only be invoked once per page"
       }

       pageScope.invoked = true

      // Now cure world poverty or whatever it is that your tag is supposed to do
   }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消