m1ndy5's coding blog

View로 서버 잘 돌아가는지 확인하기 본문

백엔드 with java/spring

View로 서버 잘 돌아가는지 확인하기

정민됴 2023. 3. 7. 22:08

/src/main/resources/static에 index.html파일을 만들면 서버를 실행했을 때 index.html에 들어가 있는 내용들이 나온다.
https://docs.spring.io/spring-boot/docs/2.3.1.RELEASE/reference/html/spring-boot-features.html#boot-features-webflux-welcome-page
It first looks for an index.html file in the configured static content locations. -> static에서 index.html을 먼저 찾는다.
메뉴얼에서 검색하는 방법을 알아두면 좋다.

/hello.hellospring/controller/HelloController

@Controller //컨트롤러 어노테이션을 붙어야 컨트롤러라고 인식이됨
public class HelloController {

    @GetMapping("hello") //hello라는 경로로 들어오게 되면
    public String hello(Model model){
        model.addAttribute("data", "hello!!"); //data라는 모델에 hello라는 값을 집어넣음
        return "hello"; //hello라는 템플릿을 찾음
    }
}

/resources/templates/hello.html

<!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}" >안녕하세요. 손님</p> //data에 저장된 값이 출력됨
</body>
</html>
  • 컨트롤러에서 리턴값으로 문자를 반환하면(ex. hello) viewResolver가 화면을 찾아서 처리
    • resources/templates/{ViewName(ex. hello)}+.html 로 찾아줌

실행하기

Intellij에서 run


@SpringBootApplication 어노테이션이 붙은 클래스를 run해주면 된다.

console에서 build

프로젝트 파일까지 들어간 후 아래의 방법을 따르면 된다.

For Linux

  1. ./gradlew build

For Window

  1. gradlew build

공통
2. cd build/libs
3. java -jar hello-spring-0.0.1-SNAPSHOT.jar
4. 실행 확인

실행시킬 때 가장 중요한 것!! 같은 포트에 서버 2개 못띄우기 때문에 이전 서버를 종료시켜줄 것!
만약 8080에 돌아가고 있어서 못한다면 ps -ef 에서 pid확인 후 kill -9 pid해서 죽여버리자!!ㅎㅎ