什么也不说,直接上代码:
package cn.itcast.web.tag; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.BodyTag; import javax.servlet.jsp.tagext.BodyTagSupport; import javax.servlet.jsp.tagext.Tag; //修改标签体 public class TagDemo4 extends BodyTagSupport { //bodycontent @Override public int doStartTag() throws JspException { return BodyTag.EVAL_BODY_BUFFERED; } @Override public int doEndTag() throws JspException { //拿到标签体 String content = this.getBodyContent().getString(); String result = content.toUpperCase(); try { this.pageContext.getOut().write(result); } catch (IOException e) { throw new RuntimeException(e); } return Tag.EVAL_PAGE; } }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="/itcast" prefix="itcast" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP '5.jsp' starting page</title> </head> <body> <itcast:demo4> xxx </itcast:demo4> </body> </html>
<tag> <name>demo4</name> <tag-class>cn.itcast.web.tag.TagDemo4</tag-class> <body-content>JSP</body-content> </tag>