【问题标题】:Calling TagLib stuff in Grails console?在 Grails 控制台中调用 TagLib 的东西?
【发布时间】:2010-05-06 15:45:43
【问题描述】:

有没有办法从 grails 控制台内部调用 taglib 闭包?我希望能够在 grails 控制台中获取消息标签,但我无法弄清楚......

【问题讨论】:

    标签: grails grails-web-console


    【解决方案1】:

    您可以获得配置的 taglib,但大多数都希望在 Web 请求的上下文中运行。为了解决这个问题,您可以绑定一个模拟请求:

    import grails.util.GrailsWebUtil
    
    GrailsWebUtil.bindMockWebRequest ctx
    
    def g = ctx.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ValidationTagLib')
    String message = g.message(code: 'default.button.delete.confirm.message')
    

    您还可以通过设置请求的语言环境来获取其他语言的消息,例如

    import grails.util.GrailsWebUtil
    
    def webRequest = GrailsWebUtil.bindMockWebRequest(ctx)
    webRequest.currentRequest.addPreferredLocale(Locale.GERMANY)
    
    def g = ctx.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ValidationTagLib')
    String message = g.message(code: 'default.button.delete.confirm.message')
    

    【讨论】:

      【解决方案2】:

      使用@Burt console plugin 这更容易,因为我们不必模拟网络请求...

      import org.codehaus.groovy.grails.plugins.web.taglib.ValidationTagLib
      
      // Getting the class name to reduce horizontal
      // scrolling in StackOverflow
      def g = ctx.getBean(ValidationTagLib.class.getName())
      
      g.message(code: 'default.button.delete.confirm.message');
      

      您可以通过在控制台中运行此代码来获取应用程序中所有 tagLib 的列表...

      // prints a bean name per line.
      ctx.getBeanNamesForType(Object).findAll {
          it =~ /.*TagLib$/
      } .sort() {println it}
      
      // add false to prevent console printing the map out
      false 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多