pooney
article thumbnail

크게 템플릿 엔진은 여러개가 존재한다. 그중 JSPThymeleaf를 설명한다 

 

 

우선 spring-boot-devtools를 추가 한후 true를 준다. 이유는 수정작업이 이루어지면 톰켓을 재시작하여 바로 결과를 반영 할 수 있게 도와준다.

 

pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
    <optional>true</optional>
</dependency>

 

 

jsp 사용하기

 

1. application.properties에 아래의 코드 삽입 

 

application.properties

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

 

 

2.  pomx.xml에 jasper 와 jstl 삽입 

 

.jsp로 끝나는 확장자는 jasper가 컴파일 하게된다. 

 

pom.xml

<!--jsp parser  -->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>

<!--jstl 라이브러리  -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>

 

3. application.properties에서 설정한 경로대로 폴더와 파일을 생성

 

WEB-INF -> views -> index.jsp

 

 

 

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<title>Insert title here</title>

 

</head>

<body>
<h2> name : ${name}</h2>
</body>
</html>

 

 

 

4. controller 생성하기 

 

Hicontroller

package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class Hicontroller {
	@RequestMapping("/jsptest")
	public ModelAndView jsptest() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("name" ,"pooney.jsp");
		mv.setViewName("index");
		
		
		
		return mv;
	}
	
	@RequestMapping("/thymetest")
	public String thymetest(Model model) {
		model.addAttribute("name" , "pooney.th");
		
		
		
		return "thymeleaf/thymeleaftest";
		
		
	}

}

 

 

5.  /jsptest 요청 

 

pooney.jsp 를 정상적으로 출력 

 

 

 

 

Thymeleaf 사용

 

 

 

1. application.properties에 아래의 코드 삽입

 

  • cache=false 는 thymeleaf를 사용하다가 수정하는 경우 재시작을 해야한다. 하지만 이를 무시하고 브라우저에서 새로고침으로 변경사항을 취하도록 설정한다. true를 운영도중에 사용하는것이 좋다 .
  • view-names는 jsp와 같이 사용할경우 구분짓기 위하여 사용 thymeleaf로 시작하는 경우는 thymeleaf로 처리  

 

 

application.properties

#thymeleaf를 사용하다가 수정하는 경우 재시작을 해야한다. 하지만 이를 무시하고 브라우저에서 새로고침으로 변경사항을 취하도록 설정한다. true를 운영도중에 사용하는것이 좋다 .
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:templates/
spring.thymeleaf.suffix=.html
#view-names는 jsp와 같이 사용할경우 구분짓기 위하여 사용 thymeleaf로 시작하는 경우는 thymeleaf로 처리  
spring.thymeleaf.view-names=thymeleaf/*

 

 

 

2. templates 밑에 thymeleaf 폴더 생성하고 thymeleaftest.html 파일 생성 

 

 

 

templates -> thymeleaf -> hymeleaftest.html 

 

 

 

thymeleaftest.html

<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello thymeleaf</title>
</head>
<body>
	<h2>name : <span th:text="${name}"></span></h2>
</body>
</html>



 

 

3. controller 생성 

 

 

Hicontroller

package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class Hicontroller {
	@RequestMapping("/jsptest")
	public ModelAndView jsptest() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("name" ,"pooney.jsp");
		mv.setViewName("index");
		
		
		
		return mv;
	}
	
	@RequestMapping("/thymetest")
	public String thymetest(Model model) {
		model.addAttribute("name" , "pooney.th");
		
		
		
		return "thymeleaf/thymeleaftest";
		
		
	}

}

 

 

 

4.  /thymetest 요청 

 

pooney.th 를 정상적으로 출력 

 

profile

pooney

@pooney

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!