NumberFormatException异常javaWEB,jsp的EL表达式异常,的解决方法

类似于代码如:

String id=req.getParameter("id");
int ID=Integer.valueOf(id);
String sql="SELECT * FROM commodity WHERE id=? ";
Object[] params={ID};
List list=mySql.Query(sql,params,Commodity.class);
req.setAttribute("Commodity", list);
req.getRequestDispatcher("/WEB-INF/jsp/Certain_commodity.jsp").forward(req, resp);


引用的类Commodity:

@Getter@Setter@ToString
public class Commodity {

String commodity_image;

...

}


代码没有问题

jsp:

${Commodity.commodity_image}

当你访问了此url:

严重: Servlet.service() for servlet jsp threw exceptionjava.lang.NumberFormatException: For input string: "commodity_image" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:492) at java.lang.Integer.parseInt(Integer.java:527) at javax.el.ListELResolver.coerce(ListELResolver.java:163) at javax.el.ListELResolver.getValue(ListELResolver.java:51)

...

它报了一个字符串转int类型的异常

但是网页上出现了:

org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/jsp/Certain_commodity.jsp at line 16

13: 
14: 	
15: 		
16: 17:
18:
19: Stacktrace: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:580) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)

	...
Sql语句没有问题经过测试,id=1或者id="1"都可以执行不报错,那么究竟是哪里错了?


解决方案:








加一个forEach就可以了,无论你想获取的是一个值也好多个值也罢,只要你attribute的是一个集合那么必须用forEach遍历取到!

你可能感兴趣的:(java异常,EL表达式异常)