做了一个简易在线的富文本编辑器,前端是html,后台是Java
前端代码:
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<title></title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<script src="https://cdn.staticfile.org/wangEditor/10.0.13/wangEditor.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
<form method="post" th:action="@{/test3}">
<div>
<div id="intro" class="TextContent"></div>
<label for="txtIntro"></label>
<textarea style="display: none" name="mytxtIntro" id="txtIntro">
</textarea>
<input class="btn btn-primary" type="submit" value="确认提交">
</div>
</form>
</body>
<script type="text/javascript">
var E = window.wangEditor;
var editor = new E('#intro');
// 获取隐藏控件<textarea>的id,用于显示内容,也方便后台获取内容
var $text1 = $('#txtIntro');
// 监控wangEditor中的内容变化,并将html内容同步更新到 textarea
editor.customConfig.onchange = function (html) {
$text1.val(html);
};
// 设置上传本地图片到服务器
editor.customConfig.uploadImgServer = '/upload';
editor.create();
$text1.val(editor.txt.html());// 初始化 textarea 的值
</script>
</html>
后台代码:
@RequestMapping(value = "/test3")
public String toIndex2(HttpServletRequest request, Model model) {
String s = request.getParameter("mytxtIntro");
System.err.println(s);
return "test3";
}
效果展示: