jsp自动标签实现字母转换


jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.cgmcc.com/jsp/containjspbody" prefix="my4"
%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'tagbodycontent.jsp' starting page</title>
   
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">   
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
  </head>
  <body>
     
      
      <my4:toUpperCase>yyyyyyBBBBgggggDDDDDD</my4:toUpperCase>
  </body>
</html>




java
package com.cgm.supportbdcontent;

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

import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.JspTag;
import javax.servlet.jsp.tagext.SimpleTag;

public class SupportUpCaseBodyConten implements SimpleTag{
private  PageContext  pageContext;

private JspFragment jspBody;


public JspFragment getJspBody() {
return jspBody;
}

public PageContext getPageContext() {
return pageContext;
}

public void setPageContext(PageContext pageContext) {
this.pageContext = pageContext;
}

public void doTag() throws JspException, IOException {
System.out.println(jspBody);
StringWriter  stringWriter=new StringWriter();
    jspBody.invoke(stringWriter);  //把jspBody的内容他写入流中

String data=stringWriter.toString();
pageContext.getOut().print(data.toUpperCase());


JspWriter jspWriter=pageContext.getOut();
this.jspBody.invoke(jspWriter);

//this.jspBody.invoke(null);  //如果设置为null直接写出去...


}

public JspTag getParent() {
// TODO Auto-generated method stub
return null;
}

public void setJspBody(JspFragment jspBody) {


//<my4:toUpperCase>yyyyyyBBBBgggggDDDDDD</my4:toUpperCase>
  this.jspBody=jspBody;

}

public void setJspContext(JspContext pc) {

this.pageContext=(PageContext) pc;
}

public void setParent(JspTag parent) {


}

}







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">
    <tlib-version>1.1</tlib-version>
    <short-name>my4</short-name>
    <uri>http://www.cgmcc.com/jsp/containjspbody</uri>
   
         <!-- 
    <my4:toUpperCase>yyyyyyBBBBgggggDDDDDD</my4:toUpperCase>
    -->
   
    <tag>
          <name>toUpperCase</name>
        <tag-class>com.cgm.supportbdcontent.SupportUpCaseBodyConten</tag-class>
      
         <!-- 支持jsp所有语法再tagzhong使用 -->
       <body-content>scriptless</body-content>
   
    </tag>
   
   
</taglib>  

你可能感兴趣的:(jsp)