【问题标题】:Spring Thymeleaf - POST into ResponseEntity?Spring Thymeleaf - POST到ResponseEntity?
【发布时间】:2020-11-21 21:10:01
【问题描述】:

整天都在这。我可能在某处遗漏了注释。我也无法让这个应用程序为 index.html 提供服务。

我在这里缺少什么?主要问题是无法让表单向后端提交任何内容。 ModelAttribute 正确吗?

提前致谢。

控制器:

package com.lms.application.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.lms.application.entity.Course;
import com.lms.application.service.CourseService;

@RestController
@RequestMapping("/courses")
public class CourseController {
    
        
    
        @Autowired
        private CourseService service;
        
        @RequestMapping(method=RequestMethod.GET)
        public ResponseEntity<Object> getCourses(){
            return new ResponseEntity<Object>(service.getCourses(), HttpStatus.OK);
        }
        
        @RequestMapping(value="/submit", method=RequestMethod.POST)     
        public ResponseEntity<Object> createCourse(@ModelAttribute("course") Course course){
            return new ResponseEntity<Object>(service.createCourse(course), HttpStatus.CREATED);
        }

表格

                <div class="container">
                    <form method="post" th:object="${course}" th:action="@{/courses/submit}">
                        <div class="row mb-3">
                            <label for="title" class="col-sm-2 col-form-label">Course Title</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="course.title" th:field="${course.title}"></input>
                            </div>
                        </div>
                        <div class="row mb-3">
                            <label for="credit" class="col-sm-2 col-form-label">Course
                                Credits</label>
                            <div class="col-sm-10">
                                <input type="number" class="form-control" id="course.credits" th:field="${course.credits}"></input>
                            </div>
                        </div>
                        <button type="submit" class="btn btn-primary">Submit</button>
                    </form>
                    </div>

【问题讨论】:

    标签: java spring spring-boot spring-mvc thymeleaf


    【解决方案1】:

    在向 Thymeleaf 请求一个对象之前,您必须在那里创建并传递一个对象。 Thymeleaf 不会为您创建对象。

    您需要通过模型将对象传递给控制器​​,如下所示:

    @ModelAttribute("course") 
    public Course course() { 
      return new Course(); 
    }
    

    您需要确保 Course 对象具有 getter、setter 和默认构造函数,以便 Thymeleaf 能够正确使用它。

    【讨论】:

      猜你喜欢
      • 2020-05-23
      • 2018-02-07
      • 2017-09-07
      • 2017-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-05
      • 2017-03-13
      相关资源
      最近更新 更多