์ดํด๋ฆฝ์Šค์—์„œ ์Šคํ”„๋ง๋ถ€ํŠธ ๊ธฐ๋ฐ˜์œผ๋กœ JSP๊ฐœ๋ฐœํ•˜๊ธฐ

์•ˆ๋…•ํ•˜์„ธ์š”, ์˜ค๋Š˜์€ ์Šคํ”„๋ง๋ถ€ํŠธ ๊ธฐ๋ฐ˜์œผ๋กœ JSP๊ฐœ๋ฐœ์„ ํ•  ์ˆ˜ ์žˆ๋Š” ํ™˜๊ฒฝ์„ ์„ธํŒ…ํ•˜๋Š” ๊ณผ์ •์„ ์•Œ๋ ค๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

์ตœ๊ทผ์— ๋Œ€๊ทœ๋ชจ์˜ ์›น ๊ฐœ๋ฐœ์€ ๋ฐฑ์—”๋“œ์™€ ํ”„๋ก ํŠธ์—”๋“œ๋ฅผ ๊ตฌ๋ถ„ํ•ด์„œ ๋ณ„๋„์˜ ํ”„๋กœ์ ํŠธ๋กœ ๊ตฌ์„ฑํ•˜๊ฑฐ๋‚˜ ๋ชจ๋“ˆ๋กœ ๋‚˜๋ˆ„์–ด ๊ฐœ๋ฐœ์„ ์ง„ํ–‰ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

์–ด๋–ป๊ฒŒ ๊ฐœ๋ฐœ์„ ํ•˜๋˜์ง€ ๊ฒฐ๊ตญ ๋ฐฑ์—”๋“œ์™€ ํ”„๋ก ํŠธ์—”๋“œ๋ฅผ ์™„์ „ํžˆ ๋ถ„๋ฆฌํ•˜์—ฌ ๊ฐœ๋ฐœํ•˜๊ฒŒ๋˜์ฃ . ํ•˜์ง€๋งŒ ๊ทธ๋ ‡๊ฒŒ๊นŒ์ง€ ํ•  ํ•„์š”๊ฐ€ ์—†๋Š” ์ž‘์€ ํ”„๋กœ์ ํŠธ๋“ค์€ ๊ตณ์ด ๊ทธ๋ ‡๊ฒŒ ๋‚˜๋ˆ„์–ด ๊ฐœ๋ฐœ์„ ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ๊ตฌ๋ถ„์„ ํ•˜๋Š” ๊ฒƒ์ด ์˜คํžˆ๋ ค ์œ ์ง€๋ณด์ˆ˜๋ฅผ ํž˜๋“ค๊ฒŒ ํ•˜๋Š” ์›์ธ์ด ๋˜๊ธฐ๋„ ํ•˜์ฃ .

์˜ค๋Š˜์€ ํ•˜๋‚˜์˜ ์Šคํ”„๋ง๋ถ€ํŠธ ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค๊ณ  JSP๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ธํŒ…ํ•˜๋Š” ๋ถ€๋ถ„๊นŒ์ง€ ์•Œ๋ ค๋“œ๋ฆฝ๋‹ˆ๋‹ค.

๊ฐœ๋ฐœ์— ํ•„์š”ํ•œ ์ค€๋น„๋ฌผ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

1. Eclipse (2020๋…„ 7์›” ๊ธฐ์ค€ ์ตœ์‹ ๋ฒ„์ „ ๋‹ค์šด๋กœ๋“œ)

Version: 2020-09 M1 (4.17.0 M1)

 

2. Spring Tools plugin (์„ค์น˜๋ฐฉ๋ฒ•)

 

์ด๋ ‡๊ฒŒ๋งŒ ์žˆ์œผ๋ฉด ์ผ๋‹จ ์ค€๋น„๋Š” ์™„๋ฃŒ์ž…๋‹ˆ๋‹ค.

 

์ด์ œ ์ƒˆ๋กœ์šด ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ํ˜„์žฌ ์ดํด๋ฆฝ์Šค๋ฅผ ์ฒ˜์Œ ์„ค์น˜ํ•˜์‹  ๋ถ„์ด๋ผ๋ฉด ํŒจํ‚ค์ง€ ํƒ์ƒ‰๊ธฐ(Project Explorer)์— ์•„๋ž˜์™€ ๊ฐ™์ด ๋‚˜์˜ค๋Š”๋ฐ ์—ฌ๊ธฐ์„œ ๋ฐ‘์—์„œ ๋‘ ๋ฒˆ์งธ์— ์žˆ๋Š” Create a project...๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ด๋ฏธ ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค์–ด ๋†“์€๊ฒŒ ์žˆ๋Š” ๋ถ„๋“ค์ด๋ผ๋ฉด ๊ทธ๋ƒฅ ํƒ์ƒ‰๊ธฐ ์ฐฝ์—์„œ ์šฐํด๋ฆญํ•ด์„œ New > Project ๋ฅผ ์„ ํƒํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์•„๋ž˜์™€ ๊ฐ™์ด ์ƒˆ ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ ๋งˆ๋ฒ•์‚ฌ๊ฐ€ ๋œจ๋ฉด spring ์œผ๋กœ ๊ฒ€์ƒ‰์„ ํ•ด์„œ Spring Starter Project๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.

 

์ด์ œ ๋งŒ๋“ค ํ”„๋กœ์ ํŠธ์˜ ์ด๋ฆ„์„ Name ํ•ญ๋ชฉ์— ์ ์–ด์ค๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  Java Version ์€ 11๋กœ ์„ ํƒํ•ด์ค๋‹ˆ๋‹ค. (8๋กœ ํ•ด๋„ ๋ฌด๋ฐฉํ•ฉ๋‹ˆ๋‹ค)

 

Spring Boot์˜ ๋ฒ„์ „์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์ด ๋ถ€๋ถ„์€ ๊ทธ๋Œ€๋กœ ๋†”๋‘๊ณ  Available ๊ฒ€์ƒ‰์ฐฝ์—์„œ web์ด๋ผ๊ณ  ๊ฒ€์ƒ‰ํ•˜์—ฌ Spring Web์„ ์„ ํƒํ•ด์ค๋‹ˆ๋‹ค. (์ด์™ธ์—๋„ lombok์ด๋‚˜ ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค ๋“œ๋ผ์ด๋ฒ„, MyBatis ๋“ฑ ์œ ์šฉํ•œ ๊ธฐ๋Šฅ๋“ค์„ ์„ ํƒํ•˜์—ฌ ์‚ฌ์ „์„ค์น˜๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค๋งŒ ์—ฌ๊ธฐ์„œ๋Š” ์„ ํƒํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.)

 

Finish ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ตฌ์กฐ๋ฅผ ๊ฐ–๋Š” ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•ด์ค๋‹ˆ๋‹ค.

์ด์ œ MyDemoApplication.java ํŒŒ์ผ์„ ์—ด์–ด ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•ด์ค๋‹ˆ๋‹ค.

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class MyDemoApplication extends SpringBootServletInitializer {

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(MyDemoApplication.class);
	}

	public static void main(String[] args) {
		SpringApplication.run(MyDemoApplication.class, args);
	}

}

SpringBootServletInitializer๋ฅผ ์ƒ์†ํ•˜๊ณ  configure ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  pom.xml ํŒŒ์ผ์— ์•„๋ž˜ ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค. jasper๋Š” JSP ํŒŒ์ผ์„ ์ปดํŒŒ์ผ ํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ์„ ํ•ฉ๋‹ˆ๋‹ค.

<!-- Need this to compile JSP -->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>

 

์ด์ œ ํ”„๋กœ์ ํŠธ๋ช…์—์„œ ์šฐํด๋ฆญํ•˜๊ณ  "src/main/webapp" ์ƒˆ ํด๋”๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํด๋”๋ฅผ ๋งŒ๋“ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.

 

์ด์ œ ๋‹ค์‹œ ์šฐํด๋ฆญํ•˜์—ฌ build path ์„ค์ •(Configure Buildpath...)ํ™”๋ฉด์œผ๋กœ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค.

 

Add Folder... ๋ฅผ ํด๋ฆญํ•˜๊ณ  webapp ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ฐพ์•„ ์ฒดํฌํ•ด์ค๋‹ˆ๋‹ค.

OK ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๊ณ  Applyํ•˜๋ฉด ํ”„๋กœ์ ํŠธ ๊ตฌ์กฐ๊ฐ€ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฐ”๋€๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด์ œ webapp ์•„๋ž˜์— WEB-INF๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ํ•˜์œ„์— ๋˜ views ๋ผ๋Š” ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  test.jsp ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•ด์ค๋‹ˆ๋‹ค.

<%@ page import="java.util.*" %>

<!DOCTYPE html>
<html>
<body>
	<h1>Test Page</h1>
	Today's date: <%= new Date() %>
</body>
</html>

 

application.properties ํŒŒ์ผ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‘ ๋ผ์ธ์„ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.

spring.mvc.view.prefix = /WEB-INF/views/
spring.mvc.view.suffix = .jsp

 

์ž, ์ด์ œ ๋งˆ์ง€๋ง‰์œผ๋กœ ํ•ด๋‹น ํŽ˜์ด์ง€์™€ ์—ฐ๊ฒฐํ•  API๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. root package์—์„œ controller ๋ผ๋Š” ํŒจํ‚ค์ง€๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค๊ณ  ๊ทธ ์•„๋ž˜์— DemoController.java๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

package com.example.demo.controller;


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


@Controller
public class DemoController {
	
	@GetMapping("/test")
	public String login() {
		return "/test";
	}
}

 

์—ฌ๊ธฐ๊นŒ์ง€ ์™„๋ฃŒ๋˜์—ˆ์œผ๋ฉด ์ด์ œ Run As > Java Application ๋˜๋Š” Run As > Spring Boot App ์œผ๋กœ ๊ธฐ๋™์‹œ์ผœ์ค๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  localhost:8080/test ์— ์ ‘์†ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด jsp ํŽ˜์ด์ง€๊ฐ€ ๋œจ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

JSP ํŽ˜์ด์ง€์— javascript ๋ฐ css ํŒŒ์ผ ์—ฐ๋™

์ด์ œ JSP ํŽ˜์ด์ง€์— javascript ํŒŒ์ผ ๋ฐ css ํŒŒ์ผ์„ ์—ฐ๊ฒฐ์‹œ์ผœ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

javascript์™€ css ํŒŒ์ผ์€ src/main/resources ํ•˜์œ„์˜ static ํด๋” ์•ˆ์ชฝ์— ๋ชฐ์•„๋„ฃ์–ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์šฐ์„  static ํด๋” ํ•˜์œ„์— script ํด๋”๋ฅผ ๋งŒ๋“ค์–ด test.jsํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ  ์•„๋ž˜ ๋‚ด์šฉ์„ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

$(document).ready(function() { printCurrentDatetime(); });

function printCurrentDatetime() {
	let date = new Date();
	$('#currentTime').html(date);
	setTimeout(printCurrentDatetime, 1000);
}

 

๊ทธ๋ฆฌ๊ณ  static ํด๋” ํ•˜์œ„์— style ํด๋”๋ฅผ ๋งŒ๋“ค์–ด test.css ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ  ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

@charset "UTF-8";

body {background-color:cornflowerblue;}

 

์ž, ๋”์ด์ƒ ํŒŒ์ผ์„ ๋งŒ๋“ค ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ JSP ํŒŒ์ผ์— ์œ„์—์„œ ์ž‘์„ฑํ•œ ๋‘ ํŒŒ์ผ์„ ์—ฐ๊ฒฐ์‹œ์ผœ์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

test.jsp ํŒŒ์ผ์„ ์—ด์–ด ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•ด์ค๋‹ˆ๋‹ค.

<%@ page import="java.util.*"%>

<!DOCTYPE html>
<html>
<head>

<!-- JS link -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="script/test.js"></script>

<!-- CSS link -->
<link href="style/test.css" rel="stylesheet">

</head>
<body>
	<h1>Test Page</h1>
	Today's date: <span id='currentTime'></span>
</body>
</html>

 

๋ณ€๊ฒฝํ•œ ๋‚ด์šฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

1. html์˜ body ์•ˆ์— ์žˆ๋˜ ์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋ฅผ test.js๋กœ ์˜ฎ๊ธฐ๋ฉด์„œ refresh ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€

2. test.jsํŒŒ์ผ์—์„œ jquery๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— htmlํŽ˜์ด์ง€(jspํŒŒ์ผ)์— jQuery ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋งํฌ ์ถ”๊ฐ€

3. css ๋งํฌ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  body์˜ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ƒ‰์ƒ์„ cornflowerblue ๋กœ ์„ค์ •

 

์—ฌ๊ธฐ๊นŒ์ง€ ์ž‘์—…์ด ์™„๋ฃŒ๋˜๋ฉด ์ตœ์ข…์ ์œผ๋กœ ์•„๋ž˜์™€ ๊ฐ™์€ ํŒจํ‚ค์ง€ ๊ตฌ์กฐ๋ฅผ ๊ฐ–๊ฒŒ๋ฉ๋‹ˆ๋‹ค.

SpringBoot + JSP ์—ฐ๋™ ์ตœ์ข… ํ”„๋กœ์ ํŠธ ๊ตฌ์กฐ

 

์—ฌ๊ธฐ๊นŒ์ง€ ์ž˜ ๋”ฐ๋ผ์˜ค์…จ๋‹ค๋ฉด ์‹คํ–‰์‹œ์ผฐ์„ ๋•Œ ์•„๋ž˜์™€ ๊ฐ™์ด ํ˜„์žฌ์‹œ๊ฐ„์ด ๊ณ„์† ์—…๋ฐ์ดํŠธ๋˜๋Š” ํŒŒ๋ž€ ํ™”๋ฉด์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๐Ÿ˜Š

 

์ตœ์ข… ์™„๋ฃŒ ํ™”๋ฉด

 

์ด์ƒ์œผ๋กœ Eclipse์—์„œ SpringBoot์™€ JSP๋ฅผ ์—ฐ๋™ํ•˜์—ฌ ์›นํ”„๋กœ์ ํŠธ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค.