2016년 8월 21일 일요일

20160821 Spring boot + thymeleaf Image 파일 어디있는거냐!

타임리프: 스프링 부트가 밀어준다. Html 기반이다. 그래서 좋아합니다.

근데 또 프로젝트 설정을 한 후 이미지 파일 하나를 보이도록 하고싶은데.
도대체 경로를 찾을 수 없었습니다.... ㅠㅠ 아... 입개발자.

-- 아래는 @EnableWebMVC를 설정하지 않았을 경우입니다.

그래서 경로와 한참을 씨름한 후 알게된 사실을 포스팅합니다.
일단 기본적으로 예전에는
webapps/WEB-INF/
를 루트로 사용했었는데, 스프링 부트의 타임리프 스타터를 사용한 후에는
<!-- thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>

</dependency>

src/main/resources 를 루트로
뷰 파일(.html 등)은 templates 안에
이미지, css, js 파일들은 static 안에 모아두면 절대경로처럼 사용할 수 있던것을 알게 되었습니다.
뷰 파일: src/main/resources/templates 
static 파일: src/main/resources/static 

그럼 이제 
html에서 저 파일을 어떻게 불러오는가.

static/images/a.png 파일을 사용하려고 한다면
<img src="images/a.png"/>
<img src="/images/a.png"/>
<img th:src="@{images/a.png}"/>
<img th:src="@{/images/a.png}"/>

등 위 방법 모두 가능합니다.

아마도. ^^;

2016년 8월 4일 목요일

20160805 JPA 사용시 주의점!

오늘 겪은 황당스러운 에러...
잘 알아보고 사용해야 하는데 대충 사용해서 이런 사단이 났습니다.
사용 디비는 H2.

Hibernate: create table 테이블 (아이디 bigint generated by default as identity, order integer, 필드 varchar(255), primary key (필드))
ERROR o.h.tool.hbm2ddl.SchemaExport - HHH000389: Unsuccessful: create table 테이블 (아이디 bigint generated by default as identity, order integer, 필드 varchar(255), primary key (필드))
==여기==
ERROR o.h.tool.hbm2ddl.SchemaExport - Syntax error in SQL statement "CREATE TABLE 테이블 (아이디 BIGINT GENERATED BY DEFAULT AS IDENTITY, ORDER[*] INTEGER, 필드 VARCHAR(255), PRIMARY KEY (필드)) ";
expected "identifier";
==여기==

==여기== 부터 ==여기== 까지 의미를 알았더라면...ㅠㅠ

또 다른 에러 내용
o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 42102, SQLState: 42S02
ERROR o.h.e.jdbc.spi.SqlExceptionHelper - Table "테이블" not found;

정확한 원인을 모른체 계속 땅을 파던중...
진짜 마지막이다 하고 차근차근 소스 파일의 1줄부터 쭈우우욱 변경하면서 원인을 분석했습니다.

원인은
private int order;
였습니다.

저는 순서를 넣고 싶었고 order 필드를 작성했습니다.
하지만, JPA 예약어였던 것입니다(아마도...).

order라는 필드는 사용하면 안됩니다! ㅠㅠ