I decided to use it for business and started studying. He is a complete amateur and is often wrong. Updated from time to time. ]
-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";
}
<!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
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
Next, use submit to change the display. It's getting more complicated, so let's take a look at it step by step.
@Controller
public class Sample {
@RequestMapping(value="/")
public String index() {
return "sample";
}
Call sample.html in the same way as part 1.
<!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.
Since the action of the form tag is / req, set it in java and receive the input
@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.
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;
}
}
}
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