티스토리 뷰

처음 하는 스프링 부트(Spring Boot), 프로젝트 생성하기에 이어서.

이번엔 지난 포스팅에서 만들었던 프로젝트에 간단한 화면을 붙여보도록 하겠습니다.

1. 화면 구성하기

지난 포스팅에서 다뤘던 내용을 똑같이 재현해보려고 합니다.
하지만 부트에서 JSP를 사용하려면 조금 더 손을 댈 곳이 있는데요.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
plugins {
    id 'org.springframework.boot' version '2.2.5.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
}
 
group = 'com.begin'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
 
repositories {
    mavenCentral()
}
 
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
 
    implementation 'javax.servlet:jstl'
    implementation 'org.apache.tomcat.embed:tomcat-embed-jasper'
 
    runtimeOnly 'mysql:mysql-connector-java'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}
 
test {
    useJUnitPlatform()
}
cs

먼저, build.gradle 에 jstl과 tomcat-embed-jasper 를 추가합니다.
부트에서 JSP 를 사용하기 위해서 추가해줘야 하는 dependency 들인데요.
필요하다면 자세한 내용은 나중에 살펴보기로 하고, 일단은 그냥 추가하고 넘어가기로 하죠.

build.gradle 에 의존성을 추가한 다음엔 항상 Refresh 를 해줘야 추가된 라이브러리를 내려받을 수 있습니다.

다음은 스프링에서 했던 것처럼 JSP viewResolver 설정을 다시 해주도록 합니다.
설정은 정말 간단하게 application 이름으로 된 properties 혹은 yml 파일에 작성하면 되는데요.
저는 yml 파일을 더 선호하니까 yml 파일로 바꾸고 작성을 했습니다.

이제 위에서 설정한 경로대로 jsp 디렉토리를 만들고, 지난번처럼 index.jsp 와 sample.jsp 를 만들었습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <strong>처음 시작하는 index.jsp</strong><br/>
    <div>
        <form action="/sample.bgn" method="post">
            <button onclick="submit">처음 만든 버튼</button>
        </form>
    </div>
</body>
</html>
cs

index.jsp 입니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>
    Begin Project!  
</h1>
<P>  "처음 만든 버튼"을 누르셨습니다! </P>
</body>
</html>
cs

sample.jsp 입니다.

2. Controller 만들기

이제 화면 띄울 준비를 마쳤으니, 서버로 호출이 들어왔을 때 화면까지 연결해줄 Controller 를 만들어보도록 할게요.

먼저 프로젝트에 sample.controller 를 생성했습니다.
참고로 sample 패키지 하위에 controller 패키지를 만들더라도, 패키지가 한 개뿐일 경우엔 이어져서 보이게 됩니다.
sample 패키지 하위에 dao, service 등 패키지를 하나 더 만들면 자연스럽게 나뉘니 이상하게 생각하지 않아도 괜찮아요.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.begin.second.sample.controller;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
 
@Controller
public class SampleController {
 
    @RequestMapping("/sample.bgn")
    public String sampleView() {
        return "sample";
    }
    
}
cs

그리고 지난번과 똑같은 SampleController 를 만들었습니다.

이제 다시 서버를 실행시키고, 이번엔 화면을 띄워보았습니다.
많이 보던 익숙한 화면이죠.

버튼을 누르면 화면으로도 잘 넘어가지네요.

이렇게 간단히 부트를 통해서 화면까지 만들어봤습니다.
다음번엔 DB 를 연결해보도록 하겠습니다.

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함