본문 바로가기

카테고리 없음

[Spring Boot + Thymeleaf] Thymeleaf 해보기

반응형

thymeleaf

 

2012년에 개발 시작.

Boot랑 거의 같은 시점에 시작.

 

Boot가 Thymeleaf를 채택하여 성장하게 됨

 

Server side template Engine이다.

Web 과 Standalone 을 다 지원함 (Jar에서도 돌아가고, WAR에서도 돌아간다.)

 

 

Thymeleaf 표현식

 

1. Variable Expressions

 : ${ }

 

 

 

 

 

실습

 

1. Controller생성

 

package com.first.myboot.controller;

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

@Controller
public class TemplateController {

	
	@GetMapping("/main")
	public String main(Model model) {
		model.addAttribute("name", "타임리프");
		return "main";
	}
}

 

 

 

 

 

2. static 폴더에 HTML파일 생성

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h2>Welcome SpringBoot</h2>
	<a href="/main">타임리프 페이지 호출</a>
</body>
</html>

 

 

 

 

 

 

 

 

 

 

 

D:\myjava\java_workspace\myboot\src\main\resources\templates

여기 로컬에서도 열린다. 이것이 타임리프 장점

 

 

 

 

 

DEVTools라는 라이브러리가있다.

클래스패스에 있는 파일이 변경될 때 마다 자동으로 재시작 해준다.

 

서버 자꾸 내렷다 올렸다 하는게 귀찮을 때 사용

 

 

배포하는 방법

 

1. pom.xml에다가 <packaging>jar</packaging> 추가

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.8.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.first</groupId>
	<artifactId>myboot</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
	<name>myboot</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>11</java.version>
		<spring-framework.version>5.2.11.RELEASE</spring-framework.version>

	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<!-- <exclusions> -->
			<!-- <exclusion> -->
			<!-- <groupId>org.junit.vintage</groupId> -->
			<!-- <artifactId>junit-vintage-engine</artifactId> -->
			<!-- </exclusion> -->
			<!-- </exclusions> -->
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>


	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

 

 

2. 이후 Maven build 에 Goals에 package 라고 써주고 Run하면 막 뭘 다운받는다.

 

이후 Target 폴더를 보면 들어가 있따.

 

 

 

다음 cmd띄워서 java -jar jar파일명 입력하면. 서버가 실행된다.

 

포트로 접속하몀 정상 실행된 것을 확인할 수 있다.

반응형