开发者

How To: jsp scriptlet equivalent code in freemarker

开发者 https://www.devze.com 2023-02-24 22:50 出处:网络
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

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 scrip开发者_Go百科tlet 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号