Memo after the first Spring project-MVC-

Introduction

Only the basic knowledge of pure Java and DB The Spring project from the start has settled down after the turbulence, so please give us your impressions and memorandum. The content of the article is more than a tutorial and less than a practice. : speech_balloon: The posted code has not been confirmed to work, so it is for reference only. : pray: English is slippery, so Japanese is diverse, forgive me. : pray :: sob :: pray:

Development environment

Red Hat (development environment is Cent) 7 series Java8 SpringBoot2.1.6 MySQL8 series STS

About MVC

Understanding that let's design separately the logic (Model), the screen (View) and the controller (Controller) that connects them. But in Spring, the data exchanged between View and Controller is called Model. I was worried if I could understand MVC in the first place, but I stopped thinking deeply because the definition of words is different for each framework. : innocent: https://qiita.com/yo1000/items/a6acbf5f454a7f53aef9 https://qiita.com/takasek/items/70ab5a61756ee620aee6

controller

·mapping Add @GetMapping ("test") to the method. @RequestMapping ("/ test") seems to be old.

It seems that what is described in parentheses is called an attribute. The above is an abbreviation for @GetMapping (path =" test "). When you want to POST by arranging multiple buttons in one form tag, you can map it with the params attribute of the controller by specifying name in html.

view.html


<form method="POST" action="path1">
    <input type="submit" name="param1" />← Press here
    <input type="submit" name="param2" />
</form>

controller.java


@PostMapping(path="path1",params="param1")

** ・ Validation ** It is provided as a function of Spring. If you can operate BindingResult, you can implement validation that is not bound by the framework. But if you make full use of annotations and custom validation, you can do most of the things. https://qiita.com/yakumo3390/items/4e47930ba643b45b7430

Messages are also provided with embedded characters and multilingual support in messages.properties, but I could not use it because it was a requirement to manage messages in DB.

By adding a group attribute, you can specify or exclude validation targets. I made an empty class for this, but I feel like there is a smarter way. https://tech.asoview.co.jp/entry/2019/12/11/104928

** ・ return ** Basically, it is a character string that represents the Path of View. There are also redirect and forward. At the time of redirect, if you pack the value in RedirectAttributes, it will reach the transition destination. http://www.ne.jp/asahi/hishidama/home/tech/java/spring/boot/web/Controller.html#h_forward_attribute

When passing an object, if toString () is implemented, it can be passed as it is, otherwise it seems to be a hassle. https://grandbig.github.io/blog/2016/05/28/redirect-parameter-spring-boot/

About views

Use thymeleaf (time leaf). Difficult to read and spell. : kissing: So-called template. Please note that if you do not check not only the screen but also the generated html, it will be a cool description.

Although th: field is convenient, it may not be used when combined with JavaScript that wants to specify name and id. It might be good to understand what you are doing. https://qiita.com/beeeyan/items/5bd820e55cb53f176b3e

Be careful when using variables in JavaScript. Exceptions are hard to find, and if you use the whole object, it will be expanded on html and it is not good for security, and you will be surprised when you see it. [Official Document 12.2 Inline Script Processing (JavaScript and Dart)](https://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf_ja.html#%E3%82%B9%E3%82%AF%E3% 83% AA% E3% 83% 97% E3% 83% 88% E3% 81% AE% E3% 82% A4% E3% 83% B3% E3% 83% A9% E3% 82% A4% E3% 83% B3% E5% 87% A6% E7% 90% 86-javascript-% E3% 81% A8-dart)

About service

Where to write business logic.

This time, in principle, one service was created for one table. And some general-purpose classes. The purpose was to narrow down the layers that can access the DB, but then one class swelled up. : frowning2: It may be better to create one service for one screen and process the data acquired from the DB with Repository. Or do you add a layer that describes the logic for one screen? .. .. ?? : confused: Untitled Diagram.png

Other notes

Memo after the first Spring project-What is Spring- Memo after the first Spring project-Database-

Recommended Posts

Memo after the first Spring project-MVC-
Memo after the first Spring project-Database-
Spring Boot for the first time
Spring AOP for the first time
Spring retrospective memo
JJUG CCC Spring 2018 memo
Spring Shell usage memo
Spring boot memo writing (1)
First Spring Boot (DI)
Spring boot memo writing (2)
Learning memo when learning Java for the first time (personal learning memo)
Books read after the first year engineer joined the company
The first WEB application with Spring Boot-Making a Pomodoro timer-
Spring Security usage memo CSRF
Spring Framework self-study memo series_1
Spring Security usage memo Run-As
This is the first post.
Spring Security usage memo Remember-Me
Try the Spring WebFlux tutorial
Spring Security usage memo CORS
Spring Security usage memo test
Spring boot controller method memo