[Java] JSP笔记 - 自定义标签
Posted 爱音乐的孩子是小白
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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笔记 - 自定义标签的主要内容,如果未能解决你的问题,请参考以下文章