Home > Software design >  Html post request using spring mvc and thymeleaf
Html post request using spring mvc and thymeleaf

Time:08-09

I'm writing a simple web-application using Spring MVC,postgreSQL,thymeleaf and some javascript scripts and css styles. It is something like a weekly task manager, and my goal is to perform CRUD operations and display information in html page. This is my controllers(main, which just redirects user from home page to application page, and application controller):

package com.example.inobitectest.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class MainController {

    @GetMapping("/home")
    public String getHomePage(){
        return "home";
    }
    @GetMapping("/test")
    public String getTestPage(){
        return "TestTask";
    }
}

package com.example.inobitectest.controllers;
import com.example.inobitectest.dto.TaskDto;
import com.example.inobitectest.models.Task;
import com.example.inobitectest.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/test")
public class TaskController {
    TaskService taskService;

    @Autowired
    public TaskController(TaskService taskService) {
        this.taskService = taskService;
    }

    @PostMapping
    public ResponseEntity<Task> createTask(@RequestBody TaskDto taskDto) {
        Task task = taskService.createTask(taskDto.getTitle(),taskDto.isCompleted());
        return ResponseEntity.status(HttpStatus.CREATED).body(task);
    }
    @PutMapping
    @ResponseBody
    public ResponseEntity<Task> updateTask(@RequestBody TaskDto taskDto){
        Task task = taskService.updateTask(taskDto.getId(),taskDto.getTitle(),taskDto.isCompleted());
        return ResponseEntity.ok(task);
    }

    @GetMapping(value = "/{id}" )
    public ResponseEntity<Task> getTaskById(@PathVariable("id") Long id ){
        Task task = taskService.getTaskById(id);
        return ResponseEntity.ok(task);
    }

    @DeleteMapping(value = "/{id}")
    @ResponseBody
    public void deleteTask(@PathVariable("id") Long id){
        taskService.deleteTaskById(id);
    }
}

Task entity:

package com.example.inobitectest.models;
import lombok.Data;
import javax.persistence.*;
import java.time.LocalDate;

@Entity
@Data
@Table(name = "tasks")
public class Task {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String title;
    private boolean isCompleted;
    private LocalDate creationDate;
}

Service layer:

package com.example.inobitectest.service.TaskServiceImpl;

import com.example.inobitectest.models.Task;
import com.example.inobitectest.service.TaskService;
import com.example.inobitectest.taskRepository.TaskRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

@Service
public class TaskServiceImpl implements TaskService {
    TaskRepository taskRepository;

    @Autowired
    public TaskServiceImpl(TaskRepository taskRepository) {
        this.taskRepository = taskRepository;
    }

    @Override
    public Task createTask(String title,boolean isCompleted){
        Task task = mapTask(title,isCompleted);

        return taskRepository.save(task);
    }

    @Override
    public Task updateTask(Long id, String title, boolean isCompleted) {
        Task task = mapTask(title, isCompleted);
        task.setId(id);
        return taskRepository.save(task);
    }

    @Override
    public Task getTaskById(Long id) {
        return  taskRepository.findById(id).get();
    }

    @Override
    public void deleteTaskById(Long id) {
        taskRepository.deleteById(id);
    }

    private Task mapTask(String title, boolean isCompleted) {
        Task task = new Task();
        task.setTitle(title);
        task.setCompleted(isCompleted);
        task.setCreationDate(LocalDate.now());
        return task;
    }
}

And HTML pop-up form:

<div  id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div >
        <div >
            <div >
                <h5  id="exampleModalLabel">Modal title</h5>
                <button type="button"  data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div >
                <form th:action="@{/test}" th:method="POST" th:object="${task}">
                    <input type="text" th:field="*{task.title}" placeholder="Task name">
                    <input type="checkbox" th:field="*{task.isCompleted}" placeholder="Done?">
                    <input type="submit" value="AddTask">
                </form>
            </div>
            <div >
                <button type="button"  data-bs-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

However I got TemplateProcessingExeption when i enter application page

CodePudding user response:

A *{} annotation is relative the the encapsulating th:object so you would have to use *{title} and *{isCompleted} in your example.

As it is now thymeleaf looks for a task parameter within your task object which does not exist

  • Related