后台的数据库中某个字段是富文本框输入的 带有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. LSP(分层服务提供程序)

    一.简介 LSP即分层服务提供商,Winsock 作为应用程序的 Windows 的网络套接字工具,可以由称为"分层服务提供商"的机制进行扩展.Winsock LSP 可用于非常广 ...

  2. js正则表达式语法[转]

    1. 正则表达式规则 1.1 普通字符 字母.数字.汉字.下划线.以及后边章节中没有特殊定义的标点符号,都是"普通字符".表达式中的普通字符,在匹配一个字符串的时候,匹配与之相同的 ...

  3. 04 Rabbits and Recurrence Relations

    Problem A sequence is an ordered collection of objects (usually numbers), which are allowed to repea ...

  4. [GO]使用select实现斐波那契

    package main import "fmt" func fibonacci(ch chan <- int, quit <- chan bool) { x, y : ...

  5. [GO]errorr接口的使用

    package main import ( "fmt" "errors" ) func main() { erro1 := fmt.Errorf("% ...

  6. Word2013写CSDN博客

    目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...

  7. 深入探讨 Java 类加载器(转载)

    类加载器(class loader)是 Java™中的一个很重要的概念.类加载器负责加载 Java 类的字节代码到 Java 虚拟机中.本文首先详细介绍了 Java 类加载器的基本概念,包括代理模式. ...

  8. 20、Semantic-UI之数据验证

    20.1 实现数据验证   在很多前端框架中都提供了数据验证的操作,比如jQuery的验证框架等,但是jQuery的验证框架js文件太多:在使用Semantic-UI框架的时候只需要导入semanti ...

  9. [LeetCode 题解]: Permutations

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  10. 搭建服务器集群——Windows7系统中nginx与IIS服务器搭建集群实现负载均衡

    转载:https://www.cnblogs.com/xiongze520/p/10308720.html 分布式,集群,云计算机.大数据.负载均衡.高并发······当耳边响起这些词时,做为一个菜鸟 ...