DevEnjoy

One Step Closer

Handebars

Spring boot view template(handlebars), static resource 갱신(without server shutdown)

2017년 7월 20일 by YongPwi Leave a Comment

이전까지 spring boot를 사용시에 webapp 폴더를 임의적으로 생성하여 그 하위에 view tempate, static resource를 설정하였다.

하지만 spring boot에서 가이드하는 방식은 resources 하위에 view template과 static resource를 설정하도록 권하고 있다.

그러므로 이전의 war 방식이 아닌 jar를 가이드하고 있기 때문이기도 한듯 싶다.

최근 jar 배포용 application을 만들다 보니 기존 설정 방식을 그대로 사용해서는

문제가 좀 발생하여 수 많은 삽질을 했다.

관련 내용을 차근차근 정리해 볼까 한다.

일단 jar 설정에서는 WEB-INF 하위에 view template이 존재 하지 않아서 매번 서버를 내렸다 올려야 화면 변경 내용이 확인 가능했다.

정말 미친듯이 귀찮다.

상위 방식은 기존 WEB-INF 하위에 view template 파일들이 있었지만

1
2
3
4
5
6
7
8
9
10
11
@Bean
    public HandlebarsViewResolver viewResolver() throws Exception {
        HandlebarsViewResolver viewResolver = new HandlebarsViewResolver();
        viewResolver.setOrder(1);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".hbs");
        viewResolver.setCache(false);
        viewResolver.registerHelpers(new HandlebarsHelper());
        viewResolver.registerHelpers(StringHelpers.class);
        return viewResolver;
    }

상위 처럼 handlebars의 경우 WebMvcConfigurerAdapter 상속받아서 상위 처럼

1
viewResolver.setCache(false);

설정하면 서버재시작 없이 브라우저 갱신하게 되면 변경 내용을 확인할 수 있었다.

하지만 jar 방식에서는 상위 방식 외에 다른 설정 방법이 필요하다.

– spring boot devtools 의존성 추가

관련 내용을 찾다보니 spring boot devtools 설정으로 해결 가능했다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
dependencies {
        compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-jdbc')
    compile('org.springframework.boot:spring-boot-starter-security')
    compile("org.springframework.boot:spring-boot-starter-web")
    compile('com.github.jknack:handlebars-springmvc:4.0.6')
    compile('org.postgresql:postgresql:9.4-1201-jdbc41')
    compile('org.bgee.log4jdbc-log4j2:log4jdbc-log4j2-jdbc4.1:1.16')
    compile('org.projectlombok:lombok')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    compile('org.codehaus.jackson:jackson-core-asl:1.9.13')
    compile('com.google.guava:guava:18.0')
    compile('org.modelmapper:modelmapper:0.7.5')

        // spring boot devtools dependency 추가
        compile("org.springframework.boot:spring-boot-devtools")
}

– spring boot 설정 변경( application.properties, application.yml )

운영 서버를 제외한 로컬 환경에만 적용하기 위하여 하위 처럼 설정한다.

1
2
3
4
5
6
7
8
9
// application.yml
spring :
     profiles: local
     devtools:
        livereload:
            enabled: true

// application.properties
spring.devtools.livereload.enabled=true

– IDE(intellij) 옵션 수정

intellij > Help > find action > registry 검색

compiler.automake.allow.when.app.running 체크

intellij > PreFerences > Build, Execution, Deployment > Compiler > Build proejct automatically 체크

상위까지 설정하고 서버 시작하면 그 이후에는 서버 재시작 없이 브라우저 새로고침만으로 view template 변경 적용되는 것을 확인 가능하다.

Posted in: Java, Programing, Talk Tagged: Handebars, JAVA, Spring boot, spring boot devtools

Calendar

6월 2025
일 월 화 수 목 금 토
« 4월    
1234567
891011121314
15161718192021
22232425262728
2930  

Recent Posts

  • ubuntu bastion 설정
  • Spring Boot properties 암호화
  • Git Repository Bitbucket과 Issue Tracker Redmine 연동 설정
  • Spring Security 동일 session 제어
  • Spring @Mock, @Mockbean, @InjectMock

Recent Comments

  • pzotov (Ubuntu 14.04에서 Sonarqube 6.7.1 service 등록)
  • cours de theatre paris (AWS ELB와 Auto Scaling 연동, nginx)
  • bayern munich (IntelliJ EAP Font rendering)
  • camiseta del chelsea (OS X에서 APP 아이콘 변경)
  • cheap football shirts replica (jQuery Ajax에서 json Array 직렬화)

Copyright © [the-year] [site-link].

Powered by [wp-link] and [theme-link].