java富文本编辑数据存储到数据库简单实现
java富文本编辑数据存储到数据库简单实现-小白日记前端代码<link rel="stylesheet" href="../themes/default/default.css" /><script charset="utf-8" src="../kindeditor-all.js"></script><script charset="utf-8" src
·
java富文本编辑数据存储到数据库简单实现-小白日记
前端代码
<link rel="stylesheet" href="../themes/default/default.css" />
<script charset="utf-8" src="../kindeditor-all.js"></script>
<script charset="utf-8" src="../lang/zh-CN.js"></script>
ps:看代码注释
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml"
>
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>查看日记</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<style>
form {
margin: 0;
}
textarea {
display: block;
}
</style>
<link rel="stylesheet" href="../themes/default/default.css" />
<script charset="utf-8" src="../kindeditor-all.js"></script>
<script charset="utf-8" src="../lang/zh-CN.js"></script>
<script>
var editor;
KindEditor.ready(function(K) {
editor = K.create('textarea[name="cont2"]', {
allowFileManager : true
});
<!--这里才是最重要的 将富文本输入的内容生成html保存在content字段-->
K('input[name=getHtml]').click(function(e) {
document.getElementById('content').value=editor.html();
alert(document.getElementById('content').value);
});
K('input[name=isEmpty]').click(function(e) {
alert(editor.isEmpty());
});
K('input[name=getText]').click(function(e) {
alert(editor.text());
});
K('input[name=selectedHtml]').click(function(e) {
alert(editor.selectedHtml());
});
K('input[name=setHtml]').click(function(e) {
editor.html('<h3>Hello KindEditor</h3>');
});
K('input[name=setText]').click(function(e) {
editor.text('<h3>Hello KindEditor</h3>');
});
K('input[name=insertHtml]').click(function(e) {
editor.insertHtml('<strong>插入HTML</strong>');
});
K('input[name=appendHtml]').click(function(e) {
editor.appendHtml('<strong>添加HTML</strong>');
});
K('input[name=clear]').click(function(e) {
editor.html('');
});
});
</script>
</head>
<body>
<form action="/addDiary" method="post">
<textarea name="cont2" style="width:800px;height:400px;visibility:hidden;">开始今天的日记!</textarea>
<input type="hidden" id="content" name="content"/>
<p>
<input type="button" name="getHtml" value="取得HTML" />
<input type="button" name="clear" value="清空内容" />
<input type="reset" name="reset" value="Reset" />
</p>
<button type="submit" class="btn btn-primary">提交</button>
</form>
</body>
</html>
后台代码
@RequestMapping("/addDiary")
public String addDiary(Diary diary){
diary.setTime(new Date());
System.out.println(diary);
diaryMapper.addDiary(diary);
return "redirect:/index";
}
保存的diary就只有string类型content这个字段,数据库类型我使用了mediumtext(最大16M),为的是保存表格这种大文本(当你插入表格这种数据的时候,保存数据库的时候因为生成html太大没办法保存)
需要kindeditor的一个资源包,我也已经上传了,需要可以自行下载。
数据库字段
更多推荐
已为社区贡献1条内容
所有评论(0)