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*|\t|\r|\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_html = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE);
m_html = p_html.matcher(htmlStr);
htmlStr = m_html.replaceAll(""); // 过滤空格回车标签 textStr = htmlStr.replaceAll(" ", ""); } 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?"...":""}
jsp 页面 摘要, 要截取字符串 ,当时 字符串中包含 html标签,截取后无法显示的更多相关文章
- PHP & Javascript 如何对字符串中包含html标签进行编码 整理
为什么要对字符串编码? 某些字符串中包含html标签,不编码,页面输出就乱了. PHP下怎么对字符串编码? htmlentities vs htmlspecialchars htmlentities ...
- jsp页面使用el 按key获取map中的对应值
jsp页面使用el 按key获取map中的对应值 转自:<jsp页面使用el 按key获取map中的对应值>地址:http://blog.csdn.net/baple/article/de ...
- 黑马基础阶段测试题:创建一个存储字符串的集合list,向list中添加以下字符串:”C++”、”Java”、” Python”、”大数据与云计算”。遍历集合,将长度小于5的字符串从集合中删除,删除成功后,打印集合中的所有元素
package com.swift; import java.util.ArrayList; import java.util.List; import java.util.ListIterator; ...
- C语言考题:输入一个字符串,将此字符串中特定的字符删去后, 显示新的字符串,要求用函数来完成删去字符的操作。
#include <stdio.h> #include <string.h> /*此题只需要删除单个字符,比较简单.相信大家也能做出来的.我这个也是可以实现的.只是加了两个判断 ...
- el: 在jsp页面内使用函数判断子字符串
e.g. <c:forEach items="${datas}" var="data"> <c:if test="${not fn: ...
- 将 jsp 页面的值 传到struts2 action中(不是表单中的值)
JSP: 页面: <%@ page language="java" pageEncoding="GBK"%> <%@taglib prefi ...
- 003杰信-在jsp页面输入数据,然后在oracle数据库中插入factory数据,当字段允许为空时要特殊处理
本博客的内容全部来自于传智播客,特在此说明. 业务要求如下:在jsp页面(jFactoryCreate.jsp)上输入数据时,转到后台,并输入到数据库. jFactoryCreate.jsp页面:
- 【HTML】处理<br>换行符追加到前端换行无效的问题 --- html中渲染的字符串中包含HTML标签无效的处理方法,字符串中包含HTML标签被转义的问题 解决
需求如下图: 追加给前台后,效果如下: 可以在源码看到: 是将后台给出来的数据,直接当作字符串给填充在了前台HTML中. 而查看浏览器编译后的HTML源码可以发现: 原来字符串中的<br> ...
- JSP学习笔记(七十八):struts2中s:select标签的使用
1.第一个例子: <s:select list="{'aa','bb','cc'}" theme="simple" headerKey="00& ...
随机推荐
- CentOS7.3下yum练手安装Nginx,支持php5.4
yum install php php-devel 安装的是5.4 那么安装完毕了,怎么设置nginx和php 解析 1 添加nginx 默认主页index.php vim .../etc/ngin ...
- eclipse模板
文件(Files)注释标签: /** * @Title: ${file_name} * @Package ${package_name} * @Description: ${todo}(用一句话描述该 ...
- Centos LVM 创建 删除 扩大 缩小
新建LVM的过程1.使用fdisk 新建分区 修改ID为8e3.使用 pvcreate 创建 PV 4.使用 vgcreate 创建 VG 5.使用 lvcreate 创建 LV 6.格式化LV7.挂 ...
- 机器学习入门-随机森林预测温度-不同参数对结果的影响调参 1.RandomedSearchCV(随机参数组的选择) 2.GridSearchCV(网格参数搜索) 3.pprint(顺序打印) 4.rf.get_params(获得当前的输入参数)
使用了RamdomedSearchCV迭代100次,从参数组里面选择出当前最佳的参数组合 在RamdomedSearchCV的基础上,使用GridSearchCV在上面最佳参数的周围选择一些合适的参数 ...
- 5.15 python 面向对象的软件开发&领域模型
1.面向对象的软件开发 参考地址::http://www.cnblogs.com/linhaifeng/articles/6182264.html#_label14 面向对象的软件工程包括下面几个部: ...
- node 的exports 和module
文件05/** * Created by Mr.tiankong on 2017/3/24. */var People = require("./test/people.js"); ...
- python 文件移动
python实现文件移动: import shutil shutil.move("original_path", "new_folder") # move fi ...
- nginx访问静态文件配置
通过nginx访问静态文件配置,均是在server模块中配置,有两种方式: 1.alias 通过alias关键字,重定义路径,如 server{ listen 7001; server ...
- 使用flash导出图集动画到unity
1.选中要导出的元件,元件所有动作要对齐,右键导出Sprite Sheet.. 2.设置如下 3.复制导出的png图片到unity,对图片进行网格裁剪,网格宽高在plist文件中:
- NISP视频知识点总结
身份认证访问控制安全审计本章实验 ===密码学=====古典密码 算法本身的保密性近代密码 机械密码\机电 密码打字密码机轮转机现代密码 基于密钥公钥密码 公钥==================对称 ...