Botte de printemps
1. Créez des documents
Ouvrez eclipse -> fichier -> nouveau -> projet Spring Starter dans la colonne supérieure,
puis
vérifiez uniquement Spring Web -> Terminer
2. Créer un contrôleur
Créez un contrôleur de dossier dans le dossier de démonstration. Créez
un fichier testController.class dans le dossier du contrôleur. Le
code est le suivant:
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!";
}
}
Ouvrez le fichier DidididiApplication sous la démo,
faites un clic droit -> exécuter en tant que -> Spring boot App
, entrez dans le navigateur: http://127.0.0.1:8080/hello La
configuration est réussie.
3. Ouvrez la page pour essayer
Ajouter une dépendance thymeleaf dans pom.xml
<!--使用thymeleaf所需依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
ps: <dependencies> </dependencies>
Ajoutez à l'intérieur pour
créer un nouveau contrôleur, nommé 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";
}
}
Créez un fichier html (nommé hello.html, correspondant au retour dans 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>
Redémarrez le serveur et ouvrez-le avec un navigateur: http: // localhost: 8080 / index .