1. Create documents
Open eclipse -> file ->new ->Spring Starter project in the upper column,
then next
only check Spring Web ->Finish
2. Create Controller
Create a folder controller in the demo folder. Create
a file testController.class in the controller folder. The
code is as follows:
package com.example.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class testController {
@RequestMapping("/hello")
public String hello() {
return "Hello Spring Boot!";
}
}
Open the file DidididiApplication under the demo,
right-click ->run as ->Spring boot App
, enter in the browser: http://127.0.0.1:8080/hello The
configuration is successful.
3. Open the page to try
Add thymeleaf dependency in pom.xml
<!--使用thymeleaf所需依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
ps: <dependencies> </dependencies>
Add inside to
create a new controller, named helloController
code:
package com.example.demo.controller;
import java.util.HashMap;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class indexController {
@RequestMapping("/index")
public String getIndex(HashMap<String,Object> map,Model model) {
model.addAttribute("hello", "你好小明");
map.put("name", "小明");
map.put("password", "1234");
return "hello";
}
}
Create an html file (named hello.html, corresponding to return in helloController):
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div>
<p th:text='${name}'></p>
<p th:text='${password}'></p>
<p th:text='${hello}'></p>
</div>
</body>
</html>
Restart the server and open it with a browser: http://localhost:8080/index .