spring引导表单不返回模型

kt06eoxx  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(244)

我正在使用springbootmvc创建一个文档上传组件。我遇到了视图不返回实体的问题。get方法将实体传递给视图,但是当视图调用post方法时,实体不会绑定到它,这意味着会创建一个新的空实体对象并将其传递给post方法(与@modeldattribute的情况相同)。我试过了
只需按一下按钮就可以用空表单发布。还是不起作用。
使用modelandview而不是model
文档上传表单是根据所需的文档类型动态生成的,因此实体由要由表单填充的子实体列表组成(这对问题不重要,仅供参考)。重申一下,视图正确地从控制器接收实体(因为子实体属性正确地显示在窗体上),但不会将其发送回控制器。
非常感谢您的帮助。
代码
子实体

import java.util.Date;

public class Document {

    private String documentType;
    private MultipartFile documentFile;

    @DateTimeFormat(pattern = "dd/MM/yyyy h:mm a")
    private Date expiryDate;

    private String fileName;
    public Document() {

    }

 //getters & setters

Entity

    public class Form {

    private List<Document> documents;
    private long masterId;

    public Form() {
        documents = new ArrayList<Document>();
    }
 //getters& setters

控制器

@Controller
public class FormController {

    private static Logger logger = LogManager.getLogger(FormController.class);

    @Autowired
    private JDBCRepository jdbcRepository;

    @GetMapping("/")
    public String formWindow(@RequestParam(name = "masterId") long masterId, Model model) {
        List<String> documentTypes = jdbcRepository.getDocumentTypes(masterId);
        Form form1 = new Form();
        form1.setMasterId(masterId);
        for (String documentType:documentTypes) {
            Document document = new Document();
            document.setDocumentType(documentType);
            form1.addDocument(document);
        }
        //return new ModelAndView("DocumentForm", "form", form);
        model.addAttribute("form1",form1);
        return "DocumentForm";
    }

    @PostMapping("/")
    public String saveData(@ModelAttribute("form1") Form form1, Model model) {
        logger.info(form1.getMasterId());
        for (Document document: form1.getDocuments()) {
            try {
                document.setFileName(document.getDocumentFile().getOriginalFilename());
                jdbcRepository.update(form1.getMasterId(),document.getDocumentType(),document.getDocumentFile().getBytes(),document.getExpiryDate(),document.getFileName());
            } catch (IOException e) {
                e.printStackTrace();
            }
            logger.info(document.getExpiryDate());
        }
        //return new ModelAndView("SaveScreen", "form", form);
        model.addAttribute("form1",form1);
        return "SaveScreen";
    }

}

HTML表单

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Document upload page</title>
    <meta charset="UTF-8"/>

    <link th:rel="stylesheet" th:href="@{/assets/tempusdominus-bootstrap-4/tempusdominus-bootstrap-4.min.css}"/>
    <link th:rel="stylesheet" th:href="@{/webjars/bootstrap/4.0.0-2/css/bootstrap.min.css} "/>
    <link th:rel="stylesheet" th:href="@{/webjars/font-awesome/5.11.2/css/all.css} "/>
</head>
<body>

<div>
    <form action="#" th:action="@{/}" th:object="${form1}" method="post">
        <table>
            <tr th:each="document : ${form1.documents}">
                <td th:text="${document.documentType}"></td>
                <td><input type="file" name="document.document" th:path="${document.documentFile}"/></td>

                <td>                <div class="form-group">
                    <label>Date:</label>
                    <div class="input-group date" id="datetimepicker1" data-target-input="nearest">
                        <input type="text" class="form-control datetimepicker-input" data-target="#datetimepicker1"
                               th:path="${document.expiryDate}" id="document.expiryDate" placeholder="Date"/>
                        <div class="input-group-append" data-target="#datetimepicker1" data-toggle="datetimepicker">
                            <div class="input-group-text"><i class="fa fa-calendar-alt"></i></div>
                        </div>
                    </div>
                </div></td>
            </tr>
        </table>
        <button type="submit">Upload</button>
    </form>
</div>

<script th:src="@{/webjars/jquery/jquery.min.js}"></script>
<script th:src="@{/webjars/popper.js/umd/popper.min.js}"></script>
<script th:src="@{/webjars/bootstrap/js/bootstrap.min.js}"></script>
<script th:src="@{/assets/moment/moment.min.js}"></script>
<script th:src="@{/assets/tempusdominus-bootstrap-4/tempusdominus-bootstrap-4.min.js}"></script>

<script>
    $.fn.datetimepicker.Constructor.Default = $.extend({}, $.fn.datetimepicker.Constructor.Default, {
        icons: {
            time: 'far fa-clock',
            date: 'far fa-calendar',
            up: 'fas fa-arrow-up',
            down: 'fas fa-arrow-down',
            previous: 'fas fa-chevron-left',
            next: 'fas fa-chevron-right',
            today: 'far fa-calendar-check-o',
            clear: 'far fa-trash',
            close: 'far fa-times'
        } });

    $('#datetimepicker1').datetimepicker({
        format: 'dd/MM/yyyy h:mm a'
    });
</script>

</body>
</html>

pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.</groupId>
    <artifactId>OnboardingProcesser</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>OnboardingProcesser</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>webjars-locator</artifactId>
            <version>0.40</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.16</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>font-awesome</artifactId>
            <version>5.11.2</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>4.0.0-2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.oracle.database.jdbc</groupId>
            <artifactId>ojdbc8</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题