繁体   English   中英

Html 使用 spring mvc 和 thymeleaf 发布请求

[英]Html post request using spring mvc and thymeleaf

I'm writing a simple web-application using Spring MVC,postgreSQL,thymeleaf and some javascript scripts and css styles. 它有点像每周任务管理器,我的目标是执行 CRUD 操作并在 html 页面中显示信息。 这是我的控制器(主要,它只是将用户从主页重定向到应用程序页面和应用程序控制器):

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);
    }
}

任务实体:

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

服务层:

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

和 HTML 弹出形式:

<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <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 class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

但是,当我进入应用程序页面时,我得到了 TemplateProcessingExeption

*{}注释与封装th:object ,因此您必须在示例中使用*{title}*{isCompleted}

现在是 thymeleaf 在您的任务 object 中查找不存在的任务参数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM