관리 메뉴

ふたりで

Springboot2.x Web 프로젝트 설정 1(프로젝트 생성 및 JSP설정) 본문

Spring

Springboot2.x Web 프로젝트 설정 1(프로젝트 생성 및 JSP설정)

graykang 2023. 5. 8. 15:16
728x90
반응형
SMALL

- 목표 프로젝트구성

 1. springboot 2.x

 2. Maven 4

 3. spring-security

 4. mariadb(mysql)

 5. mybatis2

 6. war 파일 배포

 

- 이번 포스팅은 springboot2.x로 web 프로젝트를 구성하여 테스트용 jsp 페이지를 출력해 보는 것까지 

  정리를 한다.

 

1. springboot Web프로트 생성

위 그림과 같이 설정후 넥스트 또는 피니쉬 버튼을 눌러 생성을 해준다.

 

프로젝트가 생성되고난 후의 프로젝트 구조.

 

2. application.properties 에 DB연결 정보를 설정해 준다.

mysql 관련 dependency가 pom.xml에 추가되어 있기 때문에 아래와 같이 DB연결 설정을 기본으로 하지 않을 경우

에러가 발생한다. 해당 설정 값은 추후 mybatis를 설정할 때 한 번 더 세밀하게 수정할 예정이다.

spring.datasource.url=jdbc:mysql://192.168.56.1:3306/test
spring.datasource.username=user1
spring.datasource.password=1234567
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

 

3. 이 부분은 파일 하나로 관리 하기 위해 수정을 하였지만 굳이 수정을 하지 않아도 된다. 

SpringBootTest2Application.java 파일과 ServletInitializer.java 파일을 한 개의 파일로 합치기. 

SpringBootTest2Application.java 파일을 아래와 같이 수정한 후 ServletInitializer.java 파일은 삭제한다.

package com.graykang.test2;

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

@ServletComponentScan
@SpringBootApplication
public class SpringBootTest2Application extends SpringBootServletInitializer{
	
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder applicationBuilder) {
		return applicationBuilder.sources(SpringBootTest2Application.class);
	}
	
	public static void main(String[] args) {
		SpringApplication.run(SpringBootTest2Application.class, args);
	}

}

4. Maven install을 실행하여 정상 적으로 빌드가 되는지 확인한다.

[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ spring-boot-test2 ---
[INFO] Installing C:\sts4-expro\spring-boot-test2\target\spring-boot-test2-0.0.1-SNAPSHOT.war to C:\Users\tenpe\.m2\repository\com\graykang\test2\spring-boot-test2\0.0.1-SNAPSHOT\spring-boot-test2-0.0.1-SNAPSHOT.war
[INFO] Installing C:\sts4-expro\spring-boot-test2\pom.xml to C:\Users\tenpe\.m2\repository\com\graykang\test2\spring-boot-test2\0.0.1-SNAPSHOT\spring-boot-test2-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  21.906 s
[INFO] Finished at: 2023-05-08T14:11:03+09:00
[INFO] ------------------------------------------------------------------------
[WARNING] The requested profile "pom.xml" could not be activated because it does not exist.

5. 프로젝트를 구동 시 아래와 같이 오류가 없이 잘 실행되는지 확인한다.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::      (v2.7.12-SNAPSHOT)

2023-05-08 14:13:04.142  INFO 16388 --- [  restartedMain] c.g.test2.SpringBootTest2Application     : Starting SpringBootTest2Application using Java 17.0.5 on togethers-hp450 with PID 16388 (C:\sts4-expro\spring-boot-test2\target\classes started by tenpe in C:\sts4-expro\spring-boot-test2)
2023-05-08 14:13:04.143  INFO 16388 --- [  restartedMain] c.g.test2.SpringBootTest2Application     : No active profile set, falling back to 1 default profile: "default"
2023-05-08 14:13:04.189  INFO 16388 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2023-05-08 14:13:04.189  INFO 16388 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2023-05-08 14:13:04.534  INFO 16388 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JDBC repositories in DEFAULT mode.
2023-05-08 14:13:04.541  INFO 16388 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 3 ms. Found 0 JDBC repository interfaces.
2023-05-08 14:13:04.599  WARN 16388 --- [  restartedMain] o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[com.graykang.test2]' package. Please check your configuration.
2023-05-08 14:13:04.870  INFO 16388 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2023-05-08 14:13:04.877  INFO 16388 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-05-08 14:13:04.877  INFO 16388 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.74]
2023-05-08 14:13:04.920  INFO 16388 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-05-08 14:13:04.920  INFO 16388 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 730 ms
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
2023-05-08 14:13:05.246  INFO 16388 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2023-05-08 14:13:05.250  WARN 16388 --- [  restartedMain] com.zaxxer.hikari.util.DriverDataSource  : Registered driver with driverClassName=com.mysql.jdbc.Driver was not found, trying direct instantiation.
2023-05-08 14:13:05.397  INFO 16388 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2023-05-08 14:13:05.464  INFO 16388 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2023-05-08 14:13:05.488  INFO 16388 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2023-05-08 14:13:05.495  INFO 16388 --- [  restartedMain] c.g.test2.SpringBootTest2Application     : Started SpringBootTest2Application in 1.565 seconds (JVM running for 2.1)

6. jsp 사용 설정.

 6-1. pom.xml에 아래 두 개의 dependency를 추가해 준다.

		<!-- 		jsp config -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

 6-1. application.properties파일에 JSP 사용을 위한 설정을 추가한다.

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

  6-2. WEB-INF/views 폴더 생성.

  src/webapp/WEB-INF/views 경로로 폴더를 생성하고 front 용 폴더를 views폴더 밑에 생성해 주고, test용 JSP 파일을 

  만들어 준다. test용 jsp 파일은 mainDashBoard.jsp로 만들어 준다.

  결과: src/webapp/WEB-INF/views/front/mainDashBoard.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
graykang test SpringBoot Web
</body>
</html>

  

7. jsp페이지가 잘 로딩되는지 확인하기 위해 Controller.java파일을 아래와 같이 생성한다. 파일 위치는 package 참고.

package com.graykang.test2.front.controller;

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

@Controller
public class FrontController {
	
	@GetMapping(value = "/front/dashBoard")
	public String mainDashBoard() {
		return "/front/mainDashBoard";
	}

}

8. http://localhost:8080/front/dashBoard로 접속하면 아래와 같이 설정한 페이지가 출력된다.

- 다음 포스팅에서는 mybatis설정 및 DB의 데이터를 조회하여 jsp 페이지에 출력하는 부분까지 포스팅할 예정이다.

728x90
반응형
LIST
Comments