什么也不说,直接上代码:
import java.io.IOException; import java.io.StringWriter; import javax.servlet.jsp.JspException; import javax.servlet.jsp.PageContext; import javax.servlet.jsp.tagext.JspFragment; import javax.servlet.jsp.tagext.SimpleTagSupport; //用简单标签修改标签体内容小写变大写 public class SimpleTagDemo3 extends SimpleTagSupport { @Override public void doTag() throws JspException, IOException { JspFragment jf = this.getJspBody(); StringWriter sw = new StringWriter(); jf.invoke(sw); String content = sw.getBuffer().toString(); content = content.toUpperCase(); PageContext pageContent = (PageContext) this.getJspContext(); pageContent.getOut().write(content); } }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="/simpleitcast" prefix="itcast"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>修改标签体</title> </head> <body> <itcast:demo3> xxx </itcast:demo3> </body> </html>
<tag> <name>demo3</name> <!-- 为标签处理器类配一个标签名 --> <tag-class>cn.itcast.web.simpletag.SimpleTagDemo3</tag-class> <body-content>scriptless</body-content> </tag>