1、构建一个MyTag.java的自定义标签类
package com.util;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
/**
* 自定义标签,去除文本中的html并截取字符
*
*
*/
public class MyTag extends TagSupport
{
/**
*
*/
private static final long serialVersionUID = 1L;
/**包含html的内容*/
private String htmlContent;
/**截取长度*/
private int len;
/**是否截取*/
boolean isCut;
@Override
public int doEndTag() throws JspException
{
// JspWriter out = this.pageContext.getOut();
return super.doEndTag();
}
/**
* 处理字串并输出到前台页面
*/
@Override
public int doStartTag() throws JspException
{
JspWriter out = this.pageContext.getOut();
try
{
out.print(processString());
}
catch (IOException e)
{
e.printStackTrace();
throw new JspException(e.getMessage());
}
return super.doEndTag();
}
public static String HtmlToText(String inputString)
{
String htmlStr = inputString; // 含html标签的字符串
String textStr = "";
Pattern p_script = null;
Matcher m_script = null;
Pattern p_style = null;
Matcher m_style = null;
Pattern p_html = null;
Matcher m_html = null;
Pattern p_houhtml = null;
Matcher m_houhtml = null;
Pattern p_spe = null;
Matcher m_spe = null;
Pattern p_blank = null;
Matcher m_blank = null;
Pattern p_table = null;
Matcher m_table = null;
Pattern p_enter = null;
Matcher m_enter = null;
try
{
String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>";
// 定义script的正则表达式.
String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>";
// 定义style的正则表达式.
String regEx_html = "<[^>]+>";
// 定义HTML标签的正则表达式
String regEx_houhtml = "/[^>]+>";
// 定义HTML标签的正则表达式
String regEx_spe = "\\&[^;]+;";
// 定义特殊符号的正则表达式
String regEx_blank = " +";
// 定义多个空格的正则表达式
String regEx_table = "\t+";
// 定义多个制表符的正则表达式
String regEx_enter = "\n+";
// 定义多个回车的正则表达式
p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
m_script = p_script.matcher(htmlStr);
htmlStr = m_script.replaceAll(""); // 过滤script标签
p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
m_style = p_style.matcher(htmlStr);
htmlStr = m_style.replaceAll(""); // 过滤style标签
p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
m_html = p_html.matcher(htmlStr);
htmlStr = m_html.replaceAll(""); // 过滤html标签
p_houhtml = Pattern.compile(regEx_houhtml, Pattern.CASE_INSENSITIVE);
m_houhtml = p_houhtml.matcher(htmlStr);
htmlStr = m_houhtml.replaceAll(""); // 过滤html标签
p_spe = Pattern.compile(regEx_spe, Pattern.CASE_INSENSITIVE);
m_spe = p_spe.matcher(htmlStr);
htmlStr = m_spe.replaceAll(""); // 过滤特殊符号
p_blank = Pattern.compile(regEx_blank, Pattern.CASE_INSENSITIVE);
m_blank = p_blank.matcher(htmlStr);
htmlStr = m_blank.replaceAll(" "); // 过滤过多的空格
p_table = Pattern.compile(regEx_table, Pattern.CASE_INSENSITIVE);
m_table = p_table.matcher(htmlStr);
htmlStr = m_table.replaceAll(" "); // 过滤过多的制表符
p_enter = Pattern.compile(regEx_enter, Pattern.CASE_INSENSITIVE);
m_enter = p_enter.matcher(htmlStr);
htmlStr = m_enter.replaceAll(" "); // 过滤过多的制表符
textStr = htmlStr;
}
catch (Exception e)
{
System.err.println("Html2Text: " + e.getMessage());
}
return textStr;// 返回文本字符串
}
private String processString()
{
if ( htmlContent == null || htmlContent.length() == 0){
return htmlContent;
}
String resultStr = HtmlToText(htmlContent);
resultStr = resultStr.trim();
if (resultStr.length() <= len || len == 0 ){
return resultStr;
}
if(isCut){
resultStr = resultStr.substring(0, len);
}
return resultStr;
}
/**
* @return the htmlContent
*/
public String getHtmlContent()
{
return htmlContent;
}
/**
* @param htmlContent the htmlContent to set
*/
public void setHtmlContent(String htmlContent)
{
this.htmlContent = htmlContent;
}
/**
* @return the len
*/
public int getLen()
{
return len;
}
/**
* @param len the len to set
*/
public void setLen(int len)
{
this.len = len;
}
/**
* @return the isCut
*/
public boolean getIsCut()
{
return isCut;
}
/**
* @param isCut the isCut to set
*/
public void setIsCut(boolean isCut)
{
this.isCut = isCut;
}
}
2、编写自定义标签my-taglib.tlb
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.2</tlibversion>
<jspversion>1.1</jspversion>
<shortname>myLib</shortname>
<uri>http://struts.apache.org/tags-html</uri>
<tag>
<name>htmlToText</name> <!-- 标签对应的名称 -->
<tagclass>com.util.MyTag</tagclass> <!-- 标签对应的类 -->
<bodycontent>empty</bodycontent>
<attribute>
<name>htmlContent</name> <!-- MyTag类中的htmlContent属性 -->
<required>true</required> <!-- 是否必须项 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>len</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>isCut</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>
3、编写jsp页面并使用标签
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/WEB-INF/tld/c-rt.tld" prefix="c" %>
<%@ taglib uri="/WEB-INF/tld/my-taglib.tld" prefix="mt" %>
<%
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>
<title>自定义标签测试</title>
</head>
<body>
<%--htmlContent:${htmlSegment },来自后台的一些html片段,通过EL表达式输出;isCut:true,是否截取字符;len:30,截取30个字符 --%>
<mt:htmlToText htmlContent="${htmlSegment }" isCut="true" len="30"/>
</body>
</html>