티스토리 뷰

Eclipse Spring Boot Setting

Step 1. 프로젝트 생성

  • [Help] -> [Eclipse Marketplace] -> sts 검색 -> Spring tools 4 - for Spring Boot 설치 -> Eclipse restart

  • [File] - [New] - [other...] - [spring] - [Spring Starter Project] - Name, Package 설정 - Next - Web(Spring Web 체크)

Step 2. 컨트롤러 생성

  • [SpringBoot2] - [src] - [main] - [java] - [pack02] - Apple.java 클래스 생성 -아래의 코드 복붙 - relaunch 클릭 후 실행
// 방법1
package Pack;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class Apple {
  @RequestMapping("/")
  public String root() {
    System.out.println("방법1 Apple Call");
    return "Apple Call";
  }
}

// 방법2

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


@Controller
public class Apple {
  @ResponseBody
  @RequestMapping("/")
  public String root() {
    System.out.println("방법2 Apple Call");
    return "Apple Call";
  }
}

// 방법 3 => 3번 방법을 사용해야 아래의 설정 후 main 페이지(index.jsp)가 뜬다.
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


@Controller
public class Apple {
  @RequestMapping("/")
  public String f1() { // 함수 이름을 확 바꾸었다.
    System.out.println("곧 index.jsp 실행");
    return "index";
  }
}
  • relaunch가 안되면?
    • [프로젝트명] - 마우스 우클릭 - Run as - Spring Boot App 클릭 -> console에 Spring이 뜨면 실행

Step 3. jsp 설정

  • pom.xml 파일에 아래의 코드를 추가한다.
    • Spring Boot는 공식적으로 jsp를 지원하지 않는다.
    • jsp를 사용하기 위해서는 dependency를 걸어주어야 한다.
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>
    
	<dependency>
		<groupId>org.apache.tomcat.embed</groupId>
		<artifactId>tomcat-embed-jasper</artifactId>
		<scope>provided</scope>
	</dependency>

Step 4. view 생성

  • [src] - [main] - 마우스 우클릭 - new - folder 생성 - 폴더네임 설정
  • 폴더 네임은 webapp/WEB-INF/views로 설정하여 한꺼번에 생성이 가능하다.
  • [views] - 마우스 우클릭 - [new] - [jsp file] - index.jsp 생성 - 테스트를 위해 아래 코드 복붙
// html 사용을 위한 jsp 코드

<!DOCTYPE html>
<html>

<head>
<title>Page Title</title>
</head>

<body>
  <h1>This is a Heading</h1>
  <p>This is a paragraph.</p>
</body>
</html>
  • 경로지정을 위해 [src] - [resources] - application.properties - 아래코드 복붙
* 경로지정 하는 것
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
  • index.jsp 파일을 아래의 소스로 변경하고 test를 실시한다.
// jsp 사용을 위한 jsp 코드

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.*, java.text.*"%>

<head>
  <title>메인 페이지</title>
</head>

<body>
  <%=new Date()%>
  <h2>Hello World</h2>
  
  <a href="t1">링크</a>
</body>
  • 전체적인 구조는 다음과 같다.

 

 

 

2019.09.19 (목)

Eclipse Spring Boot Setting

'SpringBoot' 카테고리의 다른 글

[STRING BOOT] String Boot + MySQL + Mybatis  (1) 2019.09.20
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
TAG
more
«   2024/05   »
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
글 보관함