从Java模板直接访问DAO

发布于 2025-01-03 04:43:55 字数 292 浏览 3 评论 0原文

是否可以通过 Java 模板直接访问 DAO?我想要实现这样的目标:

 <#foreach value="eventsDao.getEvents()" as="event">
    <= value="event.getName()" />
 </#foreach>

我觉得将所有内容写入控制器,然后写入模板是不必要的,并且限制了口是心非。

另外,JSP、Freemarker 和 Apache Tiles 之间的显着区别是什么?他们允许这样做吗?

Is it possible to somehow directly access DAO from Java templates? I would like to achieve something like

 <#foreach value="eventsDao.getEvents()" as="event">
    <= value="event.getName()" />
 </#foreach>

I feel like writing everything to controllers and then to templates is just unnecessary and limiting duplicity.

Also, what are the significant differences between JSP, Freemarker and Apache Tiles? Do they allow this?

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

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

发布评论

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

评论(3

口干舌燥 2025-01-10 04:43:55

如果你真的想做到这一点,打破模型视图分离......有一些方法。在 JSP 中,您有 <%! ... %><% ... %> 块,您可以在其中输入 Java 代码。在 FreeMarker 中,虽然无法插入 Java 块,但仍然可以调用对象和类的公共方法。调用静态方法有点棘手,因为您不能仅在语言级别上按名称访问类,而是必须将某些内容放入可以执行此操作的模板的上下文(所谓的数据模型)中(请参阅: http://freemarker.org/docs/pgui_misc_beanwrapper.html#autoid_55“访问静态方法”)。我认为,瓷砖与这个问题无关。

If you really want to do this, breaking the Model-View separation... there are ways. In JSP, you have the <%! ... %> and <% ... %> blocks in which you can enter Java code. In FreeMarker, while you can't insert Java blocks, you can still call the public methods of objects and classes. Calling the static methods is a bit tricky, because you can't just access classes by name on the language level, but has to put something into the context (the so called data-model) of the template that can do that (see: http://freemarker.org/docs/pgui_misc_beanwrapper.html#autoid_55 "Accessing static methods"). Tiles is, I believe, irrelevant in this question.

梦年海沫深 2025-01-10 04:43:55

关于 JSP、FreeMarker、Tiles 问题:Tiles 是一个视图合成框架,除了可以用作模板之外,与 JSP 和 FreeMarker 无关。

希望 JSP 和 FreeMarker 之间的差异显而易见,但简而言之,FM 对于循环等典型视图构造具有更简洁的语法,并且允许更直接地访问函数,而无需以 JSP 方式定义和公开函数。

关于 MVC 的“浪费”:当然,您可以在视图层中完成所有操作——没有什么可以阻止您编写架构不佳的系统,对于小型应用程序来说,这可能并不重要。

Regarding the JSP, FreeMarker, Tiles question: Tiles is a view compositing framework and unrelated to JSP and FreeMarker aside from being able to use either as templates.

Hopefully it's obvious what the differences between JSP and FreeMarker are, but in a nutshell, FM has a more-concise syntax for typical view constructs like looping, and allows more-direct access to functions without having to define and expose functions the JSP way.

Regarding the "waste" of MVC: sure, you can do everything in the view layer--there's nothing preventing you from writing poorly-architected systems, and for small apps, it likely doesn't matter.

闻呓 2025-01-10 04:43:55

你可以使用 Pebble 来做到这一点,它也有一个非常干净的语法;除此之外

{% for event in eventsDao.events %}
  {{ event.name }}
{% endfor %}

,我还推荐 Pebble 而不是 Velocity 或 Freemarker,主要是因为 Pebble 的最佳功能是 模板继承

免责声明:我是 Pebble 的主要作者

You can do this using Pebble and it has a pretty clean syntax as well; it would look like the following:

{% for event in eventsDao.events %}
  {{ event.name }}
{% endfor %}

Beyond that, I would also recommend Pebble over Velocity or Freemarker primarily because of Pebble's best feature which is template inheritance.

Disclaimer: I'm the main author of Pebble

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