java - 选择合适的模板引擎

标签 java templates frameworks

从今天开始 - 我一直使用简单的 JSP 标签和 JSTL,但现在我正在寻找更多的功能,也许还有更多的可用性。

通过谷歌搜索,我找到了似乎非常有名的 Freemaker、Velocity 和 Tiles。你们中有人对其中的一些有进一步的经验吗,可以给我一个简短的介绍。

或者还有其他值得研究的框架吗?

最佳答案

在我看来,对于模板引擎,少即是多。也就是说,您需要更少而不是更多的功能来促进模板的可重用性。

一个仅提供模板引擎严格需要的功能的模板引擎是StringTemplate .这不是我个人在生产系统中使用过的东西,但我正在计划我得到的下一个机会。

如果您有兴趣了解有关该主题/哲学的更多信息,请阅读 Enforcing Strict Model-View Separation in Templating Engines由 StringTemplate 的创建者编写。

如果您只阅读了那篇论文中的一个部分,请阅读第 7 章,其中包括我尝试执行的以下规则(即使在使用 JSP 之类的东西时,它可以说是“太强大”的引擎):

  1. the view cannot modify the model either by directly altering model data objects or by invoking methods on the model that cause side-effects. That is, a template can access data from the model and invoke methods, but such references must be side-effect free. This rule arises partially because data references must be order-insensitive. See Section 7.1.
  2. the view cannot perform computations upon dependent data values because the computations may change in the future and they should be neatly encapsulated in the model in any case. For example, the view cannot compute book sale prices as “$price*.90”. To be independent of the model, the view cannot make assumptions about the meaning of data.
  3. the view cannot compare dependent data values, but can test the properties of data such as presence/absence or length of a multi-valued data value. Tests like $bloodPressure<120 must be moved to the model as doctors like to keep reduc- ing the max systolic pressure on us. Expressions in the view must be replaced with a test for presence of a value simulat- ing a boolean such as $bloodPressureOk!=null Template output can be conditional on model data and com- putations, the conditional just has to be computed in the model. Even simple tests that make negative values red should be computed in the model; the right level of abstraction is usu- ally something higher level such as “department x is losing money.”
  4. the view cannot make data type assumptions. Some type assumptions are obvious when the view assumes a data value is a date, for example, but more subtle type assumptions ap- pear: If a template assumes $userID is an integer, the pro- grammer cannot change this value to be a non-numeric in the model without breaking the template. This rule forbids array indexing such as colorCode[$topic] and $name[$ID] The view further cannot call methods with arguments be- cause (statically or dynamically) there is an assumed argu- ment type, unless one could guarantee the model method merely treated them as objects. Besides graphics designers are not programmers; expecting them to invoke methods and know what to pass is unrealistic.
  5. data from the model must not contain display or layout information. The model cannot pass any display informa- tion to the view disguised as data values. This includes not passing the name of a template to apply to other data values.

关于java - 选择合适的模板引擎,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2082337/

相关文章:

java - RMI:检查客户端崩溃

java - 每次我单击按钮时都会在 Firebase 中创建节点

java - Android 媒体播放器在暂停后无法启动

php - CakePHP View 包括其他 View

c++ - 使用 MSVC 而非 GCC 编译的模板

ios - 在 iOS 上使用安全框架的 SSL

frameworks - 实体编程教程?

java - 摆脱未经检查的分配

c++ - 将函数原型(prototype)发送到模板如何工作?

yii - phpfog 应用程序运行时路径无效