如何:freemarker 中的 jsp scriptlet 等效代码

发布于 2024-11-01 01:17:21 字数 251 浏览 6 评论 0原文

我正在开发一个 POC,需要创建一个相当于 JSP 的 freemarker ftl。目的是了解创建 jsp 等效的 ftl 文件是多么容易。我在 jsp 文件中遇到了一些 scriptlet,不确定应该采取哪种方法在 ftl 中的代码中编写相同的代码。我应该创建支持 scriptlet 代码的标签吗?但是为每个 scriptlet 编写标签会太昂贵(可能我在这里完全错了)。我被困在这里了。您能告诉我在 sriptlet 中编写相同代码的最佳方法吗?

谢谢,哈努曼特。

I am working on one POC where I need to create a freemarker ftl equivalent to a JSP. The aim is to find out how easy is to create jsp equivalent ftl file. I came across some scriptlets in jsp file and not sure which approach I should take to write same in code in ftl. Shall I create tags supporting scriptlet code .. but then it will be too expensive writing tags for every scriptlet (May be I am completely wrong here ). I am stuck here. Can you tell me the best possible approach for writing the same code in sriptlet.

Thanks, Hanumant.

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

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

发布评论

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

评论(1

似梦非梦 2024-11-08 01:17:21

FreeMarker 和 JSP 之间的主要区别之一恰恰是 FreeMarker 不支持模板中的任意 Java 代码片段。就关注点分离而言,这是一件非常好的事情。

如果您绝对需要将 JSP scriptlet 转换为 FreeMarker 模板,则应首先检查 scriptlet 的实际用途。其中一些可能会被其等效的宏​​所取代;例如:

<% if (x < 3) { %>

可以很容易地替换为:

<#if (x < 3)>

你明白了。

其余的 scriptlet 可以替换为自定义 FreeMarker 宏,但您应该问自己这个问题:我在模板中做得不是太多吗?

至于编写一个可以包含Java代码的通用宏,这将是非常困难的,因为虽然JSP实际上在运行时编译成Servlet,但FreeMarker模板处理不涉及任何Java编译。这又是一件好事。

One of the major differences between FreeMarker and JSP is precisely that FreeMarker does not support arbitrary snippets of Java code in a template. As far as separation of concerns is involved, this is a very good thing.

If you absolutely need to convert JSP scriptlets into a FreeMarker template, you should first examine what the scriptlets actually do. Some of them may be replaced by their equivalent macros; for example:

<% if (x < 3) { %>

can easily be replaced by:

<#if (x < 3)>

You get the idea.

The remaining scriptlets could be replaced by custom FreeMarker macros, but you should ask yourself this question instead: am I not doing too much in my template?

As for writing a generic macro that could contain Java code, it would be very difficult because, while JSPs are actually compiled into Servlets at runtime, FreeMarker template processing doesn't involve any Java compilation. Which, once again, is a good thing.

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