今天使用jstl的foreach,平时没问题,今天出现如下错误:
javax.el.PropertyNotFoundException: Property 'id' not found on type java.lang.String
javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:193)
javax.el.BeanELResolver$BeanProperties.access$400(BeanELResolver.java:170)
javax.el.BeanELResolver.property(BeanELResolver.java:279)
明明有id属性,类型为int型,且get,set都有,怎么会报错呢?
于是打开tomcat解析后的代码:
private boolean _jspx_meth_c_005fforEach_005f0(PageContext _jspx_page_context)
throws Throwable {
PageContext pageContext = _jspx_page_context;
JspWriter out = _jspx_page_context.getOut();
// c:forEach
org.apache.taglibs.standard.tag.rt.core.ForEachTag _jspx_th_c_005fforEach_005f0 = (org.apache.taglibs.standard.tag.rt.core.ForEachTag) _005fjspx_005ftagPool_005fc_005fforEach_0026_005fvar_005fitems.get(org.apache.taglibs.standard.tag.rt.core.ForEachTag.class);
_jspx_th_c_005fforEach_005f0.setPageContext(_jspx_page_context);
_jspx_th_c_005fforEach_005f0.setParent(null);
// /WEB-INF/view/body.jsp(33,0) name = items type = java.lang.Object reqTime = true required = false fragment = false deferredValue = false expectedTypeName = null deferredMethod = false methodSignature = null
_jspx_th_c_005fforEach_005f0.setItems(new String("notices"));
// /WEB-INF/view/body.jsp(33,0) name = var type = java.lang.String reqTime = false required = false fragment = false deferredValue = false expectedTypeName = null deferredMethod = false methodSignature = null
_jspx_th_c_005fforEach_005f0.setVar("notice");
int[] _jspx_push_body_count_c_005fforEach_005f0 = new int[] { 0 };
try {
int _jspx_eval_c_005fforEach_005f0 = _jspx_th_c_005fforEach_005f0.doStartTag();
if (_jspx_eval_c_005fforEach_005f0 != javax.servlet.jsp.tagext.Tag.SKIP_BODY) {
do {
out.write("\r\n");
out.write("<li><a href=\"");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${pageContext.request.contextPath}", java.lang.String.class, (PageContext)_jspx_page_context, null, false));
out.write("/news/detail?id=");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${notice.id}", java.lang.String.class, (PageContext)_jspx_page_context, null, false)); //注意,此时notice解析类型为String,不是bean,肯定有问题
out.write("\" title=\"");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${notice.title}", java.lang.String.class, (PageContext)_jspx_page_context, null, false));
out.write("\"><font style=color:green>");
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${notice.title}", java.lang.String.class, (PageContext)_jspx_page_context, null, false));
out.write("</font></a></li>\r\n");
int evalDoAfterBody = _jspx_th_c_005fforEach_005f0.doAfterBody();
if (evalDoAfterBody != javax.servlet.jsp.tagext.BodyTag.EVAL_BODY_AGAIN)
break;
} while (true);
}
if (_jspx_th_c_005fforEach_005f0.doEndTag() == javax.servlet.jsp.tagext.Tag.SKIP_PAGE) {
return true;
}
} catch (Throwable _jspx_exception) {
while (_jspx_push_body_count_c_005fforEach_005f0[0]-- > 0)
out = _jspx_page_context.popBody();
_jspx_th_c_005fforEach_005f0.doCatch(_jspx_exception);
} finally {
_jspx_th_c_005fforEach_005f0.doFinally();
_005fjspx_005ftagPool_005fc_005fforEach_0026_005fvar_005fitems.reuse(_jspx_th_c_005fforEach_005f0);
}
return false;
}
发现解析时notice变量被当做String了,不是Javabean对象,于是看代码,发现竟然少写了一个${}符号,stupid!!!
错误写法:
<c:forEach items="notices" var="notice">
<li><a href="${pageContext.request.contextPath}/news/detail/${notice.id}" title="${notice.title}"><font style=color:green>${notice.title}</font></a></li>
</c:forEach>
修正后:
<c:forEach items="${notices}" var="notice">
<li><a href="${pageContext.request.contextPath}/news/detail/${notice.id}" title="${notice.title}"><font style=color:green>${notice.title}</font></a></li>
</c:forEach>
谨以此警告自己,不能再犯此低级错误!