(Spring + Thymeleaf) Error SpelEvaluationException

ZZOOO

·

2021. 3. 29. 18:40

안녕하세요 Spring Tool Suite 3.xx 버전 + Thymeleaf로  개발을 하고있습니다.

쉽게 해결했지만, 처음 맞닥뜨리면 당황스러우실 수 있으니 포스팅하겠습니다. ( 저도 리마인드 ~ )


소스 형식

 

아래는 thymeleaf를 사용하시는 개발자분들이라면 자주 접하는 형식이실겁니다.

<tbody>
	<tr th:each="test : ${dtoList}">
    	<td th:text="${dtoList?.getTitle()"}></td>
        <td th:text="${dtoList?.getBody()"}></td>
        <td th:text="${dtoList?.getReg_date()"}></td>
        <td th:text="${dtoList?.getAuthor()"}></td>
    </tr>
</tbody>

 

Contoller 단에서 dtoList에 List<>형태로 데이터를 담아서 보내주면
dtoList?.getXXX() 방식으로 필요한 정보만 가져올 수 있는 아주 편리한 기능입니다.


에러 내용

 

org.springframework.expression.spel.SpelEvaluationException: EL1004E: Method call: Method getXXX() cannot be found on type java.lang.String

 

이와 같은 에러가 발생했습니다.


SpelEvaluationException ??

 

저도 처음 마주치는 에러라 검색을 해보았습니다.

어떤분의 블로그를 보니, 

<td th:text="${content.category.name}">Computer</td>

이와 같은 형식으로 작성을해서

org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 0): Property or field 'name' cannot be found on null

다음과 같은 오류가 발생했다고 했습니다.


위 소스의 문제는 무엇일까요 ?

문제는 thymeleaf의 문법에있습니다.

<td th:text="${content.category?.name}">Computer</td>

사용할 프로퍼티 앞에 "?" 를 작성해줘야한다는 것입니다.

 


본인의 에러 발생 소스

 

본인의 소스는 다음과 같았습니다.

<tr th:each="company : History">

 

어떤 문제인지 바로 보이시나요 ? 

저도 이런 실수를 처음해봐서 생각도 못했습니다. ㅋㅋㅋㅋㅋㅋㅋㅋㅋ

더블클릭으로 쿼테이션 안의 내용들을 복붙하는경우가 빈번하다보니
${} 도 같이 날아가서 저런 소스가 완성(?)되었습니다.


수정 후 문제 해결

 

<tr th:each="company : ${timeLicenseHistory}">

 

다음과 같이 형식에 맞게 코딩했더니 문제없이 해결되었습니다.


도움이 되셨길바라며 포스팅 마치겠습니다.

감사합니다.

 

반응형