自定义标签的创建步骤:

自定义标签的四大功能:

自定义标签的类结构:

在 1.0 中呢, 可以将 <body-content> 的值设置为 JSP, 2.0中则不允许在自定义标签体中出现jsp代码。

接下来呢,我直接贴一些Demo代码:

tagdatetag.tld  (标签声明 XML,将之方于 WEB-INF 目录中)

<?xml version="1.0" encoding="UTF-8"?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>A tag library exercising SimpleTag handlers.</description>
<tlib-version>1.0</tlib-version>
<short-name>SimpleTagLibrary</short-name>
<!-- 标签URI,用于在jsp中指定uri -->
<uri>/date-tag</uri> <tag>
<description>Outputs Date String</description>
<name>simple</name>
<tag-class>com.demo.TagDate</tag-class>
<body-content>empty</body-content>
<attribute>
<name>format</name>
<required>false</required> <!-- 属性是否是必须的 -->
<rtexprvalue>true</rtexprvalue> <!-- 让属性在运行时可以写入 -->
</attribute>
</tag> <tag>
<description>Outputs Date String</description>
<name>simple2</name>
<tag-class>com.demo.TagDate2</tag-class>
<body-content>empty</body-content>
<attribute>
<name>format</name>
<required>false</required> <!-- 属性是否是必须的 -->
<rtexprvalue>true</rtexprvalue> <!-- 让属性在运行时可以写入 -->
</attribute>
</tag> <tag>
<name>displayOrSkipBody</name>
<tag-class>com.demo.DisplayOrSkipBody</tag-class>
<body-content>scriptless</body-content>
</tag> <tag>
<name>skipPageOrEvalPage</name>
<tag-class>com.demo.SkipPageOrEvalPage</tag-class>
<body-content>empty</body-content>
</tag> <tag>
<name>interationTagDemo</name>
<tag-class>com.demo.InterationTagDemo</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag> <tag>
<name>modifyBodyContent</name>
<tag-class>com.demo.ModifyBodyContent</tag-class>
<body-content>scriptless</body-content>
</tag> <tag>
<name>if</name>
<tag-class>com.demo.IfTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag> <!-- 标签组 -->
<tag>
<name>choose</name>
<tag-class>com.demo.ChooseTag</tag-class>
<body-content>scriptless</body-content>
</tag> <tag>
<name>when</name>
<tag-class>com.demo.WhenTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag> <tag>
<name>other</name>
<tag-class>com.demo.OtherWiseTag</tag-class>
<body-content>scriptless</body-content>
</tag> </taglib>

index.jsp (使用方法)

<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="date" uri="/date-tag" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body> <%pageContext.setAttribute("name", request.getParameter("name")); %>
<%pageContext.setAttribute("score", request.getParameter("score")); %> Hello. 今天是
<date:simple format="yyyy年MM月dd日 HH:mm:ss" /><br>
<date:simple/><br>
<date:simple2/><br>
<date:simple2 format="yyyy/MM/dd HH:mm:ss" /><br> <date:displayOrSkipBody>
<h2>中国最大的免费在线培训平台</h2>
</date:displayOrSkipBody> <!-- 条件判断标签 -->
<date:if test="${name=='imooc'}">
慕课网是中国最大的IT技能免费学习平台<br>
</date:if>
<date:if test="${name!='imooc'}">
小伙伴们快点加入慕课网一起学习吧。www.imooc.com<br>
</date:if> <%
pageContext.setAttribute("nbastar", new String[]{"jordan", "kobe", "t-mac"});
%>
<!-- 循环遍列数组 -->
<date:interationTagDemo items="${nbastar}" name="name">${name}<br></date:interationTagDemo> <!-- 修改标签体中的内容 -->
<date:modifyBodyContent>虽然我出现在这里,但是我不会显示</date:modifyBodyContent> <!-- 标签组 -->
<date:choose>
<date:when test="${score>90}">非常优秀<br></date:when>
<date:other>加油吧少年<br></date:other>
</date:choose> <br>
<a href="index2.jsp">链接到index2.jsp</a><br>
<a href="index.jsp?score=98">非常优秀</a><br>
<a href="index.jsp?name=imooc">Name为imooc</a><br>
</body>
</html>

部分实现代码:

package com.demo;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport; public class ChooseTag extends SimpleTagSupport {
private boolean flag; public boolean isFlag() {
return flag;
} public void setFlag(boolean flag) {
this.flag = flag;
} @Override
public void doTag() throws JspException, IOException {
getJspBody().invoke(null); // 输出标签体的内容
}
}
package com.demo;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport; public class WhenTag extends SimpleTagSupport {
private boolean test; public void setTest(boolean test) {
this.test = test;
} @Override
public void doTag() throws JspException, IOException {
if (test) {
getJspBody().invoke(null);
ChooseTag choose = (ChooseTag) getParent();
choose.setFlag(true); // 设置父标签的标识为true
}
}
}
package com.demo;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport; public class OtherWiseTag extends SimpleTagSupport { @Override
public void doTag() throws JspException, IOException {
ChooseTag choose = (ChooseTag) getParent();
if (!choose.isFlag()) // 如果父标签的标识为true,表示WhenTag已被执行。那么这里就再执行了
getJspBody().invoke(null);
}
}
package com.demo;

import java.io.IOException;
import java.text.SimpleDateFormat;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport; /** 自定义 Jsp 标签 */
public class TagDate extends TagSupport {
private static final long serialVersionUID = 1L; private String format; public TagDate() {} public String getFormat() {
return format;
} public void setFormat(String format) {
this.format = format;
} @Override
public int doStartTag() throws JspException {
if (format == null || format.length() == 0)
format = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
pageContext.getOut().print(sdf.format(System.currentTimeMillis()));
} catch (IOException e) {
e.printStackTrace();
}
return super.doStartTag();
}
}
package com.demo;

import java.io.IOException;
import java.text.SimpleDateFormat; import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport; /** 自定义 Jsp 标签 */
public class TagDate2 extends SimpleTagSupport {
private String format; public String getFormat() {
return format;
} public void setFormat(String format) {
this.format = format;
} @Override
public void doTag() throws JspException, IOException {
if (format == null || format.length() == 0)
format = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
getJspContext().getOut().print(sdf.format(System.currentTimeMillis()));
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.demo;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.SkipPageException;
import javax.servlet.jsp.tagext.SimpleTagSupport; /**
* @author Administrator
*
*/
public class SkipPageOrEvalPage extends SimpleTagSupport { @Override
public void doTag() throws JspException, IOException {
PageContext context = (PageContext) getJspContext();
HttpServletRequest req = (HttpServletRequest) context.getRequest();
String referer = req.getHeader("Referer");
// 如果来源(referer)为空,则代表是在浏览器中直接输入地址访问的。不继续执行
if (referer == null || referer.length() == 0)
throw new SkipPageException();
}
}

完整源码下载:

链接: http://pan.baidu.com/s/1nu851u5 密码: 456e

【感谢】

慕课网, Eleven_Lee JAVA开发工程师

http://www.imooc.com/learn/480

[Java] JSP笔记 - 自定义标签的更多相关文章

  1. [Java] JSP笔记 - EL、JSTL 常用标签

    一. 什么是 EL 语言 表达式语言(EL)是 JSP 2.0 引入的一种计算和输出 Java 对象的简单语言. 二.EL 语言的作用 为了使JSP写起来更加简单.表达式语言的灵感来自于 ECMASc ...

  2. 【JSP】自定义标签开发入门

    JSP 自定义标签 自定义标签是用户定义的JSP语言元素.当JSP页面包含一个自定义标签时将被转化为servlet,标签转化为对被 称为tag handler的对象的操作,即当servlet执行时We ...

  3. 12、Jsp加强/自定义标签/JavaBean

    1 Jsp加强回顾 Jsp加强 1)Jsp的9大内置对象 request       HttpServletRequet response     HttpServletResponse config ...

  4. Jsp开发自定义标签,自定义标签将字符串转成指定的时间格式显示

    本例以将 字符串格式的时间转成指定的时间格式显示. 第一步.定义一个标签处理程序类,需要集成javax.servlet.jsp.tagext.TagSupport,代码如下: import java. ...

  5. JavaWeb之 JSP:自定义标签

    当jsp的内置标签和jstl标签库内的标签都满足不了需求,这时候就需要开发者自定义标签. 自定义标签 下面我们先来开发一个自定义标签,然后再说它的原理吧! 自定义标签的开发步骤 步骤一 编写一个普通的 ...

  6. JavaWeb之 JSP:自定义标签的创建和使用

    当jsp的内置标签和jstl标签库内的标签都满足不了需求,这时候就需要开发者自定义标签. 下面我们先来开发一个自定义标签,然后再说它的原理吧! 自定义标签的开发步骤 步骤一 编写一个普通的java类, ...

  7. jsp的自定义标签

    1.相对于JSTL或Spring等第三方标签库而言的,用来实现项目中特定的功能需求. 2.自定义标签基本的组成部分 ①页面上看得见的部分 [1]通过taglib引入标签库 [2]标签本身 ②xxx.t ...

  8. Servlet和JSP之自定义标签学习

      此文章会讲述简单标签处理器,因为经典自定义标签处理器没有简单标签处理器方便使用,故在此不进行描述. 参考:慕课网的<JSP自定义标签>视频; <Servlet.JSP和Sprin ...

  9. Java之 jstl 自定义标签的方法

    1.写一个Java类 我的路径是写再tag包中的一个 HelloTag类 package tag; import java.io.IOException; import javax.servlet.j ...

随机推荐

  1. [Erlang 0113] Elixir 编译流程梳理

    注意:目前Elixir版本还不稳定,代码调整较大,本文随时失效      之前简单演示过如何从elixir ex代码生成并运行Erlang代码,下面仔细梳理一遍elixir文件的编译过程,书接上文,从 ...

  2. 从零自学Hadoop(18):Hive的CLI和JDBC

    阅读目录 序 Hive CLI(old CLI) Beeline CLI(new CLI) JDBC Demo下载 系列索引 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出 ...

  3. 常见的高可用MySQL解决方案

    MySQL数据库作为最基础的数据存储服务之一,在整个系统中有着非常重要的地位,因此要求其具备高可用性是无可厚非的.有很多解决方案能实现不同的SLA(服务水平协定),这些方案可以保证数据库服务器在硬件或 ...

  4. Linux程序包管理之yum及源代码安装

    第十六章.Linux程序包管理之yum及源代码安装 目录 yum介绍 yum配置文件 yum的repo配置文件中可用的变量 yum命令的使用 使用光盘作为本地yum仓库 如何创建yum仓库 编译安装的 ...

  5. [译]Thinking in React

    编者按 使用React的思想来构建应用对我在实际项目中以及帮助他人解决实际问题时起到了很大作用,所以我翻译此文来向那些正在或即将陷入React或React-Native深坑的同胞们表示慰问.网上已经有 ...

  6. Windows 7 在资源管理器中显示软件快捷方式

    该方法是利用资源管理器中储存网络位置的文件夹实现的, 不需要修改注册表. 效果如图: 操作方法: 在资源管理器中打开路径 "%appdata%\Microsoft\Windows\Netwo ...

  7. 【repost】浏览器内核、渲染引擎、js引擎

    [1]定义 浏览器内核分成两部分渲染引擎和js引擎,由于js引擎越来越独立,内核就倾向于只指渲染引擎 渲染引擎是一种对HTML文档进行解析并将其显示在页面上的工具[2]常见引擎 渲染引擎: firef ...

  8. Centos 7 minimal install 无网络无ifconfig的解决

    Centos7这个比较不厚道, minimal install下居然不带net-tools 先要连上网络 修改/etc/sysconfig/network-scripts/ifcfg-ens12312 ...

  9. JavaScript子窗口调用父窗口变量和函数的方法

    在做一个父窗口开启子窗口并且在子窗口关闭的时候调用父窗口的方法,达到局部刷新的目的. 父窗口: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 ...

  10. [LeetCode] Read N Characters Given Read4 用Read4来读取N个字符

    The API: int read4(char *buf) reads 4 characters at a time from a file.The return value is the actua ...