我如何从gsp调用Grails服务? [英] How do I call a Grails service from a gsp?
问题描述
我如何直接从视图调用服务?我尝试使用 $ {my.domain.service.method}
,但它抱怨找不到该属性。
不,我不想使用控制器,因为视图是模板。 解决方案
<%@ page import =com.myproject.MyService%>
<%
def myService = grailsApplication.classLoader.loadClass('com.myproject.MyService')。newInstance()
%>
然后您可以调用 $ {myService.method()}
在您的gsp视图中
请注意,从视图调用事务性服务方法会影响性能。最好将所有交易服务方法调用移动到控制器(如果可以的话)
How can I invoke a service directly from a view? I'm trying with ${my.domain.service.method}
, but it complains it can't find the property.
And no, I don't want to use a controller because the view is a template.
<%@ page import="com.myproject.MyService" %>
<%
def myService = grailsApplication.classLoader.loadClass('com.myproject.MyService').newInstance()
%>
And then you can call ${myService.method()}
in your gsp view
Be aware that calling transactional service methods from views hurts performance. Better to move all your transactional service method calls to the controller (if you can)
这篇关于我如何从gsp调用Grails服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!