如何从 gsp 调用 Grails 服务?

发布于 2024-08-26 09:30:29 字数 109 浏览 9 评论 0原文

如何直接从视图调用服务?我正在尝试使用 ${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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

睫毛上残留的泪 2024-09-02 09:30:29

最好使用标签库,因为通过类加载器直接在视图中创建服务实例不会自动装配声明的其他服务,这些服务可能存在于您尝试使用的服务中。

使用标签库,您将自动连接这些服务。

在您的 gsp 视图中

在您的 taglib 文件夹中 (yourApp/grails-app/taglib/com/something/ MyAppTagLib):

package com.something

class MyAppTagLib {

    def myService  // This will be auto-wired

    def customTag = { attribs ->
        def modelObj = attribs['param1']
        def someString = attribs['param2']

        // Do something with the params

        myService.method()

        out << "I just used method of MyService class"
    }
}

您的 MyService:

package com.something

class MyService {

def anotherService // This will be auto-wired

def method() {
    anotherService.anotherMethod()
}

}

Best to use the tag library because creating a service instance directly in the view via the class loader WILL NOT autowire other services declared that may live in the service you are trying to use.

Using the tag library you will have auto-wiring of those services.

In your gsp view <g:customTag param1="$modelObjec" param2="someString" />

In your taglib folder (yourApp/grails-app/taglib/com/something/MyAppTagLib):

package com.something

class MyAppTagLib {

    def myService  // This will be auto-wired

    def customTag = { attribs ->
        def modelObj = attribs['param1']
        def someString = attribs['param2']

        // Do something with the params

        myService.method()

        out << "I just used method of MyService class"
    }
}

Your MyService:

package com.something

class MyService {

def anotherService // This will be auto-wired

def method() {
    anotherService.anotherMethod()
}

}
盗梦空间 2024-09-02 09:30:29

试试这个 - 很有帮助

%{--Use BlogService--}%
<g:set var="blog" bean="blogService"/>

<ul>
    <g:each in="${blog.allTitles()}" var="title">
        <li>${title}</li>
    </g:each>
</ul>

参考这个

另外,这不是推荐的事情,您可以随时使用 taglib

Try this - much helpful

%{--Use BlogService--}%
<g:set var="blog" bean="blogService"/>

<ul>
    <g:each in="${blog.allTitles()}" var="title">
        <li>${title}</li>
    </g:each>
</ul>

Refer this

Also this is not a recommened thing, you can always use taglib

‘画卷フ 2024-09-02 09:30:29

我认为最好的方法是:通过

<%
    def myService = grailsApplication.mainContext.getBean("myService");
%>

这种方式,您可以获得服务实例,而不会丢失自动装配的服务。

I think the best way of doing it is:

<%
    def myService = grailsApplication.mainContext.getBean("myService");
%>

This way, you get the service instance without losing the autowired services.

黑寡妇 2024-09-02 09:30:29
<%@ page import="com.myproject.MyService" %>
<%
    def myService = grailsApplication.classLoader.loadClass('com.myproject.MyService').newInstance()
%>

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

请注意,从视图中调用事务服务方法会损害性能。最好将所有事务服务方法调用移至控制器(如果可以的话)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文