JSP2.0自定义标签
2020-12-13 05:25
标签:style class blog c code java ---| SimpleTag 接口 定义了标签处理类的生命周期方法。doTag() -----| SimpleTagSupport 类 全部实现了SimpleTag接口的方法,因此后面我们只需要继承并重写该类即可。 目标: 分析: 1. ChooseTag.java,必须定义一个标记字段属性 2. WhenTag.java 3. Otherwise.java 4. 描述文件 5. 引入和使用 如果提早在tld文件中写入uri
然后在*.jsp中引入www.jnb.com会报错,uri 用于打包的时候用 6.使用jar命令进行打包 D:\mytaglibs>jar cvf jnb.jar * JSP2.0自定义标签,搜素材,soscw.com JSP2.0自定义标签 标签:style class blog c code java 原文地址:http://www.cnblogs.com/friends-wf/p/3736670.html实现自己的if….else标签
1 %@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
2
3 c:choose>
4
5 c:when test="1 %>">
6
7 大于
8
9 c:when>
10
11 c:otherwise>
12
13 小于
14
15 c:otherwise>
16
17 c:choose>
1 public class ChooseTag extends SimpleTagSupport {
2 private boolean tag = true;
3 public boolean isTag() {
4 return tag;
5 }
6 public void setTag(boolean tag) {
7 this.tag = tag;
8 }
9 // 遇到标签自动执行
10 public void doTag() throws JspException, IOException {
11 // 获取标签体对象
12 JspFragment body = this.getJspBody();
13 // 执行标签体
14 body.invoke(null);
15 super.doTag();
16 }
17 }
1 public class WhenTag extends SimpleTagSupport {
2 private boolean test;
3 public boolean isTest() {
4 return test;
5 }
6 public void setTest(boolean test) {
7 this.test = test;
8 }
9 // 遇到标签自动执行
10 public void doTag() throws JspException, IOException {
11 // 获取父元素
12 ChooseTag choose = (ChooseTag)this.getParent();
13 // 获取父元素的标记变量值
14 boolean parent = choose.isTag();
15 // 判断
16 if( parent && this.isTest() ){
17 // 设置父元素的标记变量
18 choose.setTag(false);
19 // 执行标签体
20 JspFragment body = this.getJspBody();
21 body.invoke(null);
22 }
23 super.doTag();
24 }
25 }
1 public class OtherwiseTag extends SimpleTagSupport {
2
3 // 遇到标签自动执行
4 public void doTag() throws JspException, IOException {
5 // 获取父元素
6 ChooseTag choose = (ChooseTag)this.getParent();
7 // 获取父元素的标记变量值
8 boolean parent = choose.isTag();
9 // 判断
10 if(parent){
11 // 执行标签体
12 JspFragment body = this.getJspBody();
13 body.invoke(null);
14 }
15 super.doTag();
16 }
17 }
1 xml version="1.0" encoding="UTF-8"?>
2 taglib
3 xmlns="http://java.sun.com/xml/ns/javaee"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
6 version="2.1">
7
8 tlib-version>1.0tlib-version>
9 short-name>jnbshort-name>
10 tag>
11 name>choosename>
12 tag-class>cn.itcast.tags.ChooseTagtag-class>
13 body-content>scriptlessbody-content> ? JSP2.0方式
14 tag>
15 tag>
16 name>whenname>
17 tag-class>cn.itcast.tags.WhenTagtag-class>
18 body-content>scriptlessbody-content>
19 attribute>
20 name>testname>
21 required>truerequired>
22 rtexprvalue>truertexprvalue>
23 attribute>
24 tag>
25
26 tag>
27 name>otherwisename>
28 tag-class>cn.itcast.tags.OtherwiseTagtag-class>
29 body-content>scriptlessbody-content>
30 tag>
31 taglib>
1 %@taglib uri="/WEB-INF/ifelse.tld" prefix="jnb"%>
2 jnb:choose>
3 jnb:when test="2 %>">
4 小于
5 jnb:when>
6 jnb:otherwise>
7 大于
8 jnb:otherwise>
9 jnb:choose>
打包自定义标签库