微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

不能在 ModelAttribute 中使用 MultipartFile 和 Thymeleaf

如何解决不能在 ModelAttribute 中使用 MultipartFile 和 Thymeleaf

我的项目是一个 spring-boot 项目,使用父依赖项和 thymeleaf 以及一些用于 UI 的 webjars 依赖项。我不知道我错过了什么。

我不能使用 multipartfile 作为 modelattributeDto 的字段。

当我想打开 HTML 时发生异常。我的问题是我想使用 thymeleaf 的验证,并且不想使用 multipartfile 作为简单的 @RequestParam

细节在这里,如果你想要 pom.xml 我很乐意分享它。

感谢您的帮助。

除标准 spring-boot 2.4.3 依赖项之外的依赖项,pom.xml

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>4.6.0</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>webjars-locator</artifactId>
            <version>0.40</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>font-awesome</artifactId>
            <version>5.15.2</version>
        </dependency>
@Controller
public class UploadController {

    private final RestUploadService restUploadService;

    @GetMapping("/upload")
    public String load(Model model) {
        model.addAttribute("uploadParams",new UploadParams());
        return "upload/upload";
    }

    @PostMapping("/upload")
    public String upload(
            @Valid @modelattribute("uploadParams") UploadParams uploadParams,BindingResult result,Model model
    ) {
        return "upload/upload";
    }
}

上传参数:

@Data
public class UploadParams {

    String tableName;

    multipartfile fileSource;
}

HTML 表单:

<form action="#" enctype="multipart/form-data" th:action="@{/upload}" th:object="${uploadParams}" method="post"
          onsubmit="this.submit_button.disabled = true;">
        <div class="form-group row">
            <label for="tableName" class="col-sm-2 col-form-label">Table name</label>
            <div class="col-sm-10">
                <input id="tableName" class="form-control" type="text" th:field="*{tableName}"
                       placeholder="testExtendedTable"/>
                <div class="alert alert-danger" th:if="${#fields.hasErrors('tableName')}" th:errors="*{tableName}"></div>
            </div>
        </div>
        <div class="form-group row">
            <label for="fileSource" class="col-sm-2 col-form-label">SDF file</label>
            <div class="col-sm-10">
                <input id="fileSource" name="fileSource" class="form-control-file" type="file" placeholder="testExtendedTable" th:field="*{fileSource}"/>
                <div class="alert alert-danger" th:if="${#fields.hasErrors('fileSource')}" th:errors="*{fileSource}"></div>
            </div>
        </div>

        <div class="form-group row">
            <div class="col-sm-10">
                <button type="submit" class="btn btn-primary">Submit</button>
            </div>
        </div>
</form>

页面甚至无法加载,并且出现以下异常:

org.springframework.beans.NotReadablePropertyException: Invalid property 'fileSource' of bean class [com.chemaxon.consultancy.codingexercise.adapters.api.dto.UploadParams]: Bean property 'fileSource' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
    at org.springframework.beans.AbstractnestablePropertyAccessor.getPropertyValue(AbstractnestablePropertyAccessor.java:625) ~[spring-beans-5.3.4.jar:5.3.4]
    at org.springframework.beans.AbstractnestablePropertyAccessor.getPropertyValue(AbstractnestablePropertyAccessor.java:615) ~[spring-beans-5.3.4.jar:5.3.4]
    at org.springframework.validation.AbstractPropertyBindingResult.getActualFieldValue(AbstractPropertyBindingResult.java:104) ~[spring-context-5.3.4.jar:5.3.4]
    at org.springframework.validation.AbstractBindingResult.getFieldValue(AbstractBindingResult.java:228) ~[spring-context-5.3.4.jar:5.3.4]
    at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:129) ~[spring-webmvc-5.3.4.jar:5.3.4]
    at org.springframework.web.servlet.support.RequestContext.getBindStatus(RequestContext.java:903) ~[spring-webmvc-5.3.4.jar:5.3.4]
    at org.thymeleaf.Spring5.context.webmvc.SpringWebMvcThymeleafRequestContext.getBindStatus(SpringWebMvcThymeleafRequestContext.java:227) ~[thymeleaf-Spring5-3.0.12.RELEASE.jar:3.0.12.RELEASE]
    at org.thymeleaf.Spring5.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:306) ~[thymeleaf-Spring5-3.0.12.RELEASE.jar:3.0.12.RELEASE]
...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?