且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么从 JSF2.0 开始,作为视图定义语言,Facelets 比 JSP 更受欢迎?

更新时间:2023-12-04 16:56:40

没错,JSP 有一些 模板 能力,但在 JSF 中使用 JSP 的最大缺点是 JSP 一遇到模板文本内容就写入响应,而 JSF 想做一些预/用它进行后期处理.在 JSF 1.0/1.1 中,以下 JSF 代码

True, JSP has some templating capabilities, but the biggest disadvantage of using JSP in JSF is that JSP writes to the response as soon as it encounters template text content, while JSF would like to do some pre/post processing with it. In JSF 1.0/1.1 the following JSF code

<h:outputText value="first"> second <h:outputText value="third"> fourth

会产生

第二第四第一第三

这是在 JSF 1.0/1.1 时代 头痛.开发人员需要将上例中的 secondfourth 等模板文本包裹在 标签中.JSF 1.2 使用改进的视图处理程序解决了这个问题,该处理程序解析 JSP 而不是执行它,但它在底层仍然非常笨拙,因为 JSP 语法不像 XML 那样格式良好".强烈需要基于 XML 的视图技术,以便可以使用高效的基于 SAX 的解析器.Facelets 诞生了(在 Ken Paulsen 的JSFTemplating"中).

This was during the JSF 1.0/1.1 ages a headache. Developers would need to wrap template text like second and fourth in the above example in <f:verbatim> tags over all place. JSF 1.2 has solved it with an improved view handler which parses the JSP instead of executing it, but it was under the hoods still very clumsy as JSP syntax isn't "well-formed" like XML. A XML based view technology was strongly desired, so that an efficient SAX based parser could be used. And Facelets was born (among Ken Paulsen's "JSFTemplating").

此外,统一的 EL #{} 无法在 JSP 模板文本中使用,导致丑陋的—对于初学者不直观${}#{} 的混合.此外,JSTL 可以在 JSP 上的 JSF 1.x 中不被用作 查看构建时间标签.此外,带有 <% %> 东西的 JSP 语法是老派,在 JSP 中嵌入原始 Java 代码的可能性被认为是 非常糟糕的做法 打破了MVC 意识形态.

Also, unified EL #{} couldn't be used in JSP template text, resulting in ugly —and for starters unintuitive— mixing of ${} and #{}. Also, JSTL could in JSF 1.x on JSP not be used as view build time tags. Also, JSP syntax with <% %> things is old school and the possibility of embedding raw Java code in JSP is considered a very poor practice which breaks MVC ideology.

总而言之,从 JSF/MVC 的角度来看,JSP 简直是丑陋而可怕,而 Facelets 则是干净而令人敬畏.

All with all, in JSF/MVC perspective, JSP is simply ugly and terrible and Facelets is simply clean and awesome.