当前位置:   article > 正文

springboot+layui +ueditor前端及后端完整开发案例_layui ueditor

layui ueditor

官网下载地址 :GitHub - fex-team/ueditor: rich text 富文本编辑器

下载最新jsp版本导入项目,项目编辑器版本为1.4.3.3

导入到资源文件中,将demo、网页文件、jsp文件从资源目录中移除

 页面引入相关js

  1. <script type="text/javascript" charset="utf-8" src="${springMacroRequestContext.contextPath}/ueditor/ueditor.config.js"></script>
  2. <script type="text/javascript" charset="utf-8" src="${springMacroRequestContext.contextPath}/ueditor/ueditor.all.min.js"> </script>
  3. <!--建议手动加在语言,避免在ie下有时因为加载语言失败导致编辑器加载失败-->
  4. <!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型,比如你在配置项目里配置的是英文,这里加载的中文,那最后就是中文-->
  5. <script type="text/javascript" charset="utf-8" src="${springMacroRequestContext.contextPath}/ueditor/lang/zh-cn/zh-cn.js"></script>

实例化编辑器

  1. <div class="layui-form-item">
  2. <div class="layui-col-xs12">
  3. <label class="layui-form-label">内容</label>
  4. <div class="layui-input-block">
  5. <textarea id="editor" style="width: 90%;height: 250px" name="introduce" class="layui-textarea"></textarea>
  6. </div>
  7. </div>
  8. </div>
  1. <script>
  2. var ue = UE.getEditor('editor');
  3. </script>

 编辑ueditor.config.js根据自己需求对编辑器进行配置。

上传文件后端路径一定要修改为自己的地址

  1. //为编辑器实例添加一个路径,这个不能被注释
  2. UEDITOR_HOME_URL: URL
  3. // 服务器统一请求接口路径
  4. , serverUrl: URL + "article/upload"

pom中引入json包,我这里引入的json版本为20220924

  1. <dependency>
  2. <groupId>org.json</groupId>
  3. <artifactId>json</artifactId>
  4. <version>20220924</version>
  5. </dependency>

将ueditor\jsp\lib\ueditor-1.1.2.jar包转到工程文件中

controller中配置文件路径,注意要在控制器中对返回路径进行处理,否则返回的是文件存储绝对路径。

  1. @Controller
  2. @RequestMapping("/person/ueditor")
  3. public class SystemDocUeditorController {
  4. @RequestMapping("/upload")
  5. @ResponseBody
  6. public String upload(HttpServletRequest request, HttpServletResponse response) throws Exception {
  7. request.setCharacterEncoding( "utf-8" );
  8. response.setHeader("Content-Type" , "text/html");
  9. String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
  10. String result= new ActionEnter( request, rootPath+"/static" ).exec();
  11. String action = request.getParameter("action");
  12. rootPath=new File(rootPath+"/static/").toString();
  13. if( action!=null && (action.equals("listfile") || action.equals("listimage") ) ){
  14. rootPath = PathFormat.format(rootPath);
  15. result = result.replace(rootPath, "");
  16. }
  17. return result;
  18. }
  19. }

特别注意:路径错误会导致无法读取配置文件

 正确配置效果

如果出现找到上传数据问题:

1、拦截器拦截掉了数据,放行路径

2、使用ServletFileUpload时需要关闭Spring Boot的默认配置 ,禁用MultipartResolverSpring提供的默认值,在配置文件中加入

spring.servlet.multipart.enabled=false

需要修改BinaryUploader类

  1. /**
  2. for(FileItemIterator iterator = upload.getItemIterator(request); iterator.hasNext(); fileStream = null) {
  3. fileStream = iterator.next();
  4. if (!fileStream.isFormField()) {
  5. break;
  6. }
  7. }
  8. **/
  9. FileItemIterator iterator = upload.getItemIterator(request);
  10. while (iterator.hasNext()) {
  11. fileStream = iterator.next();
  12. if (!fileStream.isFormField()) {
  13. break;
  14. }
  15. }

在线管理中添加删除文件功能

前端改造

添加删除按钮:

改造ueditor\dialogs\image\image.js

改造ueditor\dialogs\attachment\attachment.js

attachment的js中action=deletefile

  1. item.appendChild(img);
  2. item.appendChild(icon);
  3. /* 添加删除功能 Start*/
  4. item.appendChild($("<span class='delbtn' url='" + list[i].url + "'>X</span>").click(function() {
  5. var del = $(this);
  6. try{
  7. window.event.cancelBubble = true; //停止冒泡
  8. window.event.returnValue = false; //阻止事件的默认行为
  9. window.event.preventDefault(); //取消事件的默认行为
  10. window.event.stopPropagation(); //阻止事件的传播
  11. }finally{
  12. if(!confirm("确定要删除吗?")) return;
  13. $.post(editor.getOpt("serverUrl") + "?action=deleteimage", { "path": del.attr("url") }, function(result) {
  14. var json = eval('(' + result + ')');
  15. if (json.state == 'SUCCESS') {
  16. del.parent().remove();
  17. }
  18. else {
  19. alert(json.state);
  20. }
  21. });
  22. }
  23. })[0]);
  24. /* 添加删除功能 End*/
  25. this.list.insertBefore(item, this.clearFloat);

添加删除按钮样式:

ueditor\dialogs\image\image.css中添加样式

ueditor\dialogs\attachment\attachment.css中添加样式

  1. #online li .delbtn {
  2. position: absolute;
  3. top: 0;
  4. right: 0;
  5. border: 0;
  6. z-index: 3;
  7. color: #ffffff;
  8. display: inline;
  9. font-size: 12px;
  10. line-height: 10.5px;
  11. padding:3px 5px;
  12. text-align: center;
  13. background-color: #d9534f;
  14. }

后端改造

改造ueditor.ActionEnter,invoke()的switch(actionCode) {}插入代码

  1. case 7:
  2. conf = this.configManager.getConfig(actionCode);
  3. int start = this.getStartIndex();
  4. state = (new FileManager(conf)).listFile(start);
  5. break;
  6. case 8:
  7. state = (new DeleteFile(this.request,this.rootPath)).doExec();
  8. break;
  9. case 9:
  10. state = (new DeleteFile(this.request,this.rootPath)).doExec();

改造ueditor.ActionMap

  1. public final class ActionMap {
  2. public static final Map<String, Integer> mapping = new HashMap<String, Integer>() {
  3. {
  4. this.put("config", 0);
  5. this.put("uploadimage", 1);
  6. this.put("uploadscrawl", 2);
  7. this.put("uploadvideo", 3);
  8. this.put("uploadfile", 4);
  9. this.put("catchimage", 5);
  10. this.put("listfile", 6);
  11. this.put("listimage", 7);
  12. this.put("deleteimage", 8);
  13. this.put("deletefile", 9);
  14. }
  15. };
  16. public static final int CONFIG = 0;
  17. public static final int UPLOAD_IMAGE = 1;
  18. public static final int UPLOAD_SCRAWL = 2;
  19. public static final int UPLOAD_VIDEO = 3;
  20. public static final int UPLOAD_FILE = 4;
  21. public static final int CATCH_IMAGE = 5;
  22. public static final int LIST_FILE = 6;
  23. public static final int LIST_IMAGE = 7;
  24. public static final int DELETE_IMAGE = 8;
  25. public static final int DELETE_FILE = 9;

创建类 DeleteFile

  1. public class DeleteFile {
  2. private HttpServletRequest request = null;
  3. private String rootPath = null;
  4. public DeleteFile(HttpServletRequest request, String rootPath) {
  5. this.request = request;
  6. this.rootPath = rootPath;
  7. }
  8. public final State doExec() {
  9. State state = null;
  10. try {
  11. File file = new File(this.rootPath+this.request.getParameter("path"));
  12. if(!file.toString().contains(this.rootPath)){
  13. return new BaseState(false, 4);
  14. }
  15. if (file.exists() && file.isFile()) {
  16. if (file.delete()) {
  17. state = new BaseState(true);
  18. } else {
  19. state = new BaseState(false, "删除文件" + file.getName() + "失败!");
  20. }
  21. } else {
  22. state = new BaseState(false, file.getName()+"文件不存在!");
  23. }
  24. } catch (Exception var10) {
  25. return new BaseState(false, 4);
  26. }
  27. return state;
  28. }
  29. }

 最终效果

 

代码下载地址:springboo+ueditor前端及后端开发代码案例-Java文档类资源-CSDN下载

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/155657
推荐阅读
相关标签
  

闽ICP备14008679号