본문 바로가기
Spring/Spring Boot 게시판 만들기

[Spring Boot] JSP파일 만들어서 Hello Spring Boot! 출력하기 [Spring Boot 기반으로 간단한 게시판 웹사이트 만들기 - 2부]

by 임채훈 2019. 2. 22.

2019/02/24 - [Spring/Spring Boot 게시판 만들기] - [Spring Boot] UserService, BoardService 클래스 작성하기 [Spring Boot 기반으로 간단한 게시판 웹사이트 만들기 - 5부]

2019/02/24 - [Spring/Spring Boot 게시판 만들기] - [Spring Boot] Spring Boot로 MyBatis 연동하기 (MySQL) [Spring Boot 기반으로 간단한 게시판 웹사이트 만들기 - 4부]

2019/02/24 - [Spring/Spring Boot 게시판 만들기] - [Spring Boot] User와 Board의 데이터베이스 테이블 구축 및 DTO(모델) 클래스 작성하기 [Spring Boot 기반으로 간단한 게시판 웹사이트 만들기 - 3부]

2019/02/21 - [Spring/Spring Boot 게시판 만들기] - [Spring Boot] 이클립스(eclipse)로 Spring Boot 개발환경 구축하기 [Spring Boot 기반으로 간단한 게시판 웹사이트 만들기 - 1부]



본격적으로 JSP파일을 생성하여 실행해보겠습니다.



1. pom.xml


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
<dependencies>
<!--     <dependency> -->
<!--         <groupId>mysql</groupId> -->
<!--         <artifactId>mysql-connector-java</artifactId> -->
<!--         <scope>runtime</scope> -->
<!--     </dependency> -->
<!--     <dependency> -->
<!--         <groupId>org.springframework.boot</groupId> -->
<!--         <artifactId>spring-boot-starter-jdbc</artifactId> -->
<!--     </dependency> -->
<!--     <dependency> -->
<!--         <groupId>org.mybatis.spring.boot</groupId> -->
<!--         <artifactId>mybatis-spring-boot-starter</artifactId> -->
<!--         <version>2.0.0</version> -->
<!--     </dependency> -->
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
</dependencies>
 
cs



pom.xml 파일입니다.


프로젝트 관리도구인 Maven의 라이브러리 의존관리를 설정해주는 파일입니다.


Spring Boot에선 기본적으로 JSP VIEW를 지원하지 않기 때문에 임의로 dependency를 추가해줘야 됩니다.


또한 JSTL Tag Library를 사용할것이기 때문에 마찬가지로 dependency를 추가해줍니다.


그리고 DB와 관련된 의존 항목들은 주석 처리를 해줍니다.


왜냐하면 스프링 부트 실행할 시 데이터베이스 기본 설정 정보(dataSource 등)가 없다면 실행 오류가 발생하기 때문입니다.





2. application.properties


1
2
3
4
server.port=8000
 
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
cs



기존 스프링에서 프로젝트 설정 정보들을 dispatcher-servlet.xml이나 web.xml등 XML파일로 설정을 했었는데요,


스프링 부트는 XML파일 사용하는것을 권고하지 않습니다.


일반적으로 application.properties파일이나 Java파일에 Configuration으로 작성하여 관리를 합니다.



server.port의 설정은 Spring Boot에 기본적으로 내장되어있는 Tomcat과 Jetty와 같은 WAS의 포트번호를 임의로 변경해줄수도 있습니다.


viewResolver에 관련된 설정은 아래의 Spring Bean으로 설정해준것과 완전히 동일합니다.


WEB-INF라는 폴더를 추가적으로 생성한 이유는 URL로 파일 경로를 통하여 접근하는것을 막는데에 효과가 있다고 합니다.


1
2
3
4
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/views/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>
cs





3. src/main/java -> com.example.demo.controller 패키지 생성 -> HomeController Class파일 생성


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.example.demo.controller;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
 
@Controller
public class HomeController {
    
    @RequestMapping(value="/")
    public String index() {
        
        return "index";
    }
    
}
cs





4. src -> main -> webapp폴더 생성 -> WEB-INF폴더 생성 -> views폴더 생성 -> index.jsp파일 생성


1
2
3
4
5
6
7
8
9
10
11
12
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
</head>
<body>
    <h1>Hello Spring Boot!</h1>
</body>
</html>
cs





5. 프로젝트 우클릭 -> Run As-> Spring Boot App






6. 실행 후 콘솔창




아무 문제없이 실행(서버 start)에 성공한 상태의 콘솔창입니다.




7. 결과 화면



아까 applicaion.properties에서 설정한 port번호로 접속을 했을때


Hello Spring Boot!가 출력이 되었으면 성공


댓글