后台的数据库中某个字段是富文本框输入的 带有Html的标签 ,去掉标签后返回给前台

1.去掉Html 标签的代码

  //过滤html标签
Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
Matcher m_html = p_html.matcher(htmlStr);
htmlStr = m_html.replaceAll("");

2.项目中使用:

@RequestMapping(value = "/details", method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value = "详情页面", httpMethod = "GET", produces = "application/json")
public AjaxResult details(@ApiParam(required = true, name="id", value="id") @RequestParam(required = true,value = "id")String id) {
//定义HTML标签的正则表达式
String regEx_html = "<[^>]+>";
Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
if (cn.cmodes.common.utils.StringUtils.isBlank(id)) {
AjaxResult.error("id不能为空!");
}
Articleinformation articleinformation = articleinformationService.selectArticleinformationById(id); if (articleinformation == null) {
Bookinformation bookinformation = bookinformationService.selectBookinformationById(id); if (bookinformation == null) {
Mediaphoto mediaphoto = mediaphotoService.selectMediaphotoById(id); if (mediaphoto == null) {
Archaeological archaeological = archaeologicalService.selectArchaeologicalById(id); if (archaeological == null) {
Scholar scholar = scholarService.selectScholarById(id);
if (scholar == null) {
Institution institution = institutionService.selectInstitutionById(id);
if (institution != null) {
return AjaxResult.success().put("result",institution);
} else {
return AjaxResult.error();
}
} else {
return AjaxResult.success().put("result",scholar);
}
} else {
// archaeological.setContents(HtmlUtils.htmlUnescape(archaeological.getContents())); Matcher m_html = p_html.matcher(archaeological.getContents());
archaeological.setContents(m_html.replaceAll(""));
return AjaxResult.success().put("result",archaeological);
}
} else {
// mediaphoto.setSunmmaryContents(HtmlUtils.htmlUnescape(mediaphoto.getSunmmaryContents()));
Matcher m_html = p_html.matcher(mediaphoto.getSunmmaryContents());
mediaphoto.setSunmmaryContents(m_html.replaceAll(""));
return AjaxResult.success().put("result",mediaphoto);
}
} else {
// bookinformation.setContent(HtmlUtils.htmlUnescape(bookinformation.getContent()));
Matcher m_html = p_html.matcher(bookinformation.getContent());
bookinformation.setContent(m_html.replaceAll(""));
return AjaxResult.success().put("result",bookinformation);
}
} else {
// articleinformation.setSummaryContents(HtmlUtils.htmlUnescape(articleinformation.getSummaryContents()));
Matcher m_html = p_html.matcher(articleinformation.getSummaryContents());
articleinformation.setSummaryContents(m_html.replaceAll(""));
return AjaxResult.success().put("result",articleinformation);
}
}

3.去掉后的数据: 就是不带html 标签的数据

数据库中数据:
<p>xvcb<span style="text-decoration: underline; font-size: 24px;"><em><strong>cxvbxcbxc<span style="text-decoration: underline; font-size: 24px; font-family: impact, chicago;">bvcbxdsfsdf s</span>fsdgdsfgsdgfds</strong></em></span></p><p><span style="text-decoration: underline; font-size: 24px; background-color: rgb(255, 255, 0);"><em><strong>dfsfsgdfgsdf<span style="text-decoration: underline; font-size: 24px; background-color: rgb(255, 255, 0); font-family: impact, chicago;"></span></strong></em></span></p> 去掉后的数据:
"xvcbcxvbxcbxcbvcbxdsfsdf sfsdgdsfgsdgfdsdfsfsgdfgsdf"

4. perfect

[Java]去除html中的标签或者元素属性(正则表达式)的更多相关文章

  1. JAVA 去除实体中类型为string的属性值中的空格

    前端传入的参数实体中,有时候会出现传入了一空格,导致操作失败,这时就可以利用java反射机制去除实体中类型为sting的属性值中的空格. java代码示例: package com.spyang.ut ...

  2. java-去除html中的标签或者元素属性(正则表达式/jsoup)

    业务场景: 如一篇使用富文本编辑器编辑的新闻稿,需要在列表页面截取前200字作为摘要,此时需要去除html标签,截取真正的文本部分. /** * 删除Html标签 */public static St ...

  3. [转]java去除List中重复的元素

    java去除List中重复的元素 如果用Set ,倘若list里边的元素不是基本数据类型而是对象, 那么请覆写Object的boolean   equals(Object   obj)   和int  ...

  4. js去除字符串中的标签

    var str="<p>js去除字符串中的标签</p>"; var result=str.replace(/<.*?>/ig,"&qu ...

  5. java 从List中随机取出一个元素

    java 从List中随机取出一个元素 List<Integer> list = new ArrayList<>(); Random random = new Random() ...

  6. 转.HTML中img标签的src属性绝对路径问题解决办法,完全解决!

    HTML中img标签的src属性绝对路径问题解决办法,完全解决   需求:有时候自己的项目img的src路径需要用到本地某文件夹下的图片,而不是直接使用项目根目录下的图片. 场景:eclipse,to ...

  7. .NET获取Html字符串中指定标签的指定属性的值

    using System.Text; using System.Text.RegularExpressions; //以上为要用到的命名空间 /// <summary> /// 获取Htm ...

  8. java去除数组中重复的元素方法总结

    /* * ArrayUnique.java * Version 1.0.0 * Created on 2017年12月16日 * Copyright ReYo.Cn */ package reyo.s ...

  9. Java jsp页面中jstl标签详解

    JSLT标签库,是日常开发经常使用的,也是众多标签中性能最好的.把常用的内容,放在这里备份一份,随用随查.尽量做到不用查,就可以随手就可以写出来.这算是Java程序员的基本功吧,一定要扎实. JSTL ...

随机推荐

  1. java的集合框架详解

    前言:数据结构对程序设计有着深远的影响,在面向过程的C语言中,数据库结构用struct来描述,而在面向对象的编程中,数据结构是用类来描述的,并且包含有对该数据结构操作的方法. 在Java语言中,Jav ...

  2. libpcap编程实例

    #include <stdio.h> #include <stdlib.h> #include <pcap.h> #include <errno.h> ...

  3. Spring.net init-method destroy-method

    <object id="exampleInitObject" type="Examples.ExampleObject" init-method=&quo ...

  4. servlet介绍

    1.首先说Servlet API:servlet的命名:server+applet Servlet的框架是由两个Java包组成的:javax.servlet与javax.servlet.http. 在 ...

  5. Eclipse Failed to get the required ADT version number from SDK

    之前本人的AndroidStudio和Eclipse使用的是同一个SDK,然后使用AndroidStudio的 SDK Manager对SDK进行了update,再打开Eclipse就报了“Eclip ...

  6. charCodeAt方法以及Unicode中文汉字编码范围

    js的charCodeAt() 方法可返回指定位置的字符的 Unicode 编码.这个返回值是 0 - 65535 之间的整数. 在字符串 "Hello world!" 中,我们将 ...

  7. npm使用【转】

    NPM是一个Node包管理和分发工具,已经成为了非官方的发布Node模块(包)的标准.有了NPM,可以很快的找到特定服务要使用的包,进行下载.安装以及管理已经安装的包.在安装nodeJS 安装包的时候 ...

  8. jquery cookie用法

    jquery cookie用法(获取cookie值,删除cookie) cookie在jquery中有指定的cookie操作类,下面我先来介绍我们在使用cookie操作类时的一些问题,然后介绍正确的使 ...

  9. Java NIO学习-详细内容(一)

    一.三大类 1.Channels FileChannel DatagramChannel SocketChannel ServerSocketChannel 2.Selector与SelectionK ...

  10. 如何轻松学习C语言编程!

    C语言是面向过程的,而C++是面向对象的 C和C++的区别: C是一个结构化语言,它的重点在于算法和数据结构.C程序的设计首要考虑的是如何通过一个过程,对输入(或环境条件)进行运算处理得到输出(或实现 ...