Create a spring boot with Eclipse (create and configure the controller)

1. Create documents

Open eclipse -> file ->new ->Spring Starter project in the upper column,
Insert picture description here
then next
Insert picture description here
only check Spring Web ->Finish

2. Create Controller

Create a folder controller in the demo folder. Create
Insert picture description here
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,
Insert picture description here
right-click ->run as ->Spring boot App
, enter in the browser: http://127.0.0.1:8080/hello The
Insert picture description here
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 .
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45743162/article/details/111603302
Recommended