Eclipse로 스프링 부트 생성 (컨트롤러 생성 및 구성)

1. 문서 작성

Eclipse-> file-> new-> Spring Starter 프로젝트를 상단 열에서 열고
여기에 사진 설명 삽입
다음으로
여기에 사진 설명 삽입
Spring Web-> Finish 만 확인하십시오.

2. 컨트롤러 생성

데모 폴더에 폴더 컨트롤러를 만들고
여기에 사진 설명 삽입
컨트롤러 폴더에 testController.class 파일을 만듭니다 .
코드는 다음과 같습니다.

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!";
	    }
}

데모에서 DidididiApplication 파일을 열고
여기에 사진 설명 삽입
마우스 오른쪽 버튼으로 클릭-> 다음 계정으로 실행-> Spring boot App을 클릭
하고 브라우저에 다음을 입력합니다. http://127.0.0.1:8080/hello
여기에 사진 설명 삽입
구성이 성공적입니다.

3. 시도 할 페이지를 엽니 다.

pom.xml에 thymeleaf 종속성 추가

		<!--使用thymeleaf所需依赖 -->
		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>

추신 : <dependencies> </dependencies>내부를 추가하여
helloController
코드 라는 새 컨트롤러 만듭니다 .

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";
	}
}

html 파일 (helloController의 return에 해당하는 hello.html이라는 이름)을 만듭니다.

<!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>

서버를 다시 시작하고 브라우저로 엽니 다 : http : // localhost : 8080 / index .
여기에 사진 설명 삽입

추천

출처blog.csdn.net/weixin_45743162/article/details/111603302