本文介绍了我如何(或者应该?)从Grails中的SiteMesh模板(views / layouts / main.gsp)访问服务层?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我需要在我正在构建的网站的页面页脚中创建一个顶部列表。页脚是在默认的SiteMesh布局模板( views / layouts / main.gsp )中创建的。

需要创建对数据库的顶级列表访问权限,所以我已经将顶级列表创建所需的所有逻辑封装在服务类( services / FooService )中。请注意,虽然通常从控制器层访问服务,但在这种情况下,默认布局模板( views / layouts / main.gsp

/ code>)是不是从控制器生成的。


  • 布局视图( views / layouts / main.gsp )访问一个服务类?如何?

  • 这是否是正确的设计决策?如果没有,什么是更好的封装,以及如何与布局视图中的封装交互( views / layouts / main.gsp )?


解决方案

我会建议创建一个标记库()来包装页脚的渲染。您可以通过简单地声明一个具有相同名称的字段来将服务注入到标记库类中。

然后从服务中获取所需内容,并直接输出html或更好地创建一个 _footer.gsp 和渲染它渲染模板:'footer',model:[yourmodel:model]


I need to create a toplist in the page footer on a site that I'm building. The footer is created in the default SiteMesh layout template (views/layouts/main.gsp).

In order to create the toplist access to the database is needed, so I've encapsulated all logic needed for the toplist creation in a service class (services/FooService).

Please note that while services are usually accessed from the controller layer, in this case the default layout template (views/layouts/main.gsp) is not generated from a controller.

  • Can the layout view (views/layouts/main.gsp) access a service class? How?
  • Is this the correct design decision? If not, what is a better encapsulation and how do I interact with said encapsulation from the layout view (views/layouts/main.gsp)?

解决方案

I would suggest creating a tag library (documented here) to wrap the rendering of your footer. You can inject your service into the tag library class by simply declaring a field with the same name.

Then get what you need from the service and either output the html directly or better yet create a _footer.gsp and render it with render template: 'footer', model: [yourmodel: model].

这篇关于我如何(或者应该?)从Grails中的SiteMesh模板(views / layouts / main.gsp)访问服务层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 01:40