el 表达式 报 java.lang.NumberFormatException: For input string: "xxx"

最近项目里面 忽然一个页面 暴出 如下异常 


Stacktrace:
	org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
	com.sinitek.agent.filter.AgentDispatcherFilter.doFilter(AgentDispatcherFilter.java:93)
	org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:78)
	org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:77)
	org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:413)
	org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:99)
root cause

java.lang.NumberFormatException: For input string: "caption"
	java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
	java.lang.Integer.parseInt(Integer.java:449)
	java.lang.Integer.parseInt(Integer.java:499)
	javax.el.ListELResolver.coerce(ListELResolver.java:163)
	javax.el.ListELResolver.getValue(ListELResolver.java:51)
	org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:104)
	org.apache.el.parser.AstValue.getValue(AstValue.java:183)
	org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
	org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:967)
	org.apache.jsp.agent.pages.personal_005fagent.productdetail_jsp._jspService(productdetail_jsp.java:481)
	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
	com.sinitek.agent.filter.AgentDispatcherFilter.doFilter(AgentDispatcherFilter.java:93)
	org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:78)
	org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:77)
	org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:413)
	org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:99)

一看到这个错 我以为是 fmt:formatNumber 标签 报错,将所有的 fmt:formatNumber 都去除 还是报错!百思不得其解!

最后一段一段代码排除,发现是数据问题! 可是还是觉得不可思议,所以今天继续故意把数据搞错,然后分析为啥会报 NumberFormatException 


最后看异常栈的信息 

java.lang.NumberFormatException: For input string: "caption"
	java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
	java.lang.Integer.parseInt(Integer.java:449)
	java.lang.Integer.parseInt(Integer.java:499)
	javax.el.ListELResolver.coerce(ListELResolver.java:163)
	javax.el.ListELResolver.getValue(ListELResolver.java:51)
	org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:104)
	org.apache.el.parser.AstValue.getValue(AstValue.java:183)
	org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
	org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:967)
	org.apache.jsp.agent.pages.personal_005fagent.productdetail_jsp._jspService(productdetail_jsp.java:481)
	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
	com.sinitek.agent.filter.AgentDispatcherFilter.doFilter(AgentDispatcherFilter.java:93)
	org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:78)
	org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:77)
	org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:413)
	org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:99)

发现  这5行是el 表达式的 解析!

谷歌 找 ListELResovler的源码 

public Object More ...getValue(ELContext context,
157                           Object base,
158                           Object property) {
159
160        if (context == null) {
161            throw new NullPointerException();
162        }
163
164        if (base != null && base instanceof List) {
165            context.setPropertyResolved(true);
166            List list = (List) base;
167            int index = toInteger(property);
168            if (index < 0 || index >= list.size()) {
169                return null;
170            } 
171            return list.get(index);
172        }
173        return null;
174    }

现在报错原因知道了,确认是 转换的时候  调用了  LisELResolver t的 转换!

但是问题来了 为啥 使用 ListELRsolver 进行转换呢?  猜测是因为 结果集是 list 导致的,最后发现果然是list!


${statu.caption}

由于 statu 是一个list 导致  el表达式 认为后面一定跟的是 下标 所以把 capition 转换成 数字 然后异常了!


${statu[0].caption}

这样就ok了! 


所以 NumberFormat 异常 不一定是 format 标签的问题!

你可能感兴趣的:(J2EE)