Java JSP 与 Velocity 哪个更好?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/610062/
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-08-11 16:50:16  来源:igfitidea点击:

JSP vs Velocity what is better?

javajspvelocity

提问by flybywire

What is better between JSP and velocity in - Performance - Ease of use - Ease of creating reusable components - Availability of open source 3rd parties - IDE support

JSP 和速度之间哪个更好 - 性能 - 易用性 - 易于创建可重用组件 - 开源 3rd 方的可用性 - IDE 支持

采纳答案by vartec

Advantages of Velocity:

速度优势:

  • strict separation of view from business logic
  • simple syntax that can be understood by graphic designers
  • 视图与业务逻辑的严格分离
  • 图形设计师可以理解的简单语法

回答by cherouvim

Velocity or even better FreeMarker. In JSP you cannot have runtime dispatch for pojo hierarchiesand everything is statically typed which is a pain. Moreover if you create many JSP2.0 custom tags (say more than 100-150) then your development-deployment cycle will slow down heavily due to inefficiencies of Jasper to resolve dependencies efficiently.

速度甚至更好的FreeMarker。在 JSP 中,您不能对 pojo 层次结构进行运行时分派,并且所有内容都是静态类型的,这很痛苦。此外,如果您创建了许多 JSP2.0 自定义标记(比如超过 100-150 个),那么您的开发部署周期将由于 Jasper 效率低下而无法有效地解决依赖关系而严重减慢。

On the other hand JSP has great tool support.

另一方面,JSP 有很好的工具支持。

slow JSP compilation references:

慢JSP编译参考:

http://www.mailinglistarchive.com/[email protected]/msg10786.html

http://www.mailinglistarchive.com/[email protected]/msg10786.html

http://marc.info/?l=tomcat-dev&m=119377083422720&w=2

http://marc.info/?l=tomcat-dev&m=119377083422720&w=2

回答by Jeroen van Bergen

I'll focus on using a template engine, because that is what I have most experience with.

我将专注于使用模板引擎,因为这是我最有经验的。

It depends on what you really want to do. Servlets in combination with Velocity (or FreeMarker for that matter) offer a very good seperation of logic and presentation. Templates are harder to test, because you would need to evaluate the template to be able to judge wheter the HTML (or whatever else the output format is) is correct. For JSP this can be done in your IDE of choice.

这取决于你真正想要做什么。Servlet 与 Velocity(或 FreeMarker)结合提供了很好的逻辑和表示分离。模板更难测试,因为您需要评估模板才能判断 HTML(或其他任何输出格式)是否正确。对于 JSP,这可以在您选择的 IDE 中完成。

The big advantage of templates is that you can store these completely outside of your application and even update them while your application is running. This is something that is a little harder to do with JSP, although hot deployment comes pretty close.

模板的一大优势是您可以将它们完全存储在应用程序之外,甚至可以在应用程序运行时更新它们。尽管热部署非常接近,但对于 JSP 来说,这有点困难。

Reusable components can be created by using the include functionality of the template engine.

可以使用模板引擎的包含功能创建可重用组件。

回答by Patrick Cornelissen

@Vartec: I don't think that the "strict separation of view from business logic" is a velocity feature that is not present in jsp. You can do business logic in jsp (more or less) but it's not recommended at all. But I agree in your point regarding the syntax.

@Vartec:我不认为“视图与业务逻辑的严格分离”是jsp 中不存在的速度特性。您可以在jsp(或多或少)中执行业务逻辑,但根本不推荐这样做。但我同意你关于语法的观点。

Performance

表现

JSP is compiled to Java, so I don't think that velocity is faster. (have not done benchmarks myself)

JSP被编译成Java,所以我不认为速度更快。(自己没有做过基准测试)

Ease of use

便于使用

For designers: velocity For programmers: (IMHO) jsp, because it's closer to code

对于设计师:velocity 对于程序员:(恕我直言)jsp,因为它更接近代码

Ease of creating reusable components

易于创建可重用组件

JSP has lots of components Velocity has no components itself (not component oriented)

JSP 有很多组件 Velocity 本身没有组件(不是面向组件的)

Availability of open source 3rd parties

开源 3rd 方的可用性

I have seen far more projects using JSP or JSP related technologies than velocity. Maybe because velocity is really low level... :-)

我看到的使用 JSP 或 JSP 相关技术的项目远远多于速度。也许是因为速度真的很低...... :-)

IDE support

IDE支持

There are plenty of tools for jsp. Especially the eclipse jboss plugin/tool suite has a good jsp editor.

jsp有很多工具。尤其是eclipse jboss插件/工具套件有一个很好的jsp编辑器。

Plugins for Velocity are mostly not functional or pretty basic (you get lucky if you have syntax highlighting)

Velocity 的插件大多没有功能或非常基本(如果你有语法高亮,你会很幸运)

UpdateIf you are looking for a templating engine now, I'd suggest to have a look at thymeleaf. It's comparably lightweight to velocity and can be used just to template some text based templates with a few lines of code or used as a full featured templating engine for example within a webapp.

更新如果您现在正在寻找模板引擎,我建议您查看 thymeleaf。它相对于velocity 来说是轻量级的,可以使用几行代码来为一些基于文本的模板进行模板化,或者用作全功能模板引擎,例如在webapp 中。

回答by Rajesh Chowdary

I don't know whether Velocity can be able to compete with JSP in all aspects but Velocity is faster and nuch easier.Efficiency of Velocity is 35 to 45% more if it is complicated webpages it may be reduced but still it is 5% more than JSP.

不知道Velocity能不能在各方面都和JSP比,不过Velocity更快更简单。如果是复杂的网页,Velocity的效率提高35%到45%,可能会降低,但仍然提高5%比JSP。

回答by atrain

The below is about Freemarker, but the comparisons are probably still relevant.

以下是关于 Freemarker 的,但这些比较可能仍然相关。

At this point in these two technologies' development, it seems like the primary reasons to choose one over the other are:

在这两种技术发展的这一点上,选择一种而不是另一种的主要原因似乎是:

  1. There is something specific that you need that is in one and not the other
  2. You want to prevent view developers from putting Java scriptlets into JSP pages
  3. Your developers are more comfortable in one than the other
  1. 您需要某种特定的东西,而不是另一个
  2. 您想阻止视图开发人员将 Java scriptlet 放入 JSP 页面
  3. 您的开发人员在其中一个比另一个更舒服

Reasons that don't seem to have as much of an impact:

似乎没有太大影响的原因:

  1. Speed. There are so many layers in a typical Java EE app that have a much greater impact than the couple of milliseconds more or less that a view renderer might take. In fact, this is probably the last layer I would tackle if my app was performing subpar.
  2. IDE support. JBoss Tools provides a Freemarker editor, and JSP tools are well known.
  3. Syntax. JSP 2 and Freemarker have virtually identical syntax for many basic operations, because of EL and JSTL.
  1. 速度。典型的 Java EE 应用程序中有很多层,它们的影响比视图渲染器可能需要的几毫秒或多或少要大得多。事实上,如果我的应用程序表现不佳,这可能是我要处理的最后一层。
  2. IDE 支持。JBoss Tools 提供了一个 Freemarker 编辑器,JSP 工具是众所周知的。
  3. 句法。由于 EL 和 JSTL,JSP 2 和 Freemarker 对于许多基本操作具有几乎相同的语法。

Freemarker Example:

Freemarker 示例:

<#list foos as foo>
  <tr>
     <td>${foo.field1}</td>
     <td>${foo.field2}</td>
     <td>
        <#list foo.childObjects as child>
           <#if child.name == 'bar'>
              ${child.value}
           </#if>
        </#list>
     </td>
  </tr>
</#list>

JSP-EL-JSTL Example:

JSP-EL-JSTL 示例:

<c:forEach items="${foos}" var="foo">
  <tr>
     <td>${foo.field1}</td>
     <td>${foo.field2}</td>
     <td>
        <c:forEach items="${foo.childObjects}" var="child">
           <c:if test="${child.name == 'bar'}">
              ${child.value}
           </c:if>
        </c:if>
     </td>
  </tr>
</c:forEach>

回答by s.k

The advantages of Velocity as per above miss a couple of very important things from the engineers perspective:

从工程师的角度来看,上述 Velocity 的优势忽略了一些非常重要的事情:

  • strict separation of view from business logic (as above)
  • simple syntax that can be understood by graphic designers (as above)
  • compact code that is less of a nightmare to revisit later, see example link
  • non-servlet container deployment means easy deploy anywhere
  • 视图与业务逻辑的严格分离(如上)
  • 图形设计师可以理解的简单语法(如上)
  • 紧凑的代码,以后再访问就不再是噩梦了,请参阅 示例链接
  • 非 servlet 容器部署意味着在任何地方轻松部署

Those last two really make Velocity useful compared to JSP.

与 JSP 相比,最后两个确实使 Velocity 有用。

回答by amit prasad

Velocity is better It adapts to many application areas It offers a simple, clear syntax for the template designer It offers a simple programming model for the developer Because templates and code are separate, you can develop and maintain them independently The Velocity engine easily integrates into any Java application environment, especially servlets Velocity enables templates to access any public method of data objects in the context

Velocity 更好 适应许多应用领域 为模板设计者提供简单、清晰的语法 为开发者提供简单的编程模型 因为模板和代码是分开的,您可以独立开发和维护 Velocity 引擎可以轻松集成到任何Java 应用程序环境,尤其是 servlets Velocity 使模板能够访问上下文中数据对象的任何公共方法