Let's create a TODO application in Java 5 Switch the display of TODO

Hello, this time by pressing the button TODO of completion ⇄ incomplete we want to implement the ability to switch.

TODO application creation link collection

1: Brief description of MVC 2: I want to make a template with Spring Initializr and make a Hello world 3: Save temporary data in MySQL-> Get all-> Display on top 4: Implementation of posting function 5: Switch the TODO display (here and now)

Search for a specific TODO by id

First of all, the flow of this process ・ Search and obtain TODO from id -Switch TODO status with true ⇄ false ・ Save TODO It will be.

So let's do the part to get TODO from id immediately.

java/com/example/todo/TodoService.java


    public TodoEntity findTodoById(Long todoId) {
        Optional<TodoEntity> todoResult = todoRepository.findById(todoId);
        //Check for null
        return todoResult.get();
    }

It should be like this.

The Optional type used here is a type that explicitly indicates that the type contained in it may be null.

//nullチェックする I will leave the part that is marked as it will be done in a future article.

This time it is of type Optional You can change it to TodoEntity type by setting Optional .get ().

Now you can get TODO by id!

Implementation of logic to switch between completed and incomplete

Next, we will implement the logic to switch the TODO state.

java/com/example/todo/TodoService.java


    public void switchTodo(Long todoId) {
        TodoEntity todoEntity = findTodoById(todoId);
        todoEntity.setStatus(!todoEntity.isStatus());
        todoRepository.save(todoEntity);
    }

First, get the TODO by calling the previous function. And I switch the status, but by doing! TodoEntity.isStatus (), it is in the opposite state to the current state. (This is a common writing style when switching booleans.)

Edit controller

java/com/example/todo/TodoController.java


    @PatchMapping("/toggle-status/{id}")
    public String switchStatus(@PathVariable("id") Long todoId) {
        todoService.switchTodo(todoId);
        return "redirect:/top";
    }

Let's write like this.

@PathVariable is an annotation that gets the {id} embedded in the URL.

Front side editing

<!DOCTYPE html>
<html lang="ja" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>hello</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>

    <!--Post form-->
    <div class=" w-75 h-auto my-1 mx-auto pt-5">
        <p class="pl-5">Create a new task</p>
        <form th:action="@{/register}" th:object="${ToDoForm}" 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" placeholder="Enter ToDo within 30 characters" class="col-9">
                </label>
                <label class="row my-0">
                    <span class="col-2 text-center">Deadline</span>
                    <input type="date" id="date" name="deadline" class="col-9 my-0">
                </label>
            </div>
            <button class="btn btn-primary w-25 col-2 mr-3" type="submit">Add ToDo</button>
        </form>
    </div>
    <div th:each="todo: ${todoList}" class=" w-75 h-25 my-1 mx-auto pt-5">
        <div class="container">
            <div  class="row">
                <div class="col-5 pl-5">
                    <p th:text="${todo.title}" class="mb-1"></p>
                    <p class="mb-1">Deadline:<span  th:text="${todo.deadline}"></span></p>
                    <p class="mb-1">Creation date:<span  th:text="${todo.createTime}"></span></p>
                </div>
                <div class="col-2 d-flex justify-content-start align-items-center px-0">
                    <a class="h-100 w-75 btn btn-info pt-4">
Edit
                    </a>
                </div>
<!--〜〜〜〜〜〜 Contents to be added this time〜〜〜〜〜〜-->
                <div th:if="${todo.status}" class="col-3 d-flex px-0">
                    <form th:action="@{/toggle-status/{id}(id=${todo.id})}" method="post" class="w-100 container d-flex my-0 mx-auto p-0 mr-2">
                        <input type="hidden" name="_method" value="patch">
                        <button type="submit" class="h-100 w-75 btn btn-success text-white">
Done
                        </button>
                    </form>
                </div>
                <div th:unless="${todo.status}" class="col-3 d-flex px-0">
                    <form th:action="@{/toggle-status/{id}(id=${todo.id})}" method="post" class="w-100 container d-flex my-0 mx-auto p-0 mr-2">
                        <input type="hidden" name="_method" value="patch">
                        <button type="submit" class="h-100 w-75 btn btn-danger text-white">
Incomplete
                        </button>
                    </form>
                </div>
<!--〜〜〜〜〜〜 Contents to be added this time 〜〜〜〜〜〜-->

            </div>
        </div>
    </div>

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" 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>

th:if="${todo.status}"

However, this means that if the status is true, that is, it has been completed. It is displayed as completed in green.

Also, although method = "post" is set, @PatchMapping was used in the previous controller editing.

In fact, HTML doesn't support the ability to request a patch. So if you want to make a request with Patch, you need to devise something.

In Thymeleaf

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

If you want to

in application.property

spring.mvc.hiddenmethod.filter.enabled=true

If you add, type = "hidden" can be used and Patch requests can be sent.

Controller, Service, TodoEntity Summary

Contoller


package com.example.todo;

import com.example.todo.dao.TodoEntity;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

@Controller
@RequiredArgsConstructor
public class TodoController {

    private final TodoService todoService;
    @GetMapping("/top")
    public String top(Model model){
        List<TodoEntity> todoEntityList = todoService.findAllTodo();
        model.addAttribute("todoList", todoEntityList);
        return "top";
    }

    @PostMapping("/register")
    public String register(@ModelAttribute TodoForm formData) {
        todoService.setTodo(formData);
        return "redirect:/top";
    }

    @PatchMapping("/toggle-status/{id}")
    public String switchStatus(@PathVariable("id") Long todoId) {
        todoService.switchTodo(todoId);
        return "redirect:/top";
    }
}

Service


package com.example.todo;

import com.example.todo.dao.TodoEntity;
import com.example.todo.dao.TodoRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
public class TodoService {
    private final TodoRepository todoRepository;

    public List<TodoEntity> findAllTodo() {
        return todoRepository.findAll();
    }

    public void setTodo(TodoForm formData) {
        TodoEntity todo = new TodoEntity();
        todo.setTitle(formData.getTitle());
        todo.setDeadline(formData.getDeadline());
        todoRepository.save(todo);
    }

    public TodoEntity findTodoById(Long todoId) {
        Optional<TodoEntity> todoResult = todoRepository.findById(todoId);
        //Exception handling
        return todoResult.get();
    }

    public void switchTodo(Long todoId) {
        TodoEntity todoEntity = findTodoById(todoId);
        todoEntity.setStatus(!todoEntity.isStatus());
        todoRepository.save(todoEntity);
    }
}

Entity


package com.example.todo.dao;

import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import java.time.LocalDate;
import java.time.LocalDateTime;

@Entity
@Getter
@Setter
@Table(name="todo")
public class TodoEntity {
    @Id
    @Column
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name="title")
    private String title;

    @Column(name="deadline")
    private LocalDate deadline;

    @Column(name="status")
    private boolean status;

    @CreationTimestamp
    @Column(name="create_time")
    private LocalDateTime createTime;

    @UpdateTimestamp
    @Column(name="update_time")
    private LocalDateTime updateTime;
}

The current TODO app looks like the one above.

Recommended Posts

Let's create a TODO application in Java 5 Switch the display of TODO
Let's create a TODO application in Java 4 Implementation of posting function
Let's create a TODO application in Java 8 Implementation of editing function
Let's create a TODO application in Java 1 Brief explanation of MVC
Let's make a calculator application in Java ~ Display the application window
Let's make a calculator application with Java ~ Create a display area in the window
Let's create a TODO application in Java 3 Save temporary data in MySQL-> Get all-> Display on top
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 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 12 Processing when a request comes in with an unused HttpMethod ・ Processing when an error occurs in the server
Measure the size of a folder in Java
Let's create a super-simple web framework in Java
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 2 I want to create a template with Spring Initializr and make a Hello world
Create a method to return the tax rate in Java
[Java] Let's take a look at Switch Expressions (Preview) of JDK 13.
Get the public URL of a private Flickr file in Java
Install Rails in the development environment and create a new application
Get the result of POST in Java
Let's create a Java development environment (updating)
The story of writing Java in Emacs
Role of JSP in Web application [Java]
Dynamically increase the number of elements in a Java 2D array (multidimensional array)
The story of forgetting to close a file in Java and failing
Let's express the result of analyzing Java bytecode with a class diagram
Sample program that returns the hash value of a file in Java
How to get the absolute path of a directory running in Java
Volume of trying to create a Java Web application on Windows Server 2016
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
The story of making ordinary Othello in Java
Create a CSR with extended information in Java
About the idea of anonymous classes in Java
Let's create a timed process with Java Timer! !!
A story about the JDK in the Java 11 era
The story of learning Java in the first programming
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]
[Java] Let's create a mod for Minecraft 1.16.1 [Introduction]
A quick review of Java learned in class
Display "Hello World" in the browser using Java
Display "Hello World" in the browser using Java
[Java] Let's create a mod for Minecraft 1.14.4 [99. Mod output]
Import files of the same hierarchy in Java
Create a native extension of Ruby in Rust
I received the data of the journey (diary application) in Java and visualized it # 001
Let's refer to C ++ in the module of AndroidStudio other project (Java / kotlin)
Let's create a versatile file storage (?) Operation library by abstracting file storage / acquisition in Java
Create a Yellowfin custom formatter and display the minus of the number as △ (triangle)
Validate the identity token of a user authenticated with AWS Cognito in Java
[Java] Let's create a mod for Minecraft 1.14.4 [0. Basic file]
Get the URL of the HTTP redirect destination in Java
[Java] Let's declare variables used in the loop in the loop [Variables in the block]
Count the number of occurrences of a string in Ruby
[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]