2010-05-06 30 views

Trả lời

11

Bạn có thể nhận thẻ taglib được định cấu hình, nhưng mong đợi nhất sẽ chạy trong ngữ cảnh yêu cầu web. Để giải quyết vấn đề này, bạn có thể liên kết một yêu cầu giả mạo:

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') 

Bạn cũng có thể nhận tin nhắn cho các ngôn ngữ khác bằng cách đặt ngôn ngữ yêu cầu, ví dụ:

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') 
3

Sử dụng @Burt console plugin này thậm chí còn dễ dàng hơn khi chúng ta không cần phải thử các yêu cầu web ...

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'); 

Bạn có thể có được một danh sách của tất cả các taglibs trong ứng dụng của bạn bằng cách chạy mã này trong giao diện điều khiển ...

// 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 
Các vấn đề liên quan