SpringBoot thymeleaf 의 사용 방법 분석

1.pom.xml 의존 도 추가

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2.application.properties

#thymeleaf
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html; charset=utf-8
spring.thymeleaf.cache=false
3.common.xml 파일,파일 경로 주의

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Insert title here</title>
</head>
<body>
  <h1>my first thymeleaf.</h1>
  hello, <span th:text="${name}"></span>
</body>
</html>

4.TemplateController.java 추가

package myshop.controller;

import java.util.Map;

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

/*
 *     @Controller   @RestController
 *           <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 *        
 * 
 * */
@Controller
@RequestMapping("/templates")
public class TemplateController {
  
  @RequestMapping("/common")
  public String Common(Map<String, Object> map)
  {
    map.put("name", "  ");
    return "Common";
  }
}
5.app.java 추가

package myshop;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
  
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    SpringApplication.run(App.class, args);
  }

}
6.접근 경로,완료
http://localhost:8080/templates/common
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기