这是我的第一个使用jsp的项目,我尝试构建一个表格,其中每一行看起来都像这样
在最后一栏(喜欢)中,目前只有一个按钮,但我想显示旁边的喜欢的数量。
这是我的问题。当我尝试使用jsp函数长度${fn:length(mes.likes)}
我收到以下异常
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)
我从 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";
}
}
以下是 MessageEntity 类
public class MessageEntity{
private int Id;
private String title;
private String content;
private Date createdAt;
private User createdBy;
private List<LikeEntity> likes;
}
最后,这是我在 index.jsp 文件中的表格,其中显示了信息。
<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>
当我删除${fn:length(mes.likes)}
时,一切正常,但我需要一种方法来获取此信息。
我看过其他类似的问题,但没有一个问题可以解决我的问题。
顺便说一句,我在文件的开头有<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
这是likeEntity类
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);
}
}
答案 0 :(得分:0)
Disclamer :这是一种解决方法,而不是真正的解决方案
我仍然不知道为什么这些喜欢不能懒得加载,但是将fetchtype从懒惰变为渴望似乎已经成功了......
我知道这个解决方案不是最佳的。在我的情况下,这种解决方法是合适的,因为它只是一个学校项目,所以我永远不会有数千个喜欢的消息。
但是,如果您使用大量数据,我认为这种解决方案不合适。