【发布时间】: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