Search code examples
jspjstljsp-tags

Can't get the size of a model attribute in JSP


This is my first project using jsp and I'm trying to build a table where each line looks a bit like this

Here is a line of the table

In the last column (likes) there is currently only a button but i would like to display the number of likes next to it.

Here is my problem. When I try to use the jsp function length ${fn:length(mes.likes)}

I get the following exception

org.apache.jasper.JasperException: javax.el.ELException: Problems calling function [fn:length]
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:606)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:482)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:170)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:314)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1325)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1069)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1008)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:870)
javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:855)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:320)
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:334)
org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:119)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilterInternal(BasicAuthenticationFilter.java:158)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:100)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:66)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
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:334)
org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:215)
org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178)
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:357)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:270)

I load all messages in the model from the HomeController

@Controller
public class HomeController {
    @Autowired
    IMessageService messageService;
    @GetMapping("/homepage")
    public String index(Model model) {
        List<MessageEntity> messages = messageService.getAll();
        model.addAttribute("allMessages", messages);
        return "index";
    }
}

Here is the MessageEntity class

public class MessageEntity{
    private int Id;
    private String title;
    private String content;
    private Date createdAt;
    private User createdBy;
    private List<LikeEntity> likes;
}

and finally, here is my table in the index.jsp file where I display the informations.

<table id="panneau">
    <thead class="bg-dark text-light">
        <tr>
            <th>Message</th>
            <th>Emetteur</th>
            <th>Date/heure</th>
            <th>Likes</th>
        </tr>
    </thead>
    <tbody class="text-black">
        <c:forEach var="mes" items="${allMessages}">
            <tr id="message${mes.id}">
                <td>
                    <div><b>${mes.title}</b></div>
                    <div>${mes.content}</div>
                </td>
                <td>
                    <c:choose>
                        <c:when test="${mes.anonymous}">
                            anonyme
                        </c:when>
                        <c:otherwise>
                            ${mes.createdBy.firstName} ${mes.createdBy.lastName}
                        </c:otherwise>
                    </c:choose>
                </td>
                <td>
                    ${mes.createdAt}
                </td>
                <td>
                    <div>${fn:length(mes.likes)}</div>
                    <div><button class="btn btn-dark" id="like${mes.id}"><i class="far fa-thumbs-up fa-2x"></i></button></div>
                </td>
            </tr>
        </c:forEach>
    </tbody>
</table>

When I remove ${fn:length(mes.likes)}, everything works fine, but I need a way to get to this information.

I have looked at other similar questions but none of them seemes to fix my problem.

By the way, I do have <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> in the begining of the file

EDIT

Here is the likeEntity class

import javax.persistence.*;
import java.util.Objects;
/**
 * Created by owner on 18-05-07.
 */
@Entity
@Table(name = "likes", schema = "heroku_9efd0238a94d992")
public class LikeEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "like_id", nullable = false)
    private int id;
    @Basic
    @Column(name = "is_dislike", nullable = true)
    private Boolean isDislike;
    @ManyToOne(cascade = {
        CascadeType.MERGE,
        CascadeType.PERSIST,
        CascadeType.DETACH,
        CascadeType.REFRESH,
    })
    @JoinColumn(name = "liked_by")
    private UserEntity likedBy;
    @ManyToOne(cascade = {
        CascadeType.MERGE,
        CascadeType.PERSIST,
        CascadeType.DETACH,
        CascadeType.REFRESH,
    })
    @JoinColumn(name = "liked_message") 
    private MessageEntity likedMessage;
    public LikeEntity() {
        this.setDislike(false);
    }
}

Solution

  • Disclamer: This is a workaround, not really a solution

    I still don't know why the likes could not load lazily, but changing the fetchtype from lazy to eager seems to have done the trick...

    I know this solution is not optimal. In my case, this workaround is suitable because it is only a school project so I will never have thousands of likes on a message.

    If however, you work with a large set of data, I do not think this solution would be appropriate.