JSP has been the De facto standard for Java and web development for years. JSP is one of the first popular dynamic web application frameworks. Since then as architecture models have evolved. PHP has taken a foothold in the simple dynamic web applications and large enterprise application with complex business logic has moved to MVC design using Java or .Net. Where does that leave JSP and how does it compare with Velocity and Freemarker?
JSP Overview
Originally, Java Server Pages were designed to be utilized within a J2EE web container that would allow for full application development. Older applications were designed to be written to directly access the JSP files. This is the MVC model 1 architecture approach. The client directly references the JSP page, the JSP page references a bean which in turn can access database information. With today's standards, however, it is typically not acceptable to utilize JSP as an independent architecture solution. As software architecture has evolved and design patterns have changed, it seems that the purposes of JSP pages have changed as well.
The ability of JSP to permit scripting, tags, and html code makes it as powerful as Java itself with the capability to be directly run as a web page. To rebuttal the strength of this point, this unrestricted ability of allowing so many different development techniques is simply dangerous. It becomes dangerous to both the application architecture and to the average developer as it is very easy to introduce inconsistency in design patterns into the JSP. The complexity of JSP can grow quickly and further development or ongoing maintenance could suffer in both time and cost.
An example of abusive JSP development can occur when mixing different JSP syntax. Using an particular tag library with with an expression or using ETL inside and outside of tags. JSP Scriplets are in violation of the MVC approach when using JSP as a view component which is the primary function JSP pages are used today.
Veloctiy and Freemarker Overview
Velocity and Freemarker are template engines that define a template language in which to parse. They take Java objects and merge the data with the template. Velocity and Freemarker are so similar I will not compare one to the other. Both are active projects. Velocity recently came out of its shell after a few slow years and had some releases in 2007. Freemarker may have a few bit more functionality than velocity but is also a bit more complex.
JSP vs Template Engines
* Compile Time - JSP need to be processed and compiled to before it is executed. JSP file is converted to a Java file. It is then compiled into a class file. Both velocity and freemarker do not need compiliation. This is especially beneficial to a developer. Everytime a developer modifies a JSP it is very slow to run it due to compilation. The Velocity engine is named Velocity for a reason.
* Learning Curve - JSP is very large with many tag different libraries. Velocity and Freemarker are simple. The engine and core functionality can be understood within a matter of hours.
* Readibility - JSP allows for tag libraries, scriplets, expression language, and other various techniques that is recognized as code. Velocity uses a short list of built in commands.
* Community Base - Many developers know JSP. It is a very common language for developers to list as a skill. Both freemarker and velocity have a large user base but neither is as large as JSP. This is because JSP was originally used as a full application framework when deployed to a compliant web container.
* Template structure - JSP allows for many different types of syntax. Scripting techniques such as declarations, scriptlets, and expressions as well as actions and tags. Velocity uses a #command for commands and $var for variables. Freemarker uses <#command> for commands and ${var} for variables. JSP 2.0 tries to appear as template engine with ${} trying to resemble template style
* Custom Tools - JSP allows for the creation of JSP taglibs. Both Freemarker and Velocity allow the utilization of JSP taglibs as well. Freemarker has a lot of extra built in functionality as well. Velocity allows the developer to create their own custom tools. These tools are created in Java and utilized as Java objects within the template.
* Integration - Most of the major libraries and development tools integrate with JSP, Velocity and Freemarker. Frameworks such as Sitemesh, Tiles, Struts, and Spring have functionality available to utilize all 3 technologies.
there are couple of things where FreeMarker might suit you better:
1. This is subjective, but the overall syntax is less awkward. I’m aware there’s lot of syntacic sugar in JSP 2.0 that’s aimed at hiding the ugliness of directly using JSTL tags for control flow etc. but remember that FreeMarker has had a rather ergonomic syntax for “foreach” and “if” for years.
2. Macros. If you want to have reusable template snippets, you can write them as macros and import them. AFAIK, in JSP 2.0 you finally have a sort of macros, but you need to define each snippet in its own file. This is nowhere as elegant as the full namespace/macro solution in FreeMarker.
3. Transforms. Transforms are the equivalent of body tags in JSP, with a significant distinction that while JSP body tags first cache all of their body and only after that act on them, FreeMarker transforms are fully output-driven, in fact they are implemented internally as chains of java.io.Writer objects. Therefore, regardless of how many transforms you have nested, you can still have progressive page rendering (“can” because a transform can choose to cache all of its body like a JSP body tag, but is not mandated to do so and can provide a clever implementation that allows progressive processing).
4. Also, the usual argument about usability outside the HTTP protocol stack. If your system does any text output beside HTTP responses – i.e. sends SMTP mail, you can use FreeMarker for generating mail messages from templates, too, or for generating arbitrary XML. It even has declarative XML processing capabilities which make it possible to use it similar to XSLT, but significantly less complex. You can simplify your project by having one language for all your text output generating needs.
5. Data model flexibility – if you like, you can implement the data model interfaces to naturally represent any underlying data model. I.e. we have a wrapper for Jython objects that pretty faithfully reflects the look and feel of Jython objects in the template syntax. People have reported providing a similar binding for JavaScript etc.
There’s also lots of goodies like automatic HTML or XML escaping of expressions, convenient literal syntax for lists and maps, etc.
This much off the top of my head – it’s a bit late here, so I can’t think of anything else at this very moment.
Velocity's Simplistic design
Of the three technologies, velocity is the simplest. It has a small amount of very powerful commands. Syntax is as follows.
Display variable name
$var
Display variable name. If null it displays blank
$!var
Directives
#foreach ($item in $collection) item is $item #end
#if ($order.total == 0) No charge #end
#parse("header.vm")
#macro
#include("disclaimer.txt")
#set ($customer = ${order.customer})
Code Comparisons
Here is some code that is written in JSP. This is the logic that would be used when processing a number named "number" is passed in as a parameter.
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<!-- Is the number guess right? -->
<logic:equal parameter="number" value="7">
You guessed right! You win a high speed blender!
</logic:equal>
<!-- If the number guessed was wrong -->
<logic:notEqual parameter="number" value="7">
<!-- Less Than -->
<logic:lessThan parameter="number" value="7">
A little higher...
</logic:lessThan>
<!-- Greater Than -->
<logic:greaterThan parameter="number" value="7">
A little lower...
</logic:greaterThan>
</logic:notEqual>
And here is the same logic within a velocity template
<!-- Is the number guess right? -->
#if ( $number == 7 )
You guessed right! You win a high speed blender!
#end
<!-- If the number guessed was wrong -->
#if ( $number != 7 )
<!-- Less Than -->
#if ( $number < 7 )
A little higher...
<!-- Greater Than -->
#elseif ( $number > 7 )
A little lower...
#end
#end
Now a another example with the same task. Create a list and display it. First JSP.
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%
java.util.ArrayList list = new java.util.ArrayList();
list.add("First");
list.add("Second");
list.add("Third");
list.add("Fourth");
list.add("Fifth");
pageContext.setAttribute("list", list, PageContext.PAGE_SCOPE);
%>
<logic:iterate id="myCollectionElement" name="list">
Element Value: <bean:write name="myCollectionElement" /><br />
</logic:iterate>
And here is the velocity version.
#set ( $list = ["First", "Second", "Third", "Fourth", "Fifth"] )
#foreach ( $item in $list )
Element Value: $item<br />
#end
Conclusion
JSP has provided developers with a very useful library and toolset. It has been around for a long time and there are no signs that its users are declining. Due to MVC style architecture, server side processing including business logic should not take place within the view component. I did not get into Velocity vs Freemarker debate since they are identical from an architectural standpoint. You can make that decision by comparing the template language itself and choose which is better suited for your needs. When determining which technology to utilize I hope that you take time to research each technology and use this article as a reference.
分享到:
相关推荐
### jsp、freemarker、velocity简介和对比 在Java Web开发领域,模板引擎是连接后端业务逻辑与前端展示层的重要桥梁。本文将重点介绍三种常用的Java Web模板引擎:JSP(Java Server Pages)、Freemarker以及...
在 WebWork2 中,我们可以随意选择使用 Freemarker 或 Velocity 作为 View,模板技术作为 View 的好处是很多,尤其和 JSP 比较起来优点更大。 Velocity 和 Freemarker 都是开源的模板技术,它们都提供了强大的功能...
标题“velocity和freemarker的比较”涉及到的是两个在Java Web开发中常用的模板引擎——Velocity和FreeMarker。它们都是用于将静态模板与动态数据结合,生成HTML或其他类型的文本输出,常用于MVC(模型-视图-控制器...
### Velocity与FreeMarker的区别 在IT领域特别是Java开发中,模板引擎是不可或缺的一部分,它们用于将数据模型转换为HTML、PDF、Word文档等格式。在众多模板引擎中,Velocity和FreeMarker是两种非常受欢迎的选择。...
本篇文章将对四个流行的Java模板引擎——Velocity、FreeMarker、Smarty4j以及HTTL进行效率分析,旨在探讨它们在处理业务逻辑编译和性能方面的优劣。 1. Velocity: Velocity是Apache软件基金会的一个开源项目,以其...
Freemarker和Velocity是两种广泛使用的模板引擎,它们在Java Web开发中扮演着重要的角色,主要用于生成动态HTML或其他格式的文本。这两者都是基于MVC(Model-View-Controller)设计模式,允许开发者将业务逻辑与展示...
在这种场景下,"Jsp结合Velocity实现依据Word模板文件生成对应数据文件"的技术方案显得尤为实用。JSP(JavaServer Pages)是用于构建动态Web应用的服务器端技术,而Velocity则是一个强大的模板引擎,它允许开发者将...
总的来说,Freemarker和Velocity的Eclipse插件对于Java Web开发者来说是必不可少的工具,它们将模板引擎的强大功能与Eclipse的优秀开发环境紧密结合,为开发者带来了极大的便利。在实际项目中,结合这些插件,开发者...
它提供了请求处理、模型绑定、异常处理等功能,并支持多种视图技术,如JSP、Freemarker和Velocity。 2. **Freemarker**: Freemarker是一个模板引擎,用于生成动态HTML或其他文本格式的输出。在本项目中,...
在已经集成了JSP的Spring环境中,我们有时会需要进一步集成Velocity,以利用其独特的优势,如更快的渲染速度和更清晰的逻辑分离。 集成Velocity主要涉及以下几个步骤: 1. **引入依赖**:首先,我们需要在项目中...
UAP基于FIS3,封装以JAVA为后端,velocity、JSP、FreeMarker为模板的UAP前端解决方案安装npm install fis3 uap -g使用开发 uap release生产 uap release prod
本话题将详细讲解如何在Spring MVC框架中整合Velocity和JSP,实现多视图解析器的功能,从而根据需求返回不同的视图。 **1. Velocity模板引擎** Velocity是一个开源的Java模板引擎,它允许开发者将业务逻辑与展示...
它基于模板来生成文本输出,仅是利用模板加上数据生成文本页面,即是由Java程序准备要显示的数据,由FreeMarker生成页面,通过模板显示准备的数据,并可以在模板中使用JSP标签。 Velocity是一个基于JAVA的模板引擎...
WebWork的核心特性之一是支持多种视图技术,包括JavaServer Pages (JSP)、Velocity模板引擎、可扩展样式表语言转换 (XSLT) 以及Jasper Reports等。这意味着开发者可以根据项目需求选择最适合的技术栈来构建应用的...
Velocity和FreeMarker是两种常用的Java模板引擎,它们可以方便地结合JSP(JavaServer Pages)进行动态网页生成,并实现页面静态化。本文将详细介绍如何使用Velocity和FreeMarker模板实现页面静态化,并提供具体的...
Struts2是一个强大的MVC(Model-View-Controller)框架,而Velocity则是一个模板引擎,它专注于视图层的呈现,可以替代传统的JSP(JavaServer Pages)来实现更加简洁、高效的页面渲染。 **Struts2框架** Struts2是...
FreeMarker是一个开源的模板引擎,主要用于生成HTML Web页面,尤其适用于MVC模式的应用程序。...尽管Velocity等其他工具也有其优点,但FreeMarker的广泛特性和强大功能使其在许多场景下成为更优选择。
- Nutz的MVC模式中,视图层支持多种模板引擎,包括JSP、Velocity、Freemarker等。 2. **Freemarker**: - Freemarker是一个模板语言,它与Java代码分离,使得开发者可以专注于页面布局和设计,而无需关心业务逻辑...
Struts2集成FreeMarker和Velocity,写成了工具类,快速实现页面静态化,以后直接调用即可,无需修改任何源代码,改压缩文件包括 1、工具类; 2、源码(含jar包); 3、mysql数据库可执行文件; 4、struts2集成...