JavaWeb-后端获取ckeditor的textarea的值

64 篇文章 1 订阅
23 篇文章 0 订阅

由于前端一些控件的影响,所以自己就在后端获取ckeditor的textarea的值

Java去除html代码中含有的html、js、css标签,获取文字内容

 

前端

HTML:

<input id="errorMsg" th:value="${errorMsg}" class="hidden" readonly="readonly"></input>

JS:

$(document).ready(function () {
	//错误提示
        if($('#errorMsg').val().length>0)
             layer.msg($('#errorMsg').val(), function(){
        });
}

 

后端

Controller层:

@RequestMapping(value="/save", method = {RequestMethod.POST})
public String saveMilestone(Model model,@ModelAttribute("thisMilestone") @Valid Milestone thisMilestone, HttpServletRequest request) {
	if(milestoneService.ckEdit(request)){
		model.addAttribute("errorMsg", "描述最少500字");
		return "add";
	}else{
		this.milestoneService.saveMilestone(thisMilestone);
		return "redirect:/list";
	}
}

Service层:

@Override
public boolean ckEdit(HttpServletRequest request) {
	String description = request.getParameter("description");
	DelTagsUtil text = new DelTagsUtil();
	description = text.getTextFromHtml(description);
	if(description.length()>10){
		return false;
	}else{
		return true;
	}
}

工具类:

package org.wts.projectadmin.common;

/**
 * 去除页面代码里的HTML标签
 * Created by MengMeng on 2018/8/3
 */
public class DelTagsUtil {
    public static String delHtmlTags(String htmlStr) {
        //定义script的正则表达式,去除js可以防止注入
        String scriptRegex="<script[^>]*?>[\\s\\S]*?<\\/script>";
        //定义style的正则表达式,去除style样式,防止css代码过多时只截取到css样式代码
        String styleRegex="<style[^>]*?>[\\s\\S]*?<\\/style>";
        //定义HTML标签的正则表达式,去除标签,只提取文字内容
        String htmlRegex="<[^>]+>";
        //定义空格,回车,换行符,制表符
        String spaceRegex = "\\s*|\t|\r|\n";
 
        htmlStr = htmlStr.replaceAll(scriptRegex, "");
        htmlStr = htmlStr.replaceAll(styleRegex, "");
        htmlStr = htmlStr.replaceAll(htmlRegex, "");
        htmlStr = htmlStr.replaceAll(spaceRegex, "");
        return htmlStr.trim(); // 返回文本字符串
    }

    public static String getTextFromHtml(String htmlStr){
        htmlStr = delHtmlTags(htmlStr);
        htmlStr = htmlStr.replaceAll(" ","");
        return htmlStr;
    }
 
}

 

 

相关学习链接:

https://blog.csdn.net/u010882234/article/details/80585175

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java Web 后端中,可以通过 `HttpServletRequest` 对象来获取 POST 请求的参数。一般情况下,POST 请求的参数是通过表单提交的,因此我们可以使用 `getParameter()` 方法来获取表单中的参数。 以下是获取 POST 请求参数的示例代码: ```java protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置请求编码格式,避免中文乱码 request.setCharacterEncoding("UTF-8"); // 获取 POST 请求参数 String username = request.getParameter("username"); String password = request.getParameter("password"); // 处理请求参数 // ... } ``` 在上面的代码中,我们首先通过 `request.setCharacterEncoding()` 方法设置请求编码格式,避免中文乱码。然后,我们通过 `request.getParameter()` 方法分别获取表单中的 `username` 和 `password` 参数,并进行相应的处理。如果需要获取多个参数,可以连续调用 `getParameter()` 方法来获取。 需要注意的是,如果 POST 请求的参数是以 JSON 格式提交的,我们需要使用 `getReader()` 方法来获取请求体,并使用 JSON 解析库来解析 JSON 数据。以下是以 JSON 格式提交 POST 请求参数的示例代码: ```java protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置请求编码格式,避免中文乱码 request.setCharacterEncoding("UTF-8"); // 获取 POST 请求参数 StringBuilder sb = new StringBuilder(); BufferedReader reader = request.getReader(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line); } String jsonStr = sb.toString(); JSONObject jsonObj = JSONObject.fromObject(jsonStr); String username = jsonObj.getString("username"); String password = jsonObj.getString("password"); // 处理请求参数 // ... } ``` 在上面的代码中,我们首先通过 `request.setCharacterEncoding()` 方法设置请求编码格式,然后使用 `request.getReader()` 方法获取请求体,并将其转换为字符串。最后,我们使用 JSON 解析库来解析 JSON 数据,并获取 `username` 和 `password` 参数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值