获取具有以下代码的异常。 JSTL显示500找不到属性(如id)。
JSP
<c:forEach var="movie" items="${movies}>">
<tr>
<td><input type="checkbox" name="foo"/></td>
<td>${movie.id}</td>
<%--<td>${movie.year}</td>--%>
<%--<td>${movie.quality}</td>--%>
<%--<td>${movie.size}</td>--%>
<%--<td>${movie.created}</td>--%>
<%--<td>${movie.seed}</td>--%>
<%--<td>${movie.peer}</td>--%>
<td></td>
</tr>
</c:forEach>
控制器
@RequestMapping(value = "/moderator/upload", method = RequestMethod.GET)
public ModelAndView showUploadPage() {
ModelAndView uploadPage = new ModelAndView("upload");
Movie movie = new Movie("Terminator 2", 200, "HD", 20, 24233523, 1991, "now");
List<Movie> movies = new ArrayList<>();
movies.add(movie);
uploadPage.addObject("movies", movies);
movies.forEach(item -> LOGGER.info(item.toString()));
return uploadPage;
}
在控制台(SLF4J)中输出以下内容:
Movie{created='now', id=0, name='Terminator 2', year=1991, quality='HD', size=24233523, seed=20, peer=200}
实体类中的Getters和Setter
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(nullable = false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
实体
@Entity
@Table
public class Movie implements Serializable, Torrent {
private int id;
private String name;
private int year;
private String quality;
private long size;
private String created;
private int seed;
private int peer;
public Movie() {
}
public Movie(String name, int peer, String quality, int seed, long size, int year, String created) {
this.name = name;
this.peer = peer;
this.quality = quality;
this.seed = seed;
this.size = size;
this.year = year;
this.created = created;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(nullable = false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(nullable = false)
public String getQuality() {
return quality;
}
public void setQuality(String quality) {
this.quality = quality;
}
@Column(nullable = false)
public long getSize() {
return size;
}
public void setSize(long size) {
this.size = size;
}
@Column(nullable = false)
public String getCreated() {
return created;
}
public void setCreated(String created) {
this.created = created;
}
@Column(nullable = false)
public int getPeer() {
return peer;
}
public void setPeer(int peer) {
this.peer = peer;
}
@Column(nullable = false)
public int getSeed() {
return seed;
}
public void setSeed(int seed) {
this.seed = seed;
}
@Column(nullable = false)
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Movie movie = (Movie) o;
if (id != movie.id) return false;
if (year != movie.year) return false;
if (size != movie.size) return false;
if (seed != movie.seed) return false;
if (peer != movie.peer) return false;
if (!name.equals(movie.name)) return false;
if (!quality.equals(movie.quality)) return false;
return created.equals(movie.created);
}
@Override
public int hashCode() {
int result = id;
result = 31 * result + name.hashCode();
result = 31 * result + year;
result = 31 * result + quality.hashCode();
result = 31 * result + (int) (size ^ (size >>> 32));
result = 31 * result + created.hashCode();
result = 31 * result + seed;
result = 31 * result + peer;
return result;
}
@Override
public String toString() {
return "Movie{" +
"created='" + created + '\'' +
", id=" + id +
", name='" + name + '\'' +
", year=" + year +
", quality='" + quality + '\'' +
", size=" + size +
", seed=" + seed +
", peer=" + peer +
'}';
}
}
堆栈跟踪
org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/views/upload.jsp at line 77
74: <c:forEach var="movie" items="${movies}>">
75: <tr>
76: <td><input type="checkbox" name="foo"/></td>
77: <td>${movie.id}</td>
78: <%--<td>${movie.year}</td>--%>
79: <%--<td>${movie.quality}</td>--%>
80: <%--<td>${movie.size}</td>--%>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:574)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:476)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:168)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1243)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1027)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:971)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:968)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:859)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:844)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
com.svitsky.filter.CharsetFilter.doFilter(CharsetFilter.java:27)
com.svitsky.filter.ModeratorFilter.doFilter(ModeratorFilter.java:38)
root cause
javax.el.PropertyNotFoundException: Property 'id' not found on type java.lang.String
javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:268)
javax.el.BeanELResolver$BeanProperties.access$300(BeanELResolver.java:221)
javax.el.BeanELResolver.property(BeanELResolver.java:355)
javax.el.BeanELResolver.getValue(BeanELResolver.java:95)
org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:110)
org.apache.el.parser.AstValue.getValue(AstValue.java:169)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:943)
org.apache.jsp.WEB_002dINF.views.upload_jsp._jspx_meth_c_005fforEach_005f0(upload_jsp.java:315)
org.apache.jsp.WEB_002dINF.views.upload_jsp._jspService(upload_jsp.java:205)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:168)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1243)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1027)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:971)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:968)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:859)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:844)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
com.svitsky.filter.CharsetFilter.doFilter(CharsetFilter.java:27)
com.svitsky.filter.ModeratorFilter.doFilter(ModeratorFilter.java:38)
答案 0 :(得分:0)
除了这一行,你的代码还可以:
<c:forEach var="movie" items="${movies}>">
这导致了问题,应该是
<c:forEach var="movie" items="${movies}">
错误我猜错了。