[JAVA] Try Spring Boot from 0 to 100.

I decided to use it for business and started studying. He is a complete amateur and is often wrong. Updated from time to time. ]

Part 1 "Display index.html with Springboot"

-I'm using an MVC model. ・ Implemented at STS. -Create a spring starter project for Package Explorer. -Select SpringBootDevTools, springweb and Thymeleaf as dependencies -A package is created in src / main / java and application.java is created by default. Since main is included in application.java, execution starts from here ・ Put the controller in the package ・ View is placed in src / main / resources / template.

First controller Write @ Controller because this is a controller Respond to requests that want to connect to the context root (http : // localhost: 0808 /) using the @ RequestMapping annotation.

@Controller
public class ClassName {
		 @RequestMapping(value = "/")
}
	  

Commentary: It's a controller with @controller @ RequestMapping (value = "slashes represent context root")

@ RequestMapping starts work when the value path matches the request. The job is to move the method. Since there is no method as it is Method added! !!

@Controller
public class ClassName {
		 @RequestMapping(value = "/")

  public String respons() {
		   return "index.html";
  }

}

Commentary: @ RequestMapping (value = "/") followed by responds (any name) method works

 public String void respons() {
		   return "index.html";
 }

By setting return to index.html, index.html will be displayed in the browser. Of course, if you change this to main.html, main.html under template will be displayed.

By the way, index.html can be omitted.

 public String respons() {
		   return "index";
 }

Part 2 "Try using Tymeleaf"
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1 th:text="${res}"></h1>
</body>
</html>

Commentary: I copied the html tag. By writing this, you can use the th option in the h1 tag. The contents of "$ {res}" are displayed in the browser Write the contents of "res" in java

Next is java

package com.samle.spring;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class Sample {

	@RequestMapping(value="/")
	public ModelAndView sample() {
		ModelAndView mod = new ModelAndView();
		mod.setViewName("sample");
		mod.addObject("res", "hello world");
		return mod;
	}
}

The difference from Part 1 is the guy called Model And View The sample method will return this mod.

In other words, the value packed in the mod is displayed in the browser.

Where do you return the mod first? Describe where to return This time it's sample.html

mod.setViewName("sample");

Next, do two things at the same time

  1. Since the mod is still empty, fill in the values.
  2. Decide where to display the packed values in sample.html
mod.addObject("res", "hello world");

In the first argument, "res" was html, "$ res" The second one contains the characters to be displayed.

Hopefully Hello World should be displayed

Part 3 "Dynamic display using request parameters"

Next, use submit to change the display. It's getting more complicated, so let's take a look at it step by step.

1. Enter the context root in the URL field. Receive a request with sample.java
@Controller
public class Sample {
	
	@RequestMapping(value="/")
	public String index() {
		return "sample";
	}

Call sample.html in the same way as part 1.

2.html is displayed
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

 <form action="/req">
  	<p>Surname</p><input type="text" name="firstName"><br>
  	<p>name</p><input type="text" name="lastName">
  	<button type="submit">Send</button>
  </form>

 <h1 th:text="${res}"></h1>
</body>
</html>

At this stage, th: text = "$ {res}" is empty and will not be displayed.

3. Put characters in text and submit

Since the action of the form tag is / req, set it in java and receive the input

4. Receive the submitted value
@RequestMapping(value="/req")
	   public ModelAndView respons(@RequestParam("firstName") String first,
			                       @RequestParam("lastName") String last) {

		   ModelAndView mod = new ModelAndView();
		   mod.setViewName("sample");
		   mod.addObject("res", "full name" + first + " " + last + "Mr.");
		   
		   return mod;
	   }

When you receive it, you can receive it by doing @ RequestMapping (value = "/ req).

The rest is the same as what I did in Part 2.

If you can give your full name, you will succeed!

package com.samle.spring;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class Sample {
	
	@RequestMapping(value="/")
	public String index() {
		return "sample";
	}

	@RequestMapping(value="/req")
	   public ModelAndView respons(@RequestParam("firstName") String first,
			                       @RequestParam("lastName") String last) {

		   ModelAndView mod = new ModelAndView();
		   mod.setViewName("sample");
		   mod.addObject("res", "full name" + first + " " + last + "Mr.");
		   
		   return mod;
	   }
}
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

 <form action="/req">
  	<p>Surname</p><input type="text" name="firstName"><br>
  	<p>name</p><input type="text" name="lastName">
  	<button type="submit">Send</button>
  </form>

 <h1 th:text="${res}"></h1>
</body>
</html>

It is possible to separate by writing two @ RequesrMapping in the sample class.

Part 4 "One argument for java" At this rate, if there are 30 inputs, the arguments will also be 30. @ModelAttribute is used in such a case Use as follows
package com.samle.spring;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class Sample {
	
	
	@RequestMapping(value="/")
	public String index() {
		return "sample";
	}
	
	


/*
New content from here
*/
	@RequestMapping(value="/req")
	   public ModelAndView respons(@ModelAttribute Param p) {

		   ModelAndView mod = new ModelAndView();
		   mod.setViewName("sample");
		   mod.addObject("res", p);
		   
		   return mod;
	  }
	
	
	public static class Param{
		private String firstName;
		private String lastName;
			
		public String getFirstName(){ 
			return firstName; 
		}
		
		public String getLastName(){
			return lastName; 
		}
		
		public void setFirstName(String firstName) {
			this.firstName = firstName;
		}
		
		public void setLastName(String lastName) {
			this.lastName = lastName;
		}
	 }
}

Explanation: ```java @RequestMapping(value="/req") public ModelAndView respons(@ModelAttribute Param p) {
	   ModelAndView mod = new ModelAndView();
	   mod.setViewName("sample");
	   mod.addObject("res", p);
	   
	   return mod;
  }
 If modelAttribute is written in the argument, an object equal to the name after modelAttribute will be New.
 With the above code, at the same time as the p instance of the param class is new
 Set the value of the request parameter
 Then pack the p instance into a mod

 To recall the contents, write "$ {res.name}"
 Next is html

```html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
 <form action="/req">
  	<p>Surname</p><input type="text" name="firstName"><br>
  	<p>name</p><input type="text" name="lastName">
  	<button type="submit">Send</button>
  </form>

  <div th:if="${res}">
  <h1 th:text=" 'you are' + ${res.firstName} + ' ' + ${res.lastName} + 'is not it' "></h1>
  </div>
</body>
</html>

The difference from Part 3 is here

<div th:if="${res}">
  <h1 th:text=" 'you are' + ${res.firstName} + ' ' + ${res.lastName} + 'is not it' "></h1>
  </div>

th: if is false if the content of res is null and is not read in the div tag. The reason for doing this is Since the contents of $ {res.firstName} are empty when first displayed in the context root Because it will result in an error. Must be enclosed in if to prevent $ {res.firstName} from being read

Recommended Posts

Try Spring Boot from 0 to 100.
Upgrade spring boot from 1.5 series to 2.0 series
Story when moving from Spring Boot 1.5 to 2.1
Changes when migrating from Spring Boot 1.5 to Spring Boot 2.0
Changes when migrating from Spring Boot 2.0 to Spring Boot 2.2
Try to implement login function with Spring Boot
Try to automate migration with Spring Boot Flyway
Introduction to Spring Boot ① ~ DI ~
Introduction to Spring Boot ② ~ AOP ~
Introduction to Spring Boot Part 1
Try using Spring Boot Security
Try Spring Boot on Mac
How to set Spring Boot + PostgreSQL
How to use ModelMapper (Spring boot)
Try running Spring Boot on Kubernetes
Spring Boot starting from zero Part 2
Spring Boot starting from zero Part 1
The story of raising Spring Boot from 1.5 series to 2.1 series part2
[Introduction to Spring Boot] Form validation check
02. I made an API to connect to MySQL (MyBatis) from Spring Boot
Change Spring Boot REST API request / response from CamelCase to SankeCase
Challenge Spring Boot
Try using Spring Boot with VS Code
What I did in the migration from Spring Boot 1.4 series to 2.0 series
Transition from Struts2 to Spring MVC (Controller)
What I did in the migration from Spring Boot 1.5 series to 2.0 series
Try Spring Boot 1 (Environment construction ~ Tomcat startup)
Spring Boot Form
Spring Boot Memorandum
gae + spring boot
How to split Spring Boot message file
Add spring boot and gradle to eclipse
[Reverse lookup] Spring Security (updated from time to time)
Use Thymeleaf text template mode from Spring Boot
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
How to use built-in h2db with spring boot
How to make Spring Boot Docker Image smaller
Try calling the CORBA service from Spring (Java)
Rails Tutorial Chapter 1 From Zero to Deployment [Try]
How to use Spring Boot session attributes (@SessionAttributes)
The story of raising Spring Boot 1.5 series to 2.1 series
Try LDAP authentication with Spring Security (Spring Boot) + OpenLDAP
How to add a classpath in Spring Boot
An introduction to Spring Boot + in-memory data grid
Touch all Spring "Guides" (updated from time to time)
How to bind to property file in Spring Boot
[Java] Article to add validation with Spring Boot 2.3.1.
I wanted to gradle spring boot with multi-project
Try to display hello world with spring + gradle
Apply Twitter Bootstrap 4 to Spring Boot 2 using Webjars
◆ Get API created by Spring Boot from React
[Spring Boot] How to refer to the property file
[Introduction to Spring Boot] Authentication function with Spring Security
Spring Boot --How to set session timeout time
SPRING BOOT learning record 01
Changes from Java 8 to Java 11
Spring Boot + Heroku Postgres
Sum from Java_1 to 100
Try to release gem
1. Start Spring framework from 1
Migrate from JUnit 4 to JUnit 5