Spring Thymeleaf – 将POST请求转换为ResponseEntity?

huangapple go评论44阅读模式
英文:

Spring Thymeleaf - POST into ResponseEntity?

问题

我可能错过了某处的注释。我还无法让这个应用程序提供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>
英文:

Been at this all day. I may be missing an annotation somewhere. I also cannot get this app to serve the index.html.

What am I missing here? The primary issue is not being able to get the form to submit anything to the backend. Is ModelAttribute correct?

Thanks in advance.

Controller:

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(&quot;/courses&quot;)
public class CourseController {
	
		
	
		@Autowired
		private CourseService service;
		
		@RequestMapping(method=RequestMethod.GET)
		public ResponseEntity&lt;Object&gt; getCourses(){
			return new ResponseEntity&lt;Object&gt;(service.getCourses(), HttpStatus.OK);
		}
		
		@RequestMapping(value=&quot;/submit&quot;, method=RequestMethod.POST)		
		public ResponseEntity&lt;Object&gt; createCourse(@ModelAttribute(&quot;course&quot;) Course course){
			return new ResponseEntity&lt;Object&gt;(service.createCourse(course), HttpStatus.CREATED);
		}

Form

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

答案1

得分: 1

在要求Thymeleaf提供一个对象之前,您需要创建并传递一个对象。Thymeleaf不会为您创建对象。

您需要通过Model将对象传递给Controller,如下所示:

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

您需要确保Course对象具有用于Thymeleaf正确工作的getter、setter和默认构造函数。

英文:

Before demanding an Object from Thymeleaf you have to create and pass one there. Thymeleaf won't create an object for you.

You need to pass the object via Model to the Controller like so:

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

You need to make sure Course object has getters, setters and default constructor for Thymeleaf to be able to work with it correctly.

huangapple
  • 本文由 发表于 2020年8月1日 12:11:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63201685.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定