码迷,mamicode.com
首页 > Web开发 > 详细

<jsp:invoke fragment=""/>的理解和使用

时间:2015-03-13 18:09:23      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

在传统 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。

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="t" tagdir="/WEB-INF/tags/"%>
http://write.blog.csdn.net/postedit
<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 中设计的样式来进行布局。

 

<jsp:invoke fragment=""/>的理解和使用

标签:

原文地址:http://www.cnblogs.com/wangshuo1/p/4335328.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!