`
xuzhfa123
  • 浏览: 119247 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

JSP Velocity Freemarker comparison

    博客分类:
  • Java
阅读更多
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.
分享到:
评论
2 楼 tomorrow009 2011-04-12  
Thanks buddy, your article helps me a lot.
1 楼 BenjaminLju 2010-08-12  
Thanks,a very good article indeed!

相关推荐

    2025最新空调与制冷作业(运行操作)考试题库及答案.docx

    2025最新空调与制冷作业(运行操作)考试题库及答案.docx

    无监督视频对象分割领域的跨模态与帧间注意力机制研究及其应用

    内容概要:本文提出了一种新的无监督视频对象分割(unsupervised VOS)方法——双原型注意力机制(Dual Prototype Attention),即IMA(跨模态注意模块)和IFA(帧间注意模块)。这些机制分别解决了现有多模态融合和时间聚集方法中存在的鲁棒性和计算效率等问题,显著提高了在多个公开基准数据集上的表现。此外,论文还探讨了原型嵌入对性能的影响并对其进行了验证。 适合人群:对视频处理特别是无监督视频对象分割领域感兴趣的计算机视觉研究员和技术开发者。 使用场景及目标:适用于各种需要进行高质量自动图像或视频内容分析的应用环境,如智能监控、增强现实、自动驾驶等领域。具体的目标是提高模型识别最突出物体时的精度以及稳定性,即使遇到遮挡或者复杂背景也能有效运作。 阅读建议:本篇文献提供了详尽的技术细节和支持性实验结果来展示所提出的DPA方法优越之处。因此,在理解和评估该研究成果的基础上可以深入了解如何利用注意力机制提升深度学习模型的效果,尤其是对于涉及时间和空间维度的数据处理任务非常有价值。

    Gartner发布2025年网络治理、风险与合规战略路线图

    新型网络风险和合规义务,日益成为网络治理、风险与合规实践面临的问题。安全和风险管理领导者可以参考本文,实现从被动、专注于合规的方法到主动、进一步自动化方法的转型。 主要发现 不断变化的监管环境和不断扩大的攻击面,使企业机构难以实现网络治理、风险与合规(GRC)与其整体风险管理战略的协调,因此必须推动GRC进行战略性转变。然而,许多安全和风险管理(SRM)领导者难以适应这些变化。 重心在满足监管要求的话,通常会导致被动的网络风险管理和评估方式。因此,网络安全团队与业务部门之间的接触和协作通常较低。 许多网络GRC管理流程缺乏充分且相关的技术自动化,导致资源紧张和控制测试疲劳。

    基于java+ssm+mysql的数据库系统原理课程平台 源码+数据库+论文(高分毕设项目).zip

    项目已获导师指导并通过的高分毕业设计项目,可作为课程设计和期末大作业,下载即用无需修改,项目完整确保可以运行。 包含:项目源码、数据库脚本、软件工具等,该项目可以作为毕设、课程设计使用,前后端代码都在里面。 该系统功能完善、界面美观、操作简单、功能齐全、管理便捷,具有很高的实际应用价值。 项目都经过严格调试,确保可以运行!可以放心下载 技术组成 语言:java 开发环境:idea 数据库:MySql8.0 部署环境:Tomcat(建议用 7.x 或者 8.x 版本),maven 数据库工具:navicat

    基于FATFS系统的STM32F407 SD卡升级Bootloader程序:自动检测与升级流程,stm32f407 SD卡升级 bootloader程序 基于sdio fatfs系统的stm32 b

    基于FATFS系统的STM32F407 SD卡升级Bootloader程序:自动检测与升级流程,stm32f407 SD卡升级 bootloader程序 基于sdio fatfs系统的stm32 bootloader程序 功能简介: 本程序使用fatfs系统读取bin文件。 开机后会自动检测sd卡,检测到sd卡后,再读取固定名称的bin文件,之后会对bin文件进行首包校验,判断该升级包的起始地址是否正确,正确的话,就循环读取bin文件并写入到flash中。 完成升级。 详细流程请看流程图 ,stm32f407; SD卡升级; bootloader程序; fatfs系统读取bin文件; 检测SD卡; 首包校验; 循环写入flash。,STM32F407 SD卡升级Bootloader程序:基于SDIO FATFS系统实现自动升级功能

    激光设备上位机源码解析:基于欧姆龙NJplc通讯协议与多种激光器控制实现,激光设备上位机源码+基于欧姆龙NJplc上位机+各种常见激光器通讯控制 ,核心关键词:激光设备上位机源码; 欧姆龙NJpl

    激光设备上位机源码解析:基于欧姆龙NJplc通讯协议与多种激光器控制实现,激光设备上位机源码+基于欧姆龙NJplc上位机+各种常见激光器通讯控制。 ,核心关键词:激光设备上位机源码; 欧姆龙NJplc上位机; 常见激光器通讯控制; PLC控制。,"欧姆龙NJplc驱动的激光设备上位机控制源码:通用激光器通讯管理"

    高效数字电源方案:图腾柱无桥pfc技术,两相交错设计,5G一体化电源批量出货,宽电压输入与高效输出,功率覆盖至kW级别,高效数字电源方案,图腾柱无桥pfc,两相交错,5g一体化电电源上已批量出,输入1

    高效数字电源方案:图腾柱无桥pfc技术,两相交错设计,5G一体化电源批量出货,宽电压输入与高效输出,功率覆盖至kW级别,高效数字电源方案,图腾柱无桥pfc,两相交错,5g一体化电电源上已批量出,输入175-265V,输出42-58V;输出效率97%,2kW 3kW都有 ,高效数字电源方案; 图腾柱无桥pfc; 两相交错; 5g一体化电电源; 批量出货; 宽输入电压范围; 高输出效率; 2kW和3kW功率。,"高效图腾柱无桥PFC电源方案,两相交错5G电平已大批量生产,宽输入范围输出高效率"

    COMSOL三维采空区通风条件下的氧气与瓦斯浓度分布研究,comsol三维采空区通风条件下,氧气,瓦斯浓度分布 ,核心关键词:comsol; 三维采空区; 通风条件; 氧气浓度分布; 瓦斯浓度分布

    COMSOL三维采空区通风条件下的氧气与瓦斯浓度分布研究,comsol三维采空区通风条件下,氧气,瓦斯浓度分布。 ,核心关键词:comsol; 三维采空区; 通风条件; 氧气浓度分布; 瓦斯浓度分布;,"三维采空区通风模拟:氧气与瓦斯浓度分布研究"

    基于java+ssm+mysql的餐馆点餐系统 源码+数据库+论文(高分毕设项目).zip

    项目已获导师指导并通过的高分毕业设计项目,可作为课程设计和期末大作业,下载即用无需修改,项目完整确保可以运行。 包含:项目源码、数据库脚本、软件工具等,该项目可以作为毕设、课程设计使用,前后端代码都在里面。 该系统功能完善、界面美观、操作简单、功能齐全、管理便捷,具有很高的实际应用价值。 项目都经过严格调试,确保可以运行!可以放心下载 技术组成 语言:java 开发环境:idea 数据库:MySql8.0 部署环境:Tomcat(建议用 7.x 或者 8.x 版本),maven 数据库工具:navicat

    Python自动化办公源码-07一键往Word文档的表格中填写数据

    Python自动化办公源码-07一键往Word文档的表格中填写数据

    2025最新初级保育员理论知识考试题库及答案.doc

    2025最新初级保育员理论知识考试题库及答案.doc

    基于Tent混沌映射改进的麻雀算法SSA优化BP神经网络(Tent-SSA-BP)回归预测MATLAB代码教程:电厂数据预测(含优化对比),基于Tent混沌映射改进的麻雀算法SSA优化BP神经网络(T

    基于Tent混沌映射改进的麻雀算法SSA优化BP神经网络(Tent-SSA-BP)回归预测MATLAB代码教程:电厂数据预测(含优化对比),基于Tent混沌映射改进的麻雀算法SSA优化BP神经网络(Tent-SSA-BP)回归预测MATLAB代码(有优化前后的对比) 代码注释清楚。 main为运行主程序,可以读取本地EXCEL数据。 很方便,容易上手。 (以电厂运行数据为例) 温馨提示:联系请考虑是否需要,程序代码,一经出,概不 。 ,Tent-SSA; BP神经网络; 回归预测; MATLAB代码; 优化对比; 代码注释; 主程序; EXCEL数据读取; 电厂运行数据。,基于Tent混沌映射与SSA优化的BP神经网络回归预测MATLAB代码(含前后对比及清晰注释)

    西门子1200 PLC轴运动控制程序模板-涵盖伺服控制、电缸、通讯及报警功能,适用于装路由器壳子的机器,具备电路图与触摸屏程序,供学习与借鉴 ,SIEMENS 西门子西门子1200plc轴运动控制程

    西门子1200 PLC轴运动控制程序模板——涵盖伺服控制、电缸、通讯及报警功能,适用于装路由器壳子的机器,具备电路图与触摸屏程序,供学习与借鉴。,SIEMENS 西门子西门子1200plc轴运动控制程序模板 介绍:此程序是之前给海康威视做的一台装路由器壳子的机器。 程序有以下: 1):调用轴控制块做的控制3个伺服, 2):1个电缸, 3):用PUT GET块与上下游plc通讯, 4):轴控制块 5):气缸报警块 6):完整的电路图 7):威纶通触摸屏程序 8):IO表 程序块已经在很多个项目上成熟应用,可以直接调用,对于做西门子1200轴控制等有很好的学习借鉴意义。 好好看一遍,有很大的提高作用。 ,SIEMENS; 1200plc; 轴运动控制; 程序模板; 伺服控制; 电缸控制; PLC通讯; 威纶通触摸屏程序; 电路图; IO表,西门子1200 PLC轴运动控制模板:海康威视项目成熟应用示例

    《2023年未来就业报告》:人工智能对未来就业市场的影响及应对措施

    内容概要:本文详细探讨了人工智能(AI)对就业市场的深远影响及其发展趋势。首先介绍了到2027年,44%的工人核心技能将受技术变革尤其是AI影响的事实,并提及自动化可能取代部分工作的现象。其次指出虽然某些职位面临风险,但也带来了全新的职业机遇与现有角色改进的可能性,关键在于人类要学会借助AI释放自身潜力并培养软实力,以适应快速发展的科技需求。再者,强调终身学习理念下企业和教育培训须革新教学手段与评估机制,以便紧跟AI进化速率,为个体和社会持续注入新动力。最后提到了教育机构应当加快调整步伐以匹配技术变革的速度,并利用AI实现个性化的教育,进而提升学习者的适应能力和解决问题的能力。 适用人群:政策制定者、企业管理层、在职人员及教育工作者,还有广大学生群体均能从中获得启示。 使用场景及目标:面向关注未来职场动向及教育发展方向的专业人士,提供前瞻性思考角度,助力各界积极规划职业生涯路径或调整教育资源分配策略。 其他说明:本文综合多位行业领袖的观点展开讨论,旨在唤起社会各界共同思考AI带来的变革及对策,而非单方面渲染危机感。

    谷歌 Adsense 合同(中文)

    谷歌 Adsense 在线服务条款,首次办理接收谷歌 Adsense 付款时需要提交的一份证明材料(合同/协议),需要提交中文版,已经翻译成中文了。

    Python自动化办公源码-14用Python按时间分割txt文件中的数据

    Python自动化办公源码-14用Python按时间分割txt文件中的数据

    Native SQLite Manager for Mac v1.29.2

    Native SQLite Manager for Mac是一款极简的SQLite数据库管理工具,专为Mac用户设计。它支持多种SQLite版本、SQLCipher加密和SQLite扩展,提供自动补全、语法高亮和SQL格式化功能。用户可以通过简洁直观的界面轻松创建、编辑、删除和备份数据库文件。软件还支持数据导入导出(如CSV、JSON、XML格式),方便数据迁移和备份。其多数据库管理功能允许同时打开多个数据库文件,提升工作效率。Native SQLite Manager适合开发者、数据分析师和学生使用,是高效管理SQLite数据库的理想选择。

    西门子SMART 200电机控制子程序V1.6:智能管理多达7个电机,灵活设置运行参数,故障自动切换备用电机,版本升级持续优化 ,西门子SMART 200 电机控制子程序V1.6,可生成库 可控制1

    西门子SMART 200电机控制子程序V1.6:智能管理多达7个电机,灵活设置运行参数,故障自动切换备用电机,版本升级持续优化。,西门子SMART 200 电机控制子程序V1.6,可生成库 可控制1-7个电机 可设置同时运行的最大电机数量 可设置每个电机是否使用 可设置电机轮时间,当系统单次运行时间>轮时间,停止运行时间最长的电机,上累计运行时间最短的电机 可设置电机启动间隔 每次启动累计运行时间最短的电机 当有电机故障时,立即停止该电机,如果有备用电机自动切备用电机 7个电机内,可自由设置备用电机个数,使用的电机总数-最大电机数量=备用电机个数 附版本升级记录: V1.1优化:当使能被关闭后自动关闭对应电机 V1.2优化:运行中改变同时使用电机数量有效 V1.3更改:open信号上升沿直接启动1个电机(跳过启动间隔),第二个电机启动间隔才有效 轮时间改为秒,当系统单次运行时间>轮时间,停止运行时间最长的电机,上累计运行时间最短的电机 V1.4优化 V1.5满足可以运行的电机数量>同时使用电机数量 时 轮才有效,不满足时,轮计时清零 V1.6 优化某些情况下,无法正确延时 ,核心关键词

    2012-2023年劳务外包数据(劳务派遣或灵活就业等)(全新整理)

    1、资源内容地址:https://blog.csdn.net/2301_79696294/article/details/144634118 2、数据特点:今年全新,手工精心整理,放心引用,数据来自权威,且标注《数据来源》,相对于其他人的控制变量数据准确很多,适合写论文做实证用 ,不会出现数据造假问题 3、适用对象:大学生,本科生,研究生小白可用,容易上手!!! 4、课程引用: 经济学,地理学,城市规划与城市研究,公共政策与管理,社会学,商业与管理

    面向能源互联网的云边协同技术研究.pdf

    面向能源互联网的云边协同技术研究.pdf

Global site tag (gtag.js) - Google Analytics