[英]How to get a service bean inside a custom tag library class in Grails 3
我想從我的自定義標簽庫中的應用程序上下文中獲取服務bean。 服務名稱我將從自定義標簽屬性中獲取。
這是我以前使用的代碼。
class CustomTagLib {
static defaultEncodeAs = [taglib:'html']
//static encodeAsForTags = [tagName: [taglib:'html'], otherTagName: [taglib:'none']]
def selectList = { attrs ->
try{
String servName=attrs.service
String servMethod=attrs.method
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(session.getServletContext())
def myservice=ctx."${servName}"
attrs.from = myservice.invokeMethod(servMethod,null);
out << g.select( attrs )
}catch(Exception e){
println("Exception in CustomTagLib in method selectList:"+e)
}
}
}
該代碼適用於Grails 2.3版本,但不適用於版本3。請幫助我找出解決方案。
請嘗試以下操作:
import grails.util.Holders
def myservice = Holders.getApplicationContext().getBean( servName )
其中servName將是您的服務名稱,其余部分用小寫的首字母和駝峰式大寫
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.