Python语言技术文档

微信小程序技术文档

php语言技术文档

jsp语言技术文档

asp语言技术文档

C#/.NET语言技术文档

html5/css技术文档

javascript

点击排行

您现在的位置:首页 > 技术文档 > jsp入门教程

jsp基础教程:自定义标签案例分析

来源:中文源码网    浏览:293 次    日期:2024-04-19 04:29:55
【下载文档:  jsp基础教程:自定义标签案例分析.txt 】


JSP自定义标签案例分析
本文为大家介绍了JSP自定义标签的案例,供大家参考,具体内容如下
案例一:实现一个基本防盗链标签
1. 标签处理类
public class MyReferer extends BodyTagSupport {
private String site;
private String back;
public String getSite() {
return site;
}
public void setSite(String site) {
this.site = site;
}
public String getBack() {
return back;
}
public void setBack(String back) {
this.back = back;
}
public int doEndTag() throws JspException {
// 获取JSP上下文环境对象
PageContext pageContext = this.pageContext;
// 获取到request对象
HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
// 判断
String header = request.getHeader("referer");
if(header != null && header.startsWith(getSite())){
// 执行后续的页面
return Tag.EVAL_PAGE;
}else{
// 页面的重定向
HttpServletResponse response = (HttpServletResponse)pageContext.getResponse();
try {
response.sendRedirect(getBack());
} catch (IOException e) {
e.printStackTrace();
}
// 不执行
return Tag.SKIP_PAGE;
}
}
}
2. 描述文件

xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">

1.0
jnb

referer
cn.itcast.custom.MyReferer
empty

site
true
true


back
true
true



3. 引入和使用
<%@taglib uri="/WEB-INF/referer.tld" prefix="my"%>
back="/day11/list.jsp"/>
JSP2.0自定义标签
---| SimpleTag 接口
定义了标签处理类的生命周期方法。doTag()
-----| SimpleTagSupport 类
全部实现了SimpleTag接口的方法,因此后面我们只需要继承并重写该类即可。
案例二:实现自己的if….else标签
目标:
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>


大于


小于


分析:
1. ChooseTag.java,必须定义一个标记字段属性
public class ChooseTag extends SimpleTagSupport {
private boolean tag = true;
public boolean isTag() {
return tag;
}
public void setTag(boolean tag) {
this.tag = tag;
}
// 遇到标签自动执行
public void doTag() throws JspException, IOException {
// 获取标签体对象
JspFragment body = this.getJspBody();
// 执行标签体
body.invoke(null);
super.doTag();
}
}
2. WhenTag.java
public class WhenTag extends SimpleTagSupport {
private boolean test;
public boolean isTest() {
return test;
}
public void setTest(boolean test) {
this.test = test;
}
// 遇到标签自动执行
public void doTag() throws JspException, IOException {
// 获取父元素
ChooseTag choose = (ChooseTag)this.getParent();
// 获取父元素的标记变量值
boolean parent = choose.isTag();
// 判断
if( parent && this.isTest() ){
// 执行标签体
JspFragment body = this.getJspBody();
body.invoke(null);
}
super.doTag();
}
}
3. Otherwise.java
public class OtherwiseTag extends SimpleTagSupport {
// 遇到标签自动执行
public void doTag() throws JspException, IOException {
// 获取父元素
ChooseTag choose = (ChooseTag)this.getParent();
// 获取父元素的标记变量值
boolean parent = choose.isTag();
// 判断
if(parent){
// 执行标签体
JspFragment body = this.getJspBody();
body.invoke(null);
}
super.doTag();
}
}
4. 描述文件

xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">

1.0
jnb

choose
cn.itcast.tags.ChooseTag
scriptless  JSP2.0方式


when
cn.itcast.tags.WhenTag
scriptless

test
true
true



otherwise
cn.itcast.tags.OtherwiseTag
scriptless


5. 引入和使用
<%@taglib uri="/WEB-INF/ifelse.tld" prefix="jnb"%>


小于


大于


打包自定义标签库
1. 建立一个taglibs文件夹
2. 将所有的标签处理类对应的class文件连同包拷贝到1中的目录中
3. 在1中的文件夹中建立一个META-INF文件夹
4. 将tld文件拷贝到META-INF目录
5. 编辑tld文件引入uri元素:http://www.jnb.com à提供引入的url路径
6. 使用jar命令进行打包:D:\mytaglibs>jar cvf jnb.jar *
总结
主要掌握如何使用JSP2.0进行自定义标签的开发和打包。
1. 建立一个taglibs文件夹
2. 将所有的标签处理类对应的class文件连同包拷贝到1中的目录中
3. 在1中的文件夹中建立一个META-INF文件夹
4. 将tld文件拷贝到META-INF目录
5. 编辑tld文件引入uri元素:http://www.jnb.com à提供引入的url路径
6. 使用jar命令进行打包:D:\mytaglibs>jar cvf jnb.jar *
总结
主要掌握如何使用JSP2.0进行自定义标签的开发和打包。

相关内容