[Java] JSP笔记 - 自定义标签
自定义标签的创建步骤:

自定义标签的四大功能:

自定义标签的类结构:

在 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笔记 - 自定义标签的更多相关文章
- [Java] JSP笔记 - EL、JSTL 常用标签
一. 什么是 EL 语言 表达式语言(EL)是 JSP 2.0 引入的一种计算和输出 Java 对象的简单语言. 二.EL 语言的作用 为了使JSP写起来更加简单.表达式语言的灵感来自于 ECMASc ...
- 【JSP】自定义标签开发入门
JSP 自定义标签 自定义标签是用户定义的JSP语言元素.当JSP页面包含一个自定义标签时将被转化为servlet,标签转化为对被 称为tag handler的对象的操作,即当servlet执行时We ...
- 12、Jsp加强/自定义标签/JavaBean
1 Jsp加强回顾 Jsp加强 1)Jsp的9大内置对象 request HttpServletRequet response HttpServletResponse config ...
- Jsp开发自定义标签,自定义标签将字符串转成指定的时间格式显示
本例以将 字符串格式的时间转成指定的时间格式显示. 第一步.定义一个标签处理程序类,需要集成javax.servlet.jsp.tagext.TagSupport,代码如下: import java. ...
- JavaWeb之 JSP:自定义标签
当jsp的内置标签和jstl标签库内的标签都满足不了需求,这时候就需要开发者自定义标签. 自定义标签 下面我们先来开发一个自定义标签,然后再说它的原理吧! 自定义标签的开发步骤 步骤一 编写一个普通的 ...
- JavaWeb之 JSP:自定义标签的创建和使用
当jsp的内置标签和jstl标签库内的标签都满足不了需求,这时候就需要开发者自定义标签. 下面我们先来开发一个自定义标签,然后再说它的原理吧! 自定义标签的开发步骤 步骤一 编写一个普通的java类, ...
- jsp的自定义标签
1.相对于JSTL或Spring等第三方标签库而言的,用来实现项目中特定的功能需求. 2.自定义标签基本的组成部分 ①页面上看得见的部分 [1]通过taglib引入标签库 [2]标签本身 ②xxx.t ...
- Servlet和JSP之自定义标签学习
此文章会讲述简单标签处理器,因为经典自定义标签处理器没有简单标签处理器方便使用,故在此不进行描述. 参考:慕课网的<JSP自定义标签>视频; <Servlet.JSP和Sprin ...
- Java之 jstl 自定义标签的方法
1.写一个Java类 我的路径是写再tag包中的一个 HelloTag类 package tag; import java.io.IOException; import javax.servlet.j ...
随机推荐
- [Erlang 0110] Erlang Abstract Format , Part 1
Erlang Abstract Format并不难懂,只是枯燥一点罢了,如果把Abstract Format的文档翻译出来,其实就是Erlang教科书中语法入门的部分. Erlang Abstract ...
- sql语句查询经纬度范围(转载,源链接失效)
MySQL性能调优 – 使用更为快速的算法进行距离 最近遇到了一个问题,通过不断的尝试最终将某句原本占据近1秒的查询优化到了0.01秒,效率提高了100倍. 问题是这样的,有一张存放用户居住地点经纬度 ...
- 64位下pwntools中dynELF函数的使用
这几天有同学问我在64位下怎么用这个函数,于是针对同一道题写了个利用dynELF的方法 编译好的程序 http://pan.baidu.com/s/1jImF95O 源码在后面 from pwn im ...
- Java动态代理
代理模式 代理模式是常用的java设计模式,他的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委托类,以及事后处理消息等.代理类与委托类之间通常会存在关联关 ...
- BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3336 Solved: 1936[Submit][ ...
- 学习Python函数笔记之二
---恢复内容开始--- 1.内置函数:取绝对值函数abs() 2.内置函数:取最大值max(),取最小值min() 3.内置函数:len()是获取序列的长度 4.内置函数:divmod(x,y),返 ...
- oracle add_months函数
oracle add_months函数 add_months 函数主要是对日期函数进行操作,举例子进行说明 add_months 有两个参数,第一个参数是日期,第二个参数是对日期进行加减的数字(以月为 ...
- 用vue.js学习es6(五):set和map的使用
一:Set用法: ES6提供了新的数据结构Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. (1).打印:console.log var data = new Set([1,2,3]); ...
- Go语言常用命令介绍
go build go build 命令主要是用于测试编译.在包的编译过程中,若有必要,会同时编译与之相关联的包. 如果是普通包,当你执行go build命令后,不会产生任何文件. 如果是main包, ...
- Linux用户管理(centos)
useradd testuser; 添加用户 testuser为用户名 passwd testuser; 修改用户密码 提示两次输入密码 赋予root权限 修改 /etc/sudoers 文件,找 ...