• 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?"...":""}
  • 相关阅读:
    jqGrid 属性、事件全集
    把app(apk和ipa文件)安装包放在服务器上供用户下方法
    c#中如何获取本机MAC地址、IP地址、硬盘ID、CPU序列号等系统信息
    Visual Studio Installer打包安装项目VS2015
    小白入门服务器压测
    PHP正则表达式快速查找
    PHP创建创建资源流上下文实现携带cookie访问
    js中文转Unicode编码与解码
    PHP的fpm配置学习笔记
    微信公众号PHP生成二维码海报的几个小扩展
  • 原文地址:https://www.cnblogs.com/zhangchenglzhao/p/4271002.html
Copyright © 2020-2023  润新知