JSP 与 FreeMarker
JSP 是否提供了比 FreeMarker 更多的灵活性,还是其他方式? 我的后端涉及Servlet。 什么情况下使用哪一种?
Does JSP provide more flexibility then FreeMarker or is it other way around?
My back-end involves Servlets.
Which one is used under what circumstances?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
JSP 将让您在视图层做更多的事情,所以您可能会说这使得它更加灵活。但你可能不应该做的事情,有人可能会认为这对 FreeMarker 是一种好处。它将使您能够更好地控制视图层可以执行的操作,并强制更好地分离关注点。
这里实际上没有正确的答案。从技术上讲,您可以使用任何一个 UI 完成几乎任何事情。如果没有有关您的项目的更多详细信息,就很难提出建议。我唯一能说的是,你应该使用 MVC 模式。两者都可以实现。
JSP is going to let you do more things in the view layer, so you might say that makes it more flexible. Things you probably shouldn't be doing though, which one could argue as a benefit for FreeMarker. It would afford you more control over what the view layer can do and force a better separation of concerns.
There's not really any right answer here. You can technically accomplish just about anything with either for your UI. Without more details about your project it's hard to make a recommendation. About the only thing I can say is that you should probably use the MVC pattern. It can be accomplished with either.
我的答案肯定是使用 Freemarker。
原因是使用方面的灵活性。
它不需要 servlet 容器,因此您还可以呈现电子邮件或任何基于文本的内容。
在 Web 应用程序中,可以将 Web 模块创建为 jar。 Freemarker 模板可以轻松地从类路径加载(此外,spring 可以使用 mvc:resources 标签从类路径加载 css/js - 静态内容并扫描控制器)。因此,Web 模块可以包含自己的模板、静态数据和控制器。
Freemarker 在创建自定义标签方面更强大,而 jsp 则让它变得很痛苦。此外,自定义标签允许回调参数化的主体,而这在 JSP 中实际上无法轻松完成。
从 Freemarker 中,您仍然可以毫无问题地使用每个可用的 JSP 标记库:)
My answer would be definitely to go with Freemarker.
Reason is flexibility in a term of usage.
It does not require servlet container, so you can also render e-mails or anything text based.
In a web application it makes possible to create web-modules as jar. Freemarker templates can easily be loaded from classpath (also, spring makes possible to load css/js - static content - from classpath with mvc:resources tag and scan for controllers). So a web module can contain its own templates, static data and controllers.
Freemarker is more powerful in terms of creating custom tags, jsp makes it just a pain. Also, custom tags allow calling back to body parameterized which you will not really be able to do in JSP very easily.
From Freemarker you can still use every JSP tag library available without problem :)
有几个围绕 JSP 的库,如 JSTL、DisplayTag 等,如果您选择使用 JSP,则可以使用它们。有了 Freemarker,这样的问题就没有那么多了。
JSP是为网页量身定做的,FTL是一种更通用的模板语言——它可以用来生成html、纯文本、电子邮件等。
要选择一个,你必须考虑你拥有什么样的工具。所有优秀的 Java IDE 都具有出色的 JSP 支持。 FTL 支持可能/可能不那么好。
There are several libraries that have been around JSP like JSTL, DisplayTag, etc. that you can use if you choose to go with JSP. With Freemarker, there are not as many of them.
JSP is tailor made for Web pages, FTL is a more generic templating language - it can be used to generate html, plain text, emails, etc.
To pick one, you must consider what kind of tools you have. All good Java IDEs have excellent JSP support. FTL support may/may not be as good.
我相信您现在已经选择了其中一种或另一种选择。无论如何,评论可能对其他人仍然有用。
正如 WhiteFang 所写,使用 MVC 是唯一强烈推荐的建议。
如果您的应用程序是基于 Web 的应用程序,JSP 很可能是一个更好的选择,因为它更广为人知,并且可以使用 JSTL 编写非常干净的演示代码(只需确保不要混合使用 scriptlet ...所有数据都应该是在 servlet 中准备并作为属性传递给 JSP)。选择 JSP,您可以为项目带来价值,因为管理人员可以在需要时轻松插入其他开发人员以加快交付速度等。
无论如何,既然您已经在 FreeMarker 上使用了它,如果模板看起来已经不错,我就不会更改它。而是等待当您有业务原因时才进行切换。无论应用程序设计得多么好,它最终都需要部分甚至完全重新架构。只要您进行模块化编程,并将转换保持在独立的模块中,您就可以随时轻松切换到您认为合适的任何技术(可能是一种新技术,比 FreeMarker 或 JSP 更好:))。
我通常将 FreeMarker 用于非 Web 应用程序。或者甚至在网络应用程序中,例如,当我需要生成通知电子邮件时,不需要启用网络。除此之外,JSP 到目前为止运行良好。
斯特夫。
I believe that you already went for one or the other option by now. Anyway, comment might still be useful for others.
As WhiteFang wrote, using MVC is the only highly recommended suggestion.
If your application is a web based one, JSP would most likely be a better option as it is better known and one can write quite clean presentation code using JSTL (just make sure you don't mix in scriptlets ... all data should be prepared in servlets and passed on to JSPs as attributes). Choosing JSP you bring value to the project by enabling managers to plug-in easily other developers when needed to deliver faster etc.
Anyway, since you already have it on FreeMarker, if the template looks ok already, I wouldn't change it. Rather wait for when you have a business reason to switch. Whatever well designed an application might be, it will eventually need to be partially or even fully rearchitected. As long as you do modular programming, and keep your transformation in an isolated module, you will be able at any time to easily switch to whatever technology you'll see fit (potentially a new one, better than FreeMarker or JSP :) ).
I am usually using FreeMarker for non web applications. Or even in web applications, when I need to generate notification emails for example, which don't need to be web enabled. Otherwise JSP works great so far.
Stef.