jsp自定义有参数标签

java代码:

package com.zx.tag;

import java.io.IOException;
import java.io.StringWriter;

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

public class ToUppserCaseTag extends SimpleTagSupport{
	@Override
	public void doTag() throws JspException, IOException {
		//1.获取到标签体(JspFragment对象)
		JspFragment jf = this.getJspBody();
		//2.使用StringWriter输出流,将jspFragment对象放入这个输出流中
		StringWriter sw = new StringWriter();
		//将jspFragment标签体对象封装(写入)输出流
		jf.invoke(sw);
		//3.从输出流读取StringBuffer
		String str = sw.getBuffer().toString();
		//4.小写变大写
		str = str.toUpperCase();
		//5.输出到页面
		this.getJspContext().getOut().write(str);
	}
}

tld代码:

<?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>自定义标签</description>
	<display-name>zdytag</display-name>
	<tlib-version>1.0</tlib-version>
	<!-- zy:ip -->
	<short-name>zy</short-name>
	<uri>http://www.zhuoxun/jxb</uri>
	<tag>
		<description>这个标签用来将标签体的内容小写改大写</description>
    	<name>toUpperCase</name>
    	<tag-class>com.zx.tag.ToUppserCaseTag</tag-class>
    	<body-content>tagdependent</body-content>
    	<!-- 
    		1.empty:表示无标签体
    		2.scriptless:不支jsp代码片段,表达式
    		3.tagdependent:支持jsp代码片段,表达式,el表达式(可以进行解析运算)
    		4.JSP:在jsp2.0版本之前有效,现以弃用,接受所有JSP语法
    	 -->
	</tag>
</taglib>

html代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.zhuoxun/jxb" prefix="zy"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>main</title>
</head>
<body>
	<!-- 被zy:toUpperCase标签包裹的标签体,全部转为大写 -->
	<!-- (有标签体无参有返回值) -->
	<zy:toUpperCase>aifjejAODFLAKFFlkgSDog</zy:toUpperCase>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值