为什么要使用模板引擎? jsp include和jstl vs tile,freemarker,speed,sitemesh [英] Why would I use a templating engine? jsp include and jstl vs tiles, freemarker, velocity, sitemesh

查看:158
本文介绍了为什么要使用模板引擎? jsp include和jstl vs tile,freemarker,speed,sitemesh的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将选择一种方式来组织我的视图(使用spring-mvc,但这没关系)

I'm about to choose to way to organize my view (with spring-mvc, but that shouldn't matter much)

据我所知,有6个选项(尽管它们不是互斥的):

There are 6 options as far as I see (though they are not mutually exclusive):

  • 平铺
  • Sitemesh
  • Freemarker
  • 速度
  • <jsp:include>
  • <%@ include file="..">
  • Tiles
  • Sitemesh
  • Freemarker
  • Velocity
  • <jsp:include>
  • <%@ include file="..">

Tiles Sitemesh 可以分组; Freemarker Velocity 也是如此.每个小组中使用哪个小组都不是本次讨论的问题,对此有足够的疑问和讨论.

Tiles and Sitemesh can be grouped; so can Freemarker and Velocity. Which one within each group to use is not a matter of this discussion, there are enough questions and discussions about it.

这是有趣的读物,但不能说服我使用瓷砖.

This is an interesting read, but can't quite convince me to use tiles.

我的问题是-这些框架给出了什么? <@ include file="..">和JSTL无法正常完成.要点(摘自本文):

My question is - what do these frameworks give that can't be properly done with <@ include file=".."> and JSTL. Main points (some taken from the article):

  1. 包括页面的某些部分,例如页眉和页脚-两者之间没有区别:

  1. Including parts of pages, like header and footer - there isn't a difference between:

<%@ include file="header.jsp" %>

<tiles:insert page="header.jsp" />

  • 在标题中定义参数-例如标题,元标记等.这非常重要,尤其是从SEO角度来看.使用模板选项,您可以简单地定义每个页面都应定义的占位符.但是,因此您可以使用 JSTL 在jsp中,使用<c:set>(在包含页面中)和<c:out>(在包含页面中)

  • Defining parameters in the header - like title, meta tags, etc. This is very important, especially from SEO point of view. With the templating options you can simply define a placeholder which each page should define. But so you can in jsp with JSTL, using <c:set> (in the including page) and <c:out> (in the included page)

    布局重组-如果要将面包屑移至菜单上方,或者将登录框移至另一侧面板上方.如果页面包含(使用jsp)的组织不佳,则在这种情况下可能需要更改每个页面.但是,如果您的布局不太复杂,并且将常见的内容放在页眉/页脚中,则无需担心.

    Layout reorganization - if you want to move the breadcrumb above the menu, or the login box above another side-panel. If page inclusions (with jsp) is not well organized, you might need to change every single page in such cases. But if your layout is not overly complex, and you put the common things in header/footer, there is nothing to worry about.

    通用组件和特定内容之间的耦合-我没有发现任何问题.如果要重用某些片段,请将其移到不包含任何页眉/页脚的页面,并在需要的地方包含它.

    Coupling between the common components and the specific content - I don't find an issue with this. If you want to reuse some fragment, move it to a page that doesn't include any header/footer, and include it wherever needed.

    效率-<%@ include file="file.jsp" %>效率最高,因为它只编译一次.所有其他选项都会被解析/执行多次.

    Efficiency - <%@ include file="file.jsp" %> is more efficient than anything else, because it is compiled once. All other options are parsed/executed many times.

    复杂性-所有非jsp解决方案都需要附加的xml文件,附加的include,预处理器配置等.这既是学习曲线,又是引入了更多潜在故障点的原因.另外,它使支持和更改变得更加乏味-您必须检查许多文件/配置才能了解正在发生的事情.

    Complexity - all non-jsp solutions require additional xml files, additional includes, pre-processor configurations, etc. This is both a learning curve and introducing more potential points of failure. Also, it makes support and changing more tedious - you have to check a number of files/configurations in order to understand what's happening.

    占位符-速度/freemarker除了JSTL还能提供更多功能吗?在JSTL中,您放置了占位符,并使用模型(由控制器放置在请求或会话范围中)填充了这些占位符.

    Placeholders - do velocity/freemarker give anything more than JSTL? In JSTL you put placeholder, and use the model (placed in request or session scope, by controllers) to fill these placeholders.

    所以,说服我,除了普通的JSP之外,我还应该使用以上任何框架.

    So, convince me that I should use any of the above frameworks instead of/in addition to plain JSP.

    推荐答案

    Velocity的一些参数(我没有使用Freemarker):

    A few arguments for Velocity (I haven't used Freemarker):

    • 可能会在网络环境之外重用模板,例如在发送电子邮件中
    • Velocity的模板语言语法比JSP EL或标记库简单
    • 将视图逻辑与任何其他种类的逻辑严格分开-无法使用下拉菜单使用scriptlet标签并在模板中执行令人讨厌的事情.
    • Potential to re-use templates outside of a web context, such as in sending emails
    • Velocity's template language syntax is far simpler than JSP EL or tag libraries
    • Strict separation of view logic from any other sort of logic - no possible option to drop down to using scriptlet tags and doing nasty things in your templates.

    占位符-速度/自由生成器除了提供JSTL之外还提供其他功能吗?在JSTL中,您放置了占位符,然后使用模型(由控制器放置在请求或会话范围中)填充这些占位符.

    Placeholders - do velocity/freemaker give anything more than JSTL? In JSTL you put placeholder, and use the model (placed in request or session scope, by controllers) to fill these placeholders.

    是的,引用确实是VTL的核心:

    Yes, references are really the core of VTL:

    <b>Hello $username!</b>
    

    #if($listFromModel.size() > 1)
        You have many entries!
    #end
    

    效率-<%@ include file="file.jsp" %>效率最高,因为它只编译一次.所有其他选项都会被多次解析/执行.

    Efficiency - <%@ include file="file.jsp" %> is more efficient than anything else, because it is compiled once. All other options are parsed/executed many times.

    不确定我是否同意或理解这一点. Velocity具有缓存模板的选项,这意味着将对解析到模板中的抽象语法树进行缓存,而不是每次都从磁盘读取.无论哪种方式(我对此都没有实数),Velocity一直对我来说感觉.

    Not so sure I agree with or understand this point. Velocity has an option to cache templates, meaning the abstract syntax tree they are parsed into will be cached rather than read from disk each time. Either way (and I don't have solid numbers for this), Velocity has always just felt fast for me.

    布局重组-如果要将面包屑移动到菜单上方,或者将登录框移动到另一侧面板上方.如果页面包含(使用jsp)的组织不佳,则在这种情况下可能需要更改每个页面.但是,如果您的布局不是过于复杂,并且将常见的内容放在页眉/页脚中,则无需担心.

    Layout reorganization - if you want to move the breadcrumb above the menu, or the login box above another side-panel. If page inclusions (with jsp) is not well organized, you might need to change every single page in such cases. But if your layout is not overly complex, and you put the common things in header/footer, there is nothing to worry about.

    区别在于,采用JSP方法时,您是否会在使用相同页眉/页脚的每个JSP文件中重新组织此布局? Tiles和SiteMesh允许您指定基本布局页面(JSP,Velocity模板等-它们都是JSP框架的核心),您可以在其中指定所需的内容,然后将其委托给主要内容的内容"片段/模板.这意味着只有一个文件可将标题移入其中.

    The difference is, with a JSP approach, wouldn't you be re-organzing this layout in every JSP file that uses the same header/footer? Tiles and SiteMesh allow you to specify a base layout page (JSP, Velocity template, etc - both are JSP frameworks at their heart) where you can specify whatever you want and then just delegate to a "content" fragment/template for the main content. This means there would be just one file to move the header in.

    这篇关于为什么要使用模板引擎? jsp include和jstl vs tile,freemarker,speed,sitemesh的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆