Let's create a TODO application in Java 8 Implementation of editing function

Hello. This time I would like to implement the editing function part of TODO posted in Java Spring.

TODO application creation link collection

1: [Understanding the super basics] A brief description of MVC 2: [Prepare a template] I want to create a template with Spring Initializr and do Hello world 3: [Connection / Settings / Data display with MySQL] Save temporary data in MySQL-> Get all-> Display on top 4: [POST function] Implementation of posting function 5: [PATCH function] Switch TODO display 6: [Easy to use JpaRepository] Implementation of search function [7: [Common with Thymeleaf template fragment] Create Header] (https://qiita.com/nomad_kartman/items/8c33eca2880c43a06e40) 8: [PUT function] Implementation of editing function (here and now)

Flow of editing function

First of all, I would like to briefly explain the flow of editing functions!

  1. Search the TODO data you want to edit by ID and display it on the front.

  2. Enter your edits and send them to the controller via PUT

  3. Process internally and save.

As you might expect, it looks like this!

Let's start by displaying the TODO you want to edit on the front desk!

Display the TODO you want to edit

Get the TODO you want to edit and send it to the front side

    @GetMapping("/edit/{id}")
    public String showEdit(Model model, @PathVariable("id") long id ) {
        TodoEntity editTarget = todoService.findTodoById(id);
        model.addAttribute( "editTarget" , editTarget);
        return "edit";
    }

First, search the ID of the TODO you want to edit in the TodoEntity you have been using before and pack it.

And by setting model.addAttribute ("variable name", the object you want to pass), you can use this object on the front side.

This time, I will use it as it is with the name editTarget.

Since the transition to the edit page is made by return "edit";, create the edit page.

Create an edit page on the front side

python


<!DOCTYPE html>
<html lang="ja" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Edit</title>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    </head>
    <body>
        <!-- Header -->
        <header th:replace="common/header :: header_fragment()"></header>
        
        <!--Edit form-->
        <div class=" w-75 h-auto my-1 mx-auto pt-5">
            <p class="pl-5">Update tasks</p>
            <form  th:action="@{'/edit/' + *{id} + '/complete'}" th:object="${editTarget}" method="post" class="container d-flex w-auto my-0 mx-auto">
                <div class="w-100">
                    <label class="row">
                        <span class="col-2 text-center">ToDo name</span>
                        <input type="text" name="title" th:value="${editTarget.title}" class="col-9">
                    </label>
                    <label class="row my-0">
                        <span class="col-2 text-center">Deadline</span>
                        <input type="date" name="deadline" th:value="${editTarget.deadline}" class="col-9 my-0">
                    </label>
                </div>
                <input type="hidden" name="_method" value="put">
                <button class="btn btn-primary w-25 col-2 mr-3" type="submit">update</button>
            </form>
        </div>
        <!--/Edit form-->
    
        <!--read bootstrap js-->
        <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
    </body>
</html>

It may be a little long and hard to see, but it is an edit page.

th:action

↓ The first point is the export part of the edit form ↓

python


 <form  th:action="@{'/edit/' + *{id} + '/complete'}" th:object="${editTarget}" method="post" class="container d-flex w-auto my-0 mx-auto">

th:action="@{'/edit/' + *{id} + '/complete'}" The contents of the form are sent to the specified URL.

This time /edit/{id}/complete I will send it to the URL. This process will be described later.

th:objects

th:object="${editTarget}"

This is the editTarget you just sent from the showEdit function. By doing this, you can use editTarget in this form.

Even though it is PUT, method = "post" ???

I think this is the point that tends to get stuck in this process.

This time, I want to use the PUT method for editing, so with method = "put"! ?? You might think that. (Actually I did that at first)

But HTML5 can't handle PUT / DELETE methods!

Therefore, the following processing is required.

  1. Send form data by POST from HTML.
  2. Switch the POST method to the PUT method on the Spring side!
  3. Send the form data to the expected function.

You may wonder why such a troublesome thing is ... but for now it seems that there is no choice but to do this.

so

python


 <input type="hidden" name="_method" value="put">

By preparing an input that is not displayed on the front side, POST is converted to PUT.

Rewrite application.properties to make POST-> PUT

However, in fact, this alone will cause an error, so add the following to application.properties.

resources/application.properties


spring.mvc.hiddenmethod.filter.enabled=true

This will eliminate the error.

By the way Handling HTTP PUT / DELETE method in SpringBoot2.2 + Thymeleaf If you are interested, please read this article as it will deepen your understanding.

Implement editing process

Process the edited contents sent from the front desk

Next, we will implement the part that processes the edited contents.

java/com/example/todo/TodoController.java


    @PutMapping("/edit/{id}/complete")
    public String edit(@ModelAttribute TodoForm formData, @PathVariable("id") long id) {
        todoService.editToDo(formData);
        return "redirect:/top";
    }

Add PUT Mapping to your controller.

Store the data sent from the front with @ModelAttribute in the TodoForm class.

↓ And I will send the object to editTodo of service class ↓

java/com/example/todo/TodoService.java


    public void editToDo(TodoForm formData) {
        TodoEntity todoEntity = findTodoById(formData.getId());
        todoEntity.setTitle(formData.getTitle());
        todoEntity.setDeadline(formData.getDeadline());
        todoEntity.setStatus(formData.isStatus());
        todoRepository.save(todoEntity);
    }

Get TODO from id and rewrite the value to formData which is the edited content.

Since there is no return value this time, it is set to void.

This completes the implementation of the editing process!

Recommended Posts

Let's create a TODO application in Java 8 Implementation of editing function
Let's create a TODO application in Java 6 Implementation of search function
Let's create a TODO application in Java 1 Brief explanation of MVC
Let's create a TODO application in Java 5 Switch the display of TODO
Implementation of like function in Java
Let's create a TODO application in Java 11 Exception handling when accessing TODO with a non-existent ID
Create a TODO app in Java 7 Create Header
Let's create a super-simple web framework in Java
[Rails] Implementation of retweet function in SNS application
Let's create a TODO application in Java 13 TODO form validation 1: Character limit ・ Gradle update to use @Validated
Let's create a TODO application in Java 3 Save temporary data in MySQL-> Get all-> Display on top
Let's make a calculator application with Java ~ Create a display area in the window
Implementation of gzip in java
Implementation of tri-tree in Java
Let's create a TODO application in Java 9 Create TODO display Sort by date and time + Set due date default to today's date
Let's create a TODO application in Java 2 I want to create a template with Spring Initializr and make a Hello world
Let's make a calculator application in Java ~ Display the application window
Let's create a TODO application in Java 12 Processing when a request comes in with an unused HttpMethod ・ Processing when an error occurs in the server
[Implementation procedure] Create a user authentication function using sorcery in Rails
Implementation of DBlayer in Java (RDB, MySQL)
Let's create a Java development environment (updating)
Role of JSP in Web application [Java]
Volume of trying to create a Java Web application on Windows Server 2016
Create authentication function in Rails application using devise
Create a CSR with extended information in Java
Do you need a memory-aware implementation of Java?
Let's create a timed process with Java Timer! !!
Measure the size of a folder in Java
Try to create a bulletin board in Java
Let's create a custom tab view in SwiftUI 2.0
[Java] Let's create a mod for Minecraft 1.14.4 [Introduction]
[JQuery] Implementation procedure of AutoComplete function [Java / Spring]
[Java] Let's create a mod for Minecraft 1.16.1 [Introduction]
A quick review of Java learned in class
[Java] Let's create a mod for Minecraft 1.14.4 [99. Mod output]
Create a native extension of Ruby in Rust
Let's create a versatile file storage (?) Operation library by abstracting file storage / acquisition in Java
[Java] Let's create a mod for Minecraft 1.14.4 [0. Basic file]
Interpreter implementation in Java
[Java] Let's create a mod for Minecraft 1.14.4 [4. Add tools]
How to create a Java environment in just 3 seconds
A quick review of Java learned in class part4
[Java] Create a filter
[Java] Let's create a mod for Minecraft 1.14.4 [5. Add armor]
Let's write a Qiita article in org-mode of Emacs !!
[Java] Let's create a mod for Minecraft 1.14.4 [Extra edition]
[Java] Let's create a mod for Minecraft 1.14.4 [7. Add progress]
[Java] Let's create a mod for Minecraft 1.14.4 [6. Add recipe]
[Java] Let's create a mod for Minecraft 1.16.1 [Add item]
Implementation of search function
[Java] Let's create a mod for Minecraft 1.16.1 [Basic file]
I tried to create a Clova skill in Java
[Java] Let's create a mod for Minecraft 1.14.4 [1. Add items]
How to create a data URI (base64) in Java
Boyer-Moore implementation in Java
Let's make a robot! "A simple demo of Java AWT Robot"
I tried to make a login function in Java
Heapsort implementation (in java)
A quick review of Java learned in class part3
Implementation of pagination function
A quick review of Java learned in class part2