springboot+ thymeleaf 实现文件上传

Controller层

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;

@Controller
public class FileUploadController {

    @PostMapping("/upload")
    public String handleFileUpload(@RequestParam("files") MultipartFile[] files, Model model) {
        boolean allSuccessful = true; // 假设所有文件上传都成功,根据实际情况修改此逻辑
        for (MultipartFile file : files) {
            // 你的文件上传逻辑...
            if (!uploadSuccessful) {
                allSuccessful = false;
                break;
            }
        }

        if (allSuccessful) {
            model.addAttribute("successMessage", "所有文件上传成功!");
        } else {
            model.addAttribute("errorMessage", "文件上传过程中发生错误!");
        }
        return "uploadResult"; // 返回到展示结果的Thymeleaf页面
    }
}

前端thymeleaf

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>文件上传结果</title>
</head>
<body>
    <!-- 成功提示 -->
    <div th:if="${successMessage}" class="alert alert-success" role="alert">
        <span th:text="${successMessage}"></span>
    </div>

    <!-- 失败提示 -->
    <div th:if="${errorMessage}" class="alert alert-danger" role="alert">
        <span th:text="${errorMessage}"></span>
    </div>
    
    
</body>
</html>

实现富文本编辑器可以使用一些开源的JavaScript库,比如常用的有: 1. CKEditor:功能强大、可扩展性好、使用方便,支持多种浏览器和平台,但是需要付费使用高级功能。 2. TinyMCE:也是一个功能强大的富文本编辑器,开源免费,支持多种浏览器和平台,易于集成到应用中。 3. Summernote:一个基于Bootstrap的富文本编辑器,开源免费,支持多种浏览器和平台,并且具有一些独特的功能,如代码视图、图片上传等。 4. Froala Editor:一个功能强大、易于使用的富文本编辑器,支持多种浏览器和平台,但是需要付费使用高级功能。 以下是基于SpringBootThymeleaf实现使用CKEditor富文本编辑器的步骤: 1. 引入CKEditor相关的JavaScript和CSS文件,可以从官网下载或者使用CDN方式引入: ```html <!-- 引入CSS --> <link rel="stylesheet" href="https://cdn.ckeditor.com/4.16.0/standard/ckeditor.css"> <!-- 引入JS --> <script src="https://cdn.ckeditor.com/4.16.0/standard/ckeditor.js"></script> ``` 2. 在Thymeleaf模板中使用CKEditor: ```html <!-- 定义富文本编辑器容器 --> <div id="editor"></div> <!-- 初始化CKEditor --> <script> CKEDITOR.replace('editor'); </script> ``` 3. 在后端Controller中获取富文本编辑器中的内容: ```java @PostMapping("/save") public String save(@RequestParam("content") String content) { // 处理富文本编辑器中的内容 return "success"; } ``` 以上就是基于SpringBootThymeleaf实现使用CKEditor富文本编辑器的步骤,其他富文本编辑器的使用方法类似。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值