TY blog

타임리프 클래스 필드 참조 오류 본문

웹 개발

타임리프 클래스 필드 참조 오류

주짓수하는 개발자 2024. 4. 1. 22:32

타임리프 템플릿을 적용하여 개인 웹 사이트 개발을 진행하는 도중

 

An error happened during template parsing

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1021 E: A problem occurred whilst attempting to access the property 'projectNum': 'Accessing member 'projectNum' is forbidden for type 'class com.sun.springprofile_v2.model.ProjectVO' in this expression context.'

 

위와 같은 서버 오류를 발견하였는데 

@Getter
@ToString
public class ProjectVO {

    private int projectNum;
    private String main_img;
    private String title;

}

 

VO 코드

 

<div class="project_box" th:each="item : ${list}" th:onclick="|goPage('project/detail?num=${item.projectNum}')|">
    <span th:text="${item.title}"></span>
    <img th:src="${item.main_img}" />
</div>

 

HTML 타임리프 코드 일부분

Controller에서는 Model 객체에 

ProjectVO를 리스트로 넘겨주는 코드였고 

 

Private 필드로 되어 있어도 @Getter 어노테이션이 있어 참조를 할 수 있어야 정상인데 

 HTML 코드 부분에서 클래스의 필드를 참조하지 못해 오류가 발생했다. 

 

원인을 찾던 중 문서를 확인해 보니 

 

https://www.thymeleaf.org/doc/articles/thymeleaf31whatsnew.html

 

Thymeleaf 3.1: What’s new and how to migrate - Thymeleaf

Thymeleaf 3.1: What’s new and how to migrate Latest version is Thymeleaf 3.1.1.RELEASE. What’s new Support for Servlet API 5.0 and the jakarta.* class namespace Thymeleaf 3.1 adds support for the new jakarta.* class namespace in the Servlet API since v

www.thymeleaf.org

 

타임리프 문서에 내용을 보면 

 

타임리프 문서

 

 이러한 패키지의 클래스와 정적 참조에 대해 메서드/생성자 호출이 금지됩니다.

 

라는 내용이 있는데 

 

내가 만든 vo 경로에 패키지 부분은 com.sun.springprofile_v2.model 이였으며 

패키지 경로가 com.sun.* 으로 되어 있어서 클래스의 필드를 참조하지 못했던 것이다.

 

패키지 이름을 수정 후 다시 실행해 보니

해결이 되었다.

Comments