5.把标签体内容改为大写(tld中的配置我就省略了,详细请看jsp自定义标签1)
import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.BodyContent; import javax.servlet.jsp.tagext.BodyTag; import javax.servlet.jsp.tagext.BodyTagSupport; import javax.servlet.jsp.tagext.Tag; /** * @author: snowing * @date : 2017年4月18日 * 把标签体改为大写 */ public class TagDemo5 extends BodyTagSupport { @Override public int doStartTag() throws JspException { return BodyTag.EVAL_BODY_BUFFERED;//把标签体作为对象传递,通过setBodyContent传递给标签处理器类 } @Override public int doEndTag() throws JspException { BodyContent bc=this.getBodyContent();//得到标签体对象 String content=bc.getString(); content=content.toUpperCase();//变成大写 try { this.pageContext.getOut().write(content); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return Tag.EVAL_PAGE;//自定义标签结束,显示整个页面 } }
TagUpperCase.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="snowing.com" prefix="snowing"%> <%@page import="javax.servlet.jsp.tagext.Tag" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <snowing:UPPERCASETAG>aaaa</snowing:UPPERCASETAG> </body> </html>
结果: