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 databas开发者_Python百科e 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]
.
You should access your service from your controllers instead of the views.
精彩评论