如何在视图中调用Grails服务? [英] How to call a Grails service in a view?
本文介绍了如何在视图中调用Grails服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
简单的问题:
我有一个服务类(比如 helpersService
)和一个方法 def constructURI(params)
。
我怎样才能从模板视图调用这个方法。
Simple question :
I have a service class (let's say helpersService
) and a method def constructURI(params)
.
How can I call this method from a template view.
我已经尝试了下面的代码,但没有成功
I have tried the following code without success
<% def helpersService = new HelpersService() // or def helpersService
%>
<img src="${helpersService. constructURI(params)}"/>
但我得到以下结果:
But I get the following result:
No signature of method: com.HelpersService. constructURI() is applicable for argument types...
或(在我使用 def helpersService
)
Cannot invoke method constructURI() on null object
有什么想法?
Any ideas?
推荐答案
服务不打算在视图内使用。你可以创建一个TagLib,你可以通过依赖注入来获得对服务的引用。
Services are not intended to be used inside views. You could create a TagLib where you can get a reference to the service via dependency injection.
这篇关于如何在视图中调用Grails服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文