java JSP 与 FreeMarker

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5559727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:40:07  来源:igfitidea点击:

JSP vs FreeMarker

javajspservletsfreemarker

提问by rl99

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?

JSP 是否提供了比 FreeMarker 更大的灵活性,还是相反?我的后端涉及 Servlet。在什么情况下使用哪一个?

采纳答案by WhiteFang34

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.

JSP 会让你在视图层做更多的事情,所以你可能会说这让它更灵活。不过,您可能不应该做的事情,人们可能会认为这对 FreeMarker 有利。它可以让您更好地控制视图层可以做什么,并强制更好地分离关注点。

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 MVCpattern. It can be accomplished with either.

这里真的没有任何正确的答案。您可以在技术上为您的 UI 完成几乎任何事情。如果没有关于您的项目的更多详细信息,就很难提出建议。我唯一能说的是,您可能应该使用MVC模式。它可以通过任何一个来完成。

回答by mdinnyes

My answer would be definitely to go with Freemarker. Reason is flexibility in a term of usage.

我的答案肯定是使用 Freemarker。原因是使用术语的灵活性。

  1. It does not require servlet container, so you can also render e-mails or anything text based.

  2. 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.

  3. 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.

  4. From Freemarker you can still use every JSP tag library available without problem :)

  1. 它不需要 servlet 容器,因此您还可以呈现电子邮件或任何基于文本的内容。

  2. 在 Web 应用程序中,可以将 Web 模块创建为 jar。Freemarker 模板可以很容易地从类路径加载(此外,spring 可以从带有 mvc:resources 标签的类路径加载 css/js - 静态内容 - 并扫描控制器)。所以一个 web 模块可以包含它自己的模板、静态数据和控制器。

  3. Freemarker 在创建自定义标签方面更强大,jsp 让它变得很痛苦。此外,自定义标签允许回调参数化的正文,而这在 JSP 中实际上无法轻松实现。

  4. 从 Freemarker,您仍然可以毫无问题地使用每个可用的 JSP 标记库:)

回答by Stef

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.

我相信您现在已经选择了一种或另一种选择。无论如何,评论可能对其他人仍然有用。正如 WhiteFang 所写,使用 MVC 是唯一强烈推荐的建议。

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.

如果您的应用程序是基于 Web 的应用程序,那么 JSP 很可能是更好的选择,因为它更广为人知,并且可以使用 JSTL 编写非常干净的表示代码(只需确保不要混入 scriptlet ......所有数据都应该是在 servlet 中准备并作为属性传递给 JSP)。选择 JSP,您可以通过使管理人员在需要更快交付等时轻松插入其他开发人员,从而为项目带来价值。

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 :) ).

不管怎样,既然你已经在 FreeMarker 上有了它,如果模板看起来已经不错了,我不会改变它。而是等待您有商业理由切换。无论应用程序设计得如何,最终都需要部分甚至完全重新构建。只要您进行模块化编程,并将您的转换保持在一个独立的模块中,您就可以随时轻松地切换到您认为合适的任何技术(可能是一种新的技术,比 FreeMarker 或 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.

我通常将 FreeMarker 用于非 Web 应用程序。甚至在 Web 应用程序中,例如当我需要生成通知电子邮件时,不需要启用 Web。否则 JSP 到目前为止效果很好。

Stef.

斯蒂芬。

回答by rahul

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 的库,如 JSTL、DisplayTag 等,如果您选择使用 JSP,则可以使用它们。有了 Freemarker,它们就没有那么多了。

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.

JSP 是为网页量身定做的,FTL 是一种更通用的模板语言——它可以用来生成 html、纯文本、电子邮件等。

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.

要选择一个,您必须考虑您拥有什么样的工具。所有优秀的 Java IDE 都具有出色的 JSP 支持。FTL 支持可能/可能没有那么好。