woniper

spring boot-4(Velocity 설정과 사용) 본문

Spring

spring boot-4(Velocity 설정과 사용)

woniper1 2014. 10. 25. 16:16

[spring] spring boot-1(특징과 기본 설정)
[spring] spring boot-2(프로젝트 구조와 Tomcat 연동 및 proerties사용)
[spring] spring boot-3(JPA 설정과 사용)


Velocity 설정

1.  pom.xml

  Velocity도 다른 설정과 마찬가지로 dependency만 추가 하면 모든 설정이 자동이다.


      org.springframework.boot
      spring-boot-starter-velocity

2. application.properties

  velocity를 사용할때 utf-8로 설정을 해야 한글이 깨지지 않는다.

spring.velocity.charSet=UTF-8

spring.velocity.properties.input.encoding=UTF-8

spring.velocity.output.encoding=UTF-8


Velocity 사용

1. vm 파일 생성

  velocity는 *.vm이라는 확장자를 쓴다. html과 똑같다. 다만 확장자만 vm이고 velocity문법을 사용하기 위해서이다. 

main > resources > telplates > index.vm 생성 후 아래 코드를 작성.

<!DOCTYPE html>

<html>

<head lang="en">

    <meta charset="UTF-8">

    <title></title>

</head>

<body>

#if(true)

    this is true!!!

#end

</body>

</html>


velocity 기본 문법은 Velocity User Guide를 참고한다.


2. Controller

package net.woniper.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * Created by woniper on 14. 10. 25..
 */
@Controller
@RequestMapping("/")
public class MainController {

    @RequestMapping("/velocity")
    public String velocity() {
        return "index";
    }
}

localhost:8080/velocity로 접속해서 "this is true!!!"라고 뜨면 성공이다.


Comments