Search code examples
spring-bootthymeleaf

POJO objects in a Thymeleaf templates


I have a basic SpringBoot app. using Spring Initializer, embedded Tomcat, Thymeleaf template engine, and package as an executable JAR file

I have this domain objects:

    @Entity
    @Table(name="t_user")
    public class User implements Serializable, UserDetails {

        @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
        private Company company;

       public Company getCompany() {
           return company;
        }

    public void setCompany(Company company) {
        this.company = company;
    }

    ..
    }

and

@Entity
@Table(name="t_company")
public class Company implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public Company() {
    }

    /**
     * @param companyName
     */
    public Company(String companyName) {
        super();
        this.name = companyName;
    }

    @NotEmpty
    @Length(max = 100)
    private String name;


public String getName() {
    return name;
}



public void setName(String name) {
    this.name = name;
}

@Override
public String toString() {
    return "Company [id=" + id + ", name=" + name + ", timeLapses=" + timeLapses + ", applications=" + applications
            + "]";
}


..
}

But when I retrieve the value of the company name in the Thymeleaf template:

<td class="col_company" th:text="${user.company.name}"></td><!-- COMPANY NAME -->

I got this error:

rg.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "user.company.name" (/nicinc/user/userList:87)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214)
    org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177)
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)
    org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:105)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
Root Cause

But if I put in the template <td class="col_company" th:text="${user.company}"></td>

That's the result:

Company [id=1, name=No En Tenim Ni Cinc SL, timeLapses=[], applications=[]]

under root cause:

org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "user.company.name" (/nicinc/user/userList:82)
    org.thymeleaf.spring4.expression.SpelVariableExpressionEvaluator.evaluate(SpelVariableExpressionEvaluator.java:161)
    org.thymeleaf.standard.expression.VariableExpression.executeVariable(VariableExpression.java:154)
    org.thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:59)
    org.thymeleaf.standard.expression.Expression.execute(Expression.java:103)
    org.thymeleaf.standard.expression.Expression.execute(Expression.java:133)
    org.thymeleaf.standard.expression.Expression.execute(Expression.java:120)
    org.thymeleaf.standard.processor.attr.AbstractStandardTextChildModifierAttrProcessor.getText(AbstractStandardTextChildModifierAttrProcessor.java:68)
    org.thymeleaf.processor.attr.AbstractTextChildModifierAttrProcessor.getModifiedChildren(AbstractTextChildModifierAttrProcessor.java:59)
    org.thymeleaf.processor.attr.AbstractChildrenModifierAttrProcessor.processAttribute(AbstractChildrenModifierAttrProcessor.java:59)
    org.thymeleaf.processor.attr.AbstractAttrProcessor.doProcess(AbstractAttrProcessor.java:87)
    org.thymeleaf.processor.AbstractProcessor.process(AbstractProcessor.java:212)
    org.thymeleaf.dom.Node.applyNextProcessor(Node.java:1017)
    org.thymeleaf.dom.Node.processNode(Node.java:972)
    org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695)
    org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668)
    org.thymeleaf.dom.Node.processNode(Node.java:990)
    org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695)
    org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668)
    org.thymeleaf.dom.Node.processNode(Node.java:990)
    org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695)
    org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668)
    org.thymeleaf.dom.Node.processNode(Node.java:990)
    org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695)
    org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668)
    org.thymeleaf.dom.Node.processNode(Node.java:990)
    org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695)
    org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668)
    org.thymeleaf.dom.Node.processNode(Node.java:990)
    org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695)
    org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668)
    org.thymeleaf.dom.Node.processNode(Node.java:990)
    org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695)
    org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668)
    org.thymeleaf.dom.Node.processNode(Node.java:990)
    org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695)
    org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668)
    org.thymeleaf.dom.Node.processNode(Node.java:990)
    org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695)
    org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668)
    org.thymeleaf.dom.Node.processNode(Node.java:990)
    org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695)
    org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668)
    org.thymeleaf.dom.Node.processNode(Node.java:990)
    org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695)
    org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668)
    org.thymeleaf.dom.Node.processNode(Node.java:990)
    org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695)
    org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668)
    org.thymeleaf.dom.Node.processNode(Node.java:990)
    org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695)
    org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668)
    org.thymeleaf.dom.Node.processNode(Node.java:990)
    org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695)
    org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668)
    org.thymeleaf.dom.Node.processNode(Node.java:990)
    org.thymeleaf.dom.Document.process(Document.java:93)
    org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1155)
    org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060)
    org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1011)
    org.thymeleaf.spring4.view.ThymeleafView.renderFragment(ThymeleafView.java:335)
    org.thymeleaf.spring4.view.ThymeleafView.render(ThymeleafView.java:190)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1282)
    org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1037)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:980)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214)
    org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177)
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)
    org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:105)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
Root Cause

Solution

  • Changing to

     @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
     private Company company;
    

    solved my problem