写JSP和Struts的人肯定对JSP中一堆的JSP TAG觉得挺烦的。
例如输出一个属性就要写那么多代码
<s:property value=" userName "/>
而写一些嵌套就更难看了,所以,很多人转向了Tapstry和Freemarker。
废话少说,来看看一个Facelets的例子:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:c="http://java.sun.com/jstl/core">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Hello</title>
</head>
<body>
Hi. 我是#{UserBean.name}<p/>
<form id="helloForm" jsfc="h:form">
<input type="text" jsfc="h:inputText" id="userNo"
value="#{UserBean.userNumber}"
validator="#{UserBean.validate}"/>
<p />
<input type="submit" jsfc="h:commandButton" id="submit"
action="success" value="Submit" />
<p />
<h:message showSummary="true" id="errors1" for="userNo"/>
<p />
<c:forEach begin="1" end="4" varStatus="v">
#{view.viewId} #{v.index}<br/>
</c:forEach>
</form>
</body>
</html>
请注意以上代码的以下特点:
- 输出一个变量变得如此简单“我是#{UserBean.name}”,和freemarker差不多;
- 有利于原型法修改html页面:通过jsfc来绑定一个UI空间,不需要<ui:input这种方式来指定了,保持了html的原始面貌;有点像Tapestry的jwcid属性;
- 对于其他的Tag,可以完全照常使用。
JSP的优点:
1)可平衡现有的JSP应用;
2)很多极好的IDE均支持JSP。
Facelets的优点:
1)在性能和可伸缩性方面大大改进;
2)快速地模板/装饰功能,支持页面重用;
3)精确地定位错误报告;
4)完整支持EL表达式。
结论:除非项目被迫使用JSP,否则最好是选用Facelets。
本文参考了:http://www.jsfcentral.com/articles/facelets_1.html,我只描述了精华部分。