【jsp】使用SimpleTagSupport自定义标签库

   使用springmvc作为开发环境。

1,概述

  自定义标签库是jsp api下的内容。通常一个自定义标签会包含3部分内容:标签处理类,tld标签描述文件,jsp文件(使用)。

    *标签处理类:继承自 SimpleTagSupport类即可,重写其doTag()方法。我们在页面写了一个自定义标签,这个标签的最终输出内容就是在doTag方法里面实现的,调用getJspContext得到JspContext,即这个jsp页面的上下文环境,然后再从上下文环境中得到jspwriter,就是用这个jspwriter来绘制最终显示的内容的。

    *tld描述文件:这个文件对应了一个标签库,即很多的标签。跟元素是taglib,taglib下会包含很多的tag子元素,用来描述该标签库下的各种标签,除此之外还有uri元素,很重要,本质是定义了这个标签库的名字。

    *要先指定标签库的uri和前缀,然后用前缀:标签名的方式来调用该库内的标签

  下面看例子

2,先定义最简单的标签,显示hello world:

package Tag;


import java.io.IOException;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class SimpleTag1 extends SimpleTagSupport{

	@Override
	public void doTag(){
		JspWriter out = getJspContext().getOut();
		try {
			out.write("hello world");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
只打印helloworld字符串。


<?xml version="1.0" encoding="GBK"?>
<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 web-jsptaglibrary_2_0.xsd"
    version="2.0">
    <tlib-version>1.0</tlib-version>
    <short-name>mytaglib</short-name>
    <!-- 定义该标签库的URI -->
    <uri>http://www.ly.org/mytaglib</uri>
    <!-- 定义第一个标签 -->
    <tag>
        <!-- 定义标签名 -->
        <name>helloWorld</name>
        <!-- 定义标签处理类 -->
        <tag-class>Tag.SimpleTag1</tag-class>
        <!-- 定义标签体为空 -->
        <body-content>empty</body-content>
    </tag>
</taglib>
body-content指的是标签内的内容,只能为empty或者scriptless两个属性,前者表明没有内容,后者表明有非jsp的内容,如el表达式,html等。


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://www.ly.org/mytaglib" prefix="ly" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h3>hello</h3>
<ly:helloWorld ></ly:helloWorld>
</body>
</html>
使用时,要指定标签库url和前缀。

最终:




3,带有属性的标签:

属性用来向后台的标签处理类传递参数。如果带了属性,那么在处理类中要加上属性,同时有getter setter,按照javabean命名规范。同时在tld文件中加入属性描述,即在tag元素下添加attribute元素。

package Tag;


import java.io.IOException;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class SimpleTag1 extends SimpleTagSupport{

	private String fontSize;
	
	@Override
	public void doTag(){
		JspWriter out = getJspContext().getOut();
		String str = "<h"+fontSize+">"+"hello world"+"</h"+fontSize+">";
		try {
			out.write(str);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String getFontSize() {
		return fontSize;
	}

	public void setFontSize(String fontSize) {
		this.fontSize = fontSize;
	}
}
把字号传来

<?xml version="1.0" encoding="GBK"?>
<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 web-jsptaglibrary_2_0.xsd"
    version="2.0">
    <tlib-version>1.0</tlib-version>
    <short-name>mytaglib</short-name>
    <!-- 定义该标签库的URI -->
    <uri>http://www.ly.org/mytaglib</uri>
    <!-- 定义第一个标签 -->
    <tag>
        <!-- 定义标签名 -->
        <name>helloWorld</name>
        <!-- 定义标签处理类 -->
        <tag-class>Tag.SimpleTag1</tag-class>
        <!-- 定义标签体为空 -->
        <body-content>empty</body-content>
        <attribute>
            <name>fontSize</name>
            <required>true</required>
        </attribute>
    </tag>
</taglib>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://www.ly.org/mytaglib" prefix="ly" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h3>hello</h3>
<ly:helloWorld fontSize="1"></ly:helloWorld>
<ly:helloWorld fontSize="3"></ly:helloWorld>
</body>
</html>
结果:

4,带有内容的标签:

标签的内容指的是尖括号之间的内容。如果有内容,那么要在tld中的对标签进行修改,即<body-content>不是emtpy而是scriptless。在处理类中,调用jspfragment

的invoke方法就会显示一次body的内容,调用两次就会显示两次,以此类推。当然如果是多次显示,那么会在标签之间重复显示,而不是多个标签。

<?xml version="1.0" encoding="GBK"?>
<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 web-jsptaglibrary_2_0.xsd"
    version="2.0">
    <tlib-version>1.0</tlib-version>
    <short-name>mytaglib</short-name>
    <!-- 定义该标签库的URI -->
    <uri>http://www.ly.org/mytaglib</uri>
    <!-- 定义第一个标签 -->
    <tag>
        <!-- 定义标签名 -->
        <name>helloWorld</name>
        <!-- 定义标签处理类 -->
        <tag-class>Tag.SimpleTag1</tag-class>
        <!-- 定义标签体为空 -->
        <body-content>scriptless</body-content>
        <attribute>
            <name>fontSize</name>
            <required>true</required>
        </attribute>
    </tag>
</taglib>


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://www.ly.org/mytaglib" prefix="ly" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h3>hello</h3>
<ly:helloWorld fontSize="1">gg</ly:helloWorld>
</body>
</html>

package Tag;


import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class SimpleTag1 extends SimpleTagSupport{

	private String fontSize;
	
	@Override
	public void doTag(){
		JspWriter out = getJspContext().getOut();
		String str = "<h"+fontSize+">"+"hello world"+"</h"+fontSize+">";
		try {
			out.write(str);
			getJspBody().invoke(null);
			getJspBody().invoke(null);
			getJspBody().invoke(null);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JspException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public String getFontSize() {
		return fontSize;
	}

	public void setFontSize(String fontSize) {
		this.fontSize = fontSize;
	}
}

调用三次invoke,显示了三个gg


  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值