关闭

菜鸟学Android笔记(四十七):自定义标签开发案例及打包

158人阅读 评论(0) 收藏 举报
分类:

案例1:开发if标签

先建立一个IFTag.java

package com.Test;

import java.io.IOException;

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

public class IFTag extends SimpleTagSupport {
    
	private boolean test;
	
	public void doTag() throws JspException, IOException {
		
		if(test){
			//this.getJspBody是为了获取标签体的JspFragment对象传递给标签处理器对象
			//invoke()里面的参数是一个输出流,如果写成为空,那么将标签体的内容默认输出到浏览器显示
			this.getJspBody().invoke(null);
		}	
	}

	public void setTest(boolean test) {
		this.test = test;
	}
		
}
然后建立一个IFTag.tld文件配置上面的java文件

<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" 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">
 <tlib-version>1.0</tlib-version>
 <short-name>IFTag</short-name>
 <uri>http://www.cuixueyong.com/IFTag</uri>
 <tag>
  <description>if标签</description>
  <name>if</name>
  <tag-class>com.Test.IFTag</tag-class>
  <body-content>scriptless</body-content>
  <attribute>
   <name>test</name>
   <rtexprvalue>true</rtexprvalue>
   
  </attribute>
 </tag>
</taglib>
写一个IFTag.jsp来测试

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.cuixueyong.com/IFTag" prefix="kk" %> 
<%
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>
   
  </head>
  
  <body>
 	<kk:if test="true">打印出来</kk:if>
 	<kk:if test="false">打印不出来</kk:if>
  </body>
</html>
经过测试,在浏览器可以输出“打印出来”,而不能输出”打印不出来“



案例2:html文件转义

新建HTMLTag.java

package com.Test;

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

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

public class HTMLTag extends SimpleTagSupport {
	
	
	private String filter(String message) {
		 
		         if (message == null){
		             return (null);
		         }
		         char content[] = new char[message.length()];
		         message.getChars(0, message.length(), content, 0);
		         StringBuilder result = new StringBuilder(content.length + 50);
		         for (int i = 0; i < content.length; i++) {
		             switch (content[i]) {
		             case '<':
		                 result.append("<");
		                 break;
		             case '>':
		                 result.append(">");
		                 break;
		             case '&':
		                 result.append("&");
		                 break;
		             case '"':
		                 result.append(""");
		                 break;
		             default:
		                 result.append(content[i]);
		             }
		         }
		         return (result.toString());
		     }

	@Override
	
	public void doTag() throws JspException, IOException {
		//获取一个字符串输出流,作用是保持标签体的内容
		StringWriter sw=new StringWriter();
		//将标签体的内容先输出到StringWriter流中
		this.getJspBody().invoke(sw);
		//将StringWriter流中的内容变为字符串
		//getBuffer()是将StringWriter流变为缓冲区,然后在转变为字符串
		String str=sw.getBuffer().toString();
		//这个时候写一个方法,将字符串里面的html代码将其转义
		String str1=filter(str);
		//返回页面的JspContext对象,然后得到它的输出流,在将字符串写入
		this.getJspContext().getOut().write(str1);
		
	}
	
	
}
在THML.tld配置文件

<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" 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">
 <tlib-version>1.0</tlib-version>
 <short-name>HTMLTag</short-name>
 <uri>http://www.cuixueyong.comHTMLTag</uri>
 <tag>
  <name>HTMLTag</name>
  <tag-class>com.Test.HTMLTag</tag-class>
  <body-content>scriptless</body-content>
 </tag>
</taglib>
然后在新建HTMLTag.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.cuixueyong.comHTMLTag"  prefix="jk"%>
<%
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 'HTMLTag.jsp' starting page</title>
   

  </head>
  
  <body>
  	<jk:HTMLTag><html>这是一个html的根元素</html></jk:HTMLTag>
  </body>
</html>
在浏览器中输出可得:<html>这是一个html的根元素</html>,将字符已经转义了



案例3:防盗链





0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:17897次
    • 积分:803
    • 等级:
    • 排名:千里之外
    • 原创:63篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章分类