如何:freemarker 中的 jsp scriptlet 等效代码
我正在开发一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
FreeMarker 和 JSP 之间的主要区别之一恰恰是 FreeMarker 不支持模板中的任意 Java 代码片段。就关注点分离而言,这是一件非常好的事情。
如果您绝对需要将 JSP scriptlet 转换为 FreeMarker 模板,则应首先检查 scriptlet 的实际用途。其中一些可能会被其等效的宏所取代;例如:
可以很容易地替换为:
你明白了。
其余的 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:
can easily be replaced by:
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.