jsp 页面 摘要, 要截取字符串 ,当时 字符串中包含 html标签,截取后无法显示

如题: 

处理办法: 

1.  使用struts标签 

    <s:property  value ="#text.replaceAll('<[^>]+>','').substring(0,77)" escape="false"/> 

    过滤掉所有 标签

2. 使用 js  

  

   var div = document.getElementById('div');

  div.innerHTML = '<div>aaa<span>bbb<u>ccc<b>ddd<i>fff</i>';   //截取完后的值 

  alert(div.innerHTML);  //<div>aaa<span>bbb<u>ccc<b>ddd<i>fff</i></b></u></span></div>

  dom对象的 innerHTML 有自动补全 标签的功能

3. 使用自定义标签 过滤掉 html 

  

package org.uintec.util;

import java.util.regex.Pattern;

import org.apache.struts2.views.jsp.PropertyTag;

/**
 * 自定义 jstl 标签 
 * @author zc
 *
 */

public class CustomDefinedJstl {
    
    /**
     * 过滤html标签
     * 包含 script style html
     */
    public static String filterHtmlTags(String inputString) {
        String htmlStr = inputString; // 含html标签的字符串
        String textStr = "";
        java.util.regex.Pattern p_script;
        java.util.regex.Matcher m_script;
        java.util.regex.Pattern p_style;
        java.util.regex.Matcher m_style;
        java.util.regex.Pattern p_html;
        java.util.regex.Matcher m_html;
        if(null == htmlStr || "".equals(htmlStr)){
            return "";
        }else{
            try {
                // 定义script的正则表达式{或<script[^>]*?>[\s\S]*?<\/script>
                String regEx_script = "<[\s]*?script[^>]*?>[\s\S]*?<[\s]*?\/[\s]*?script[\s]*?>"; 
                
                // 定义style的正则表达式{或<style[^>]*?>[\s\S]*?<\/style>
                String regEx_style = "<[\s]*?style[^>]*?>[\s\S]*?<[\s]*?\/[\s]*?style[\s]*?>"; 
                
                String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
                String regEx_space = "\s*|	|
|
";//定义空格回车换行符
                
                
                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_html = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE);  
                m_html = p_html.matcher(htmlStr);  
                htmlStr = m_html.replaceAll(""); // 过滤空格回车标签  
                
                textStr = htmlStr.replaceAll("&nbsp;", "");

            } catch (Exception e) {
                System.err.println("Html2Text: " + e.getMessage());
            }
            return textStr.trim();// 返回文本字符串
        }
        
    }
    
    
}
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
    <description>自定义jstl</description>
    <display-name>custom defined jstl functions</display-name>
    <tlib-version>1.0</tlib-version>
    <short-name>cdj</short-name>
    <uri>/custom-defined-jstl</uri>

    <function>
        <name>filterHtmlTags</name>
        <function-class>org.uintec.util.CustomDefinedJstl</function-class>
        <function-signature>java.lang.String filterHtmlTags(java.lang.String)
        </function-signature>
        <example>${cdj:filterHtmlTags('')}</example>
    </function>
       
</taglib>
放在 web-inf 下的 taglib 文件夹下
jsp 页面调用
${fn:substring(cdj:filterHtmlTags(ex.explain),0,10)}${fn:length(ex.explain)>10?"...":""}
原文地址:https://www.cnblogs.com/zhangchenglzhao/p/4271002.html