`
cucaracha
  • 浏览: 138184 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
博客专栏
A8f3fa2f-18ce-3862-897a-5d2209197c90
Java NIO.2
浏览量:86087
7a076ba7-8ec8-3241-aa3c-67bb2f7856a2
Java EE 7 简明教...
浏览量:35789
社区版块
存档分类
最新评论

[JSP] JSP Fragment 实现布局管理

    博客分类:
  • JSP
阅读更多
在传统 JSP 中,想要实现页面布局管理比较麻烦,为了解决在 JSP 中布局的问题,出现了很多开源软件,比如 Apache Tiles 和 SiteMesh 就是其中比较优秀的。但是使用开源软件实现布局或多或少会产生一些性能问题,有没有办法在不依赖第三方开源软件的情况下,使用 JSP 本身来实现页面布局呢?

JSP 2.0 引入了 Fragment 技术,使用 Fragment 技术可以在 JSP 中实现类似 Tiles 和 SiteMesh 的页面布局管理。

下面的例子说明了如何使用 Fragment 实现页面布局。

1、首先在 WEB-INF/tags 文件夹中创建 template.tag 文件:

<%@tag description="template 1" pageEncoding="UTF-8"%>
<%@attribute name="header" fragment="true" %>
<%@attribute name="footer" fragment="true" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>

    <body>
        <jsp:invoke fragment="header"/>
        <jsp:doBody/>
        <jsp:invoke fragment="footer"/>
    </body>
</html>


在 tag 文件头部申明了两个 attribute 分别是 header 和 footer。在 <body> 标签中调用了这两个 attribute 所对应的 fragment。jsp:invoke 和 jsp:doBody 中的具体内容会被 jsp 中的内容替换。现在编写 index.jsp。

2、创建 index.jsp 文件

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="t" tagdir="/WEB-INF/tags/"%>

<t:template>
    <jsp:attribute name="header">
        这里的内容显示在头部。
    </jsp:attribute>
    <jsp:attribute name="footer">
        这里的内容显示在尾部。
    </jsp:attribute>
    <jsp:body>
        这里显示正文内容:Hello World!
    </jsp:body>
</t:template>


jsp:attribute 标签中的内容将会替换 template.tag 中 jsp:invoke 的内容,name 属性对应 fragment 属性。

如果访问 index.jsp 页面,可以看到显示的内容会按照 template.tag 中设计的样式来进行布局。

文章来源:http://www.aptusource.org/2014/03/jsp-layout/
0
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics