Créer un spring boot avec Eclipse (créer et configurer le contrôleur)

1. Créez des documents

Ouvrez eclipse -> fichier -> nouveau -> projet Spring Starter dans la colonne supérieure,
Insérez la description de l'image ici
puis
Insérez la description de l'image ici
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
Insérez la description de l'image ici
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,
Insérez la description de l'image ici
faites un clic droit -> exécuter en tant que -> Spring boot App
, entrez dans le navigateur: http://127.0.0.1:8080/hello La
Insérez la description de l'image ici
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 .
Insérez la description de l'image ici

Je suppose que tu aimes

Origine blog.csdn.net/weixin_45743162/article/details/111603302
conseillé
Classement