jsp模板继承

jsp通过自定义标签实现类似模板继承的效果

  1. 关于标签的定义、注册、使用在上面文章均以一个自定义时间的标签体现,如有不清楚自定义标签流程的话请参考这篇文章 http://www.cnblogs.com/zhuchenglin/p/8109787.html
  2. 关于jsp 的模板继承 请参考 这篇文章,上面的使用方法非常清楚,这里就不再说明
  3. 自定义标签实现类似模板继承的效果 参考

下面来看一下我的代码:

1. 在新建包 com.xiangmu.tags 中新建两个类,一个是 BlockTag.java,另一个是OverwriteTag.java,内容如下:

BlockTag.java 占位标签

public class BlockTag extends BodyTagSupport {
	 /**
     * 占位模块名称
     */
    private String name;

    private static final long serialVersionUID = 1425068108614007667L;
    
    public int doStartTag() throws JspException{
        return super.doStartTag();                
    }
    
    public int doEndTag() throws JspException {
        ServletRequest request = pageContext.getRequest();
        //block标签中的默认值
        String defaultContent = (getBodyContent() == null)?"":getBodyContent().getString();        
        String bodyContent = (String) request.getAttribute(OverwriteTag.PREFIX+ name);
        //如果页面没有重写该模块则显示默认内容
        bodyContent = StringUtils.isEmpty(bodyContent)?defaultContent:bodyContent;
        try {
            pageContext.getOut().write(bodyContent);
        } catch (IOException e) {
            e.printStackTrace();
        }        
        return super.doEndTag();
    } 
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

OverwriteTag.java 重写标签

public class OverwriteTag extends BodyTagSupport {
 private static final long serialVersionUID = 5901780136314677968L;
    //模块名的前缀
    public static final String PREFIX = "JspTemplateBlockName_";
    //模块名
    private String name;
    
    public int doStartTag() throws JspException {
        return super.doStartTag();
    }
    
    public int doEndTag() throws JspException {
        ServletRequest request = pageContext.getRequest();
        //标签内容
        BodyContent bodyContent = getBodyContent();
        request.setAttribute(PREFIX+name,  StringUtils.trim(bodyContent.getString()));        
        return super.doEndTag();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }    
}
 
2. 注册标签
	在WEB-INF文件夹下写一个自定义标签的配置文件我的以mytags.tld为例:
	<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
  PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
  "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
  <tlib-version>1.0</tlib-version><!-- 代表标签库的版本号 -->
  <jsp-version>1.2</jsp-version><!-- 代表jsp的版本 -->
  <short-name>mt</short-name><!-- 你的标签库的简称(我这里取mytag的首字母mt) -->
  <uri>http://xiangmu.com/mytags</uri><!-- 你标签库的引用uri -->
  
  <tag>
      <name>block</name><!-- 你定义的标签的名称 -->
       <tag-class>com.xingmu.tags.BlockTag</tag-class><!-- 对应的标签处理程序:包名+类名 -->
      	<attribute>
			<name>name</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
      <!--  <body-content>JSP</body-content> --><!-- 标签体内容的格式 -->
  </tag>
   <tag>
      <name>overwrite</name><!-- 你定义的标签的名称 -->
       <tag-class>com.xingmu.tags.OverwriteTag</tag-class><!-- 对应的标签处理程序:包名+类名 -->
      <attribute>
			<name>name</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
      <!--<body-content>JSP</body-content> --><!-- 标签体内容的格式 -->
  </tag>
</taglib>

3. 使用示例

基础页面 menu.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="tmpl" uri="http://xingmu.com/mytags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<!--[if IE 9]>         <html class="no-js lt-ie10" lang="en"> <![endif]-->
<!--[if gt IE 9]><!--> <html class="no-js" lang="en"> <!--<![endif]-->
    <head>
        <meta charset="utf-8">

        <title>title</title>

        <meta name="description" content="AppUI is a Web App Bootstrap Admin Template created by pixelcave and published on Themeforest">
        <meta name="author" content="pixelcave">
        <meta name="robots" content="noindex, nofollow">

        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

   </head>
    <body>
    	<div>
    		<tmpl:block name="page-content"></tmpl:block>
    	</div>
    </body>
</html>

子页面 继承上面的基础页面 index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="tmpl" uri="http://xiangmu.com/mytags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<tmpl:overwrite name="page-content">
    	//写入要重写的内容
</tmpl:overwrite>
 <%@include file="menu.jsp"%>   	

这样最终访问index.jsp时所显示的页面就是这两个页面的组合。

注:本文是经过自己在网上搜的资料结合自己的想法所得,如需转载请注明出处:http://www.cnblogs.com/zhuchenglin/p/8110199.html
posted @ 2017-12-25 17:00  lin_zone  阅读(692)  评论(0编辑  收藏  举报