如何从 gsp 调用 Grails 服务? [英] How do I call a Grails service from a gsp?

查看:15
本文介绍了如何从 gsp 调用 Grails 服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何直接从视图调用服务?我正在尝试使用 ${my.domain.service.method},但它抱怨找不到该属性.

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()
%>

然后你可以在你的 gsp 视图中调用 ${myService.method()}

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆