티스토리 뷰

Spring Boot

[JAVA Spring] View 환경설정

JaeGuin 2021. 1. 25. 15:58

기존의 run server:80를 해주면 

 

 

같은 아무것도 없는 에러페이지가 나온다.

 

 

이번에는 View기능을 통해 welcomePage를 만들어보겠다.

 

 

 

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Hello</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'안녕하세요. ' + ${data}" >안녕하세요. spring</p>
</body>
</html>

/resources/static/index.html 로 설정해두면

첫 화면이 흰바탕에 '안녕하세요. spring'으로 나타날 것이다.

 

 

이 페이지는 html을 그냥 서버에 올려준 정적 페이지이다.

 

 

 

 

 

 

 

이미지를 클릭하면 해당 페이지로 이동

 

스프링은 워낙에 방대한 양을 담고 있기에 구현 방법을 찾는게 중요하다.

정보가 필요하면 공식페이지에서 정보를 찾도록 하자.

 

 

 

 

이번에는 정적인 페이지가 아닌 동작하고 프로그래밍된 화면을 만들어 보겠다.

 

 

 

hello.hellospring 하위에 새 패키지를 만들어주자.

 

 

package hello.hellospring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller //프레임워크를 연결해줄 컨트롤러
public class HelloController {

    @GetMapping("hello") //웹 어플리케이션에서 /hello 라고 주소를 입력하면 이 메서드 호출
    public String hello(Model model){
        model.addAttribute("data", "spring");//어트리뷰트이름이 'data' 값은 'hello'로 설정
        return "hello";


    }
}

 

 

코드를 작성해주면  http://localhost:8080/hello 에서 홈페이지를 보여준다

 

 

 

+ S{data}를 가져와서 치환이 된다.

 

 

 

 

 

 

 

 

 

 

 

댓글