- Spring Initializr에 접속하여 Spring Boot 프로젝트 생성하기

  1. start.spring.io 에 접속하기

       Spring Initializr는 https://github.com/spring-io/initializr 오픈소스를 이용한 웹 서비스 이다.



  2. 원하는 Build 도구와 Spring Boot 버전을 선택한 뒤, 내게 필요한 dependency들을 선택한다.

       이미 많이 사용하는 의존성 목록을 추가해 놓았기 때문에, 일일히 mvnrepository에서 각 패키지들을 import 시킬 필요가 없고, start로 주어지는 묶음을 사용하면 매우 편리하다.


  3. Generate Project를 눌러 Project 파일을 다운로드 받고, 압축을 푼다.

  4. IntelliJ나 eclipse에서 압축을 푼 프로젝트를 열어준다.

  5. 만약 Thymeleaf나 react 등이 아닌 JSP로 Frontend를 사용하고 싶다면, 아래와 같은 jasper와 jstl dependency를 추가 해주어야 한다.

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>

6. 작성 중

'Java Programming' 카테고리의 다른 글

Maven에서 Java Spring 세팅하기  (0) 2018.11.30
Spring Boot 2에서 Thymeleaf 사용하기  (0) 2018.11.21
Java 101 - StringBuffer  (0) 2018.09.03
Java 101 - 생성자(Constructor)  (0) 2018.09.03

WRITTEN BY
장태희

,

※ 본 항목은 업데이트 중입니다.


- pom.xml 설정하기

Spring Project를 개발하기 위해서는, pom.xml에 packaging과 property, dependency를 추가 해 주어야 한다.


  - packaging을 war로 설정한다.

<packaging>war</packaging>

  - property에서 Spring Framework version을 설정한다.

    spring.version에 대한 정보는 Github(https://github.com/spring-projects/spring-framework/wiki/Spring-Framework-Versions) 혹은 Spring 공식 사이트(https://spring.io/projects/spring-framework#learn)에서 확인할 수 있다.

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version> 4.3.5.RELEASE</spring.version>
</properties>

  - <dependencies>에서 spring framework에 필요한 spring-context, spring-webmvc, spring-test dependency들을 추가해 준다.

<dependencies>
<!-- Spring 프로젝트에 필요한 dependency -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>

  - JSP를 추가적으로 이용하고자 한다면, JSTL과 EL dependency들을 추가해준다.

<!-- Servlet JSP 사용을 위한 JSTL dependency -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

  - Database 통신을 위해 PlatformTransactionManager와 JDBC, SQL Driver(여기에서는 MySQL), Connection Pool(여기에서는 DBCP2)을 추가한다.

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>

<!-- basic data source -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>

  - 파일 전송을 원한다면, Apache Commons File Upload와 IO를 추가한다.

<!-- Apache Commons FileUpload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>

<!-- Apache Commons IO -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>


</dependencies>

  - JDK 버전과 tomcat 버전을 추가해준다.

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<charset>UTF-8</charset>
<uriEncoding>UTF-8</uriEncoding>
<port>8080</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>


- Servlet 3.0 미만에서 web.xml 설정하기

  전통적인 Spring Framework에서는 해당 프로젝트의 /src/main/webapp/WEB-INF 폴더에 web.xml 파일을 추가함으로서 Application Context를 등록할 수 있다. web.xml은 tomcat과 같은 was가 읽어서 등록시킨다.

  web.xml은 아래와 같이 설정한다.

<?xml version="1.0" encoding="UTF-8"?>
<web-app>

<display-name>Spring JavaConfig Sample</display-name>
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>examples.daoexam.config.ApplicationConfig</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>examples.daoexam.config.WebMvcContextConfiguration</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>


</web-app>


'Java Programming' 카테고리의 다른 글

Maven으로 Spring Boot 2 세팅하기  (0) 2018.12.03
Spring Boot 2에서 Thymeleaf 사용하기  (0) 2018.11.21
Java 101 - StringBuffer  (0) 2018.09.03
Java 101 - 생성자(Constructor)  (0) 2018.09.03

WRITTEN BY
장태희

,

- Thymeleaf란?

타임리프는 server-side의 Java 템플릿으로서,  TML, XML, JavaScript, CSS, 일반 텍스트 문서를 생성하는 View Template Engine이다.

타임리프의 목표는 우아하고, 고 유지보수성(유지보수가 용이한)의 템플릿 생성을 제공하는 것이다.


- Spring Boot2에서 Thymeleaf 적용 설정하기

1-1. Spring Initializr에서 Thymeleaf 선택하기

Spring Initializr(https://start.spring.io/)에서 Spring Boot 프로젝트를 생성할 때, Thymeleaf 항목을 추가한다.


1-2. pom.xml에서 spring-boot-starter-thymeleaf 추가하기(Maven 기준)

https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf 에서 본인이 원하는 버전에 맞는 타임리프 버전의 dependency를 추가해 준다.

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>

2. application.properties에서 thymeleaf 관련 설정

application.properties에서 다음의 항목을 추가해 준다.

spring.thymeleaf.enabled=true
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.prefix=/WEB-INF/views/
spring.thymeleaf.suffix=.html

spring.thymeleaf.enabled → thymeleaf의 사용을 enable 시킨다.

spring.thymeleaf.encoding → thymeleaf의 인코딩을 UTF-8로 기본 설정한다.

spring.thymeleaf.prefix → thymeleaf의 기본 경로인 templates를 원하는 임의의 경로로 설정한다. (여기에서는 webapps/WEB-INF/views 로 설정하였다.)

spring.thymeleaf.suffix → thymeleaf가 읽어들이는 파일의 확장자를 설정한다. 예를 들어 Controller의 method에서 return "index"; 일 경우, 자동적으로 index.html을 읽어로게 된다.


- Thymeleaf의 기본 폴더 경로

Thymeleaf에서는 기본적으로 resources 폴더의 templates폴더에 html 파일들이 위치한다.

이 외의 css, js 등의 정적 파일들은 static 폴더에 위치하게 된다.


- Trouble Shooting

1. ERROR - org.thymeleaf.exceptions.TemplateInputException: Error resolving template “index”, template might not exist or might not be accessible by any of the configured Template Resolvers

→ templates 폴더에 "index.html"파일이 없어서 발생하는 문제이다. 이 외에도 해당 경로에 대상 view가 없으면 이와 같은 에러가 발생한다.

2. 기존에 JSP를 사용하고 있다면, jstl과 jasper 관련 설정을 pom.xml에서 주석 처리 해주어야 한다.


참고

http://cyberx.tistory.com/132

http://araikuma.tistory.com/30

https://blog.hanumoka.net/2018/08/01/spring-20180801-spring-Thymeleaf/

https://www.tutorialspoint.com/spring_boot/spring_boot_thymeleaf.htm

https://www.mkyong.com/spring-boot/spring-boot-hello-world-example-thymeleaf/

http://rura6502.tistory.com/2

http://wonwoo.ml/index.php/post/1209

https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#what-is-thymeleaf

https://www.thymeleaf.org/doc/articles/standarddialect5minutes.html

https://memorynotfound.com/spring-boot-thymeleaf-configuration-example/

https://cizz3007.github.io/%ED%83%80%EC%9E%84%EB%A6%AC%ED%94%84/syntax/thymeleaf/2018/04/09/thymeleaf/


'Java Programming' 카테고리의 다른 글

Maven으로 Spring Boot 2 세팅하기  (0) 2018.12.03
Maven에서 Java Spring 세팅하기  (0) 2018.11.30
Java 101 - StringBuffer  (0) 2018.09.03
Java 101 - 생성자(Constructor)  (0) 2018.09.03

WRITTEN BY
장태희

,