Search code examples
javaspringspring-bootspring-mvcthymeleaf

Spring Thymeleaf - POST into ResponseEntity?


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("/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);
        }

Form

                <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>

Solution

  • 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("course") 
    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.