求大神解答!!!!String转int错误

2014似水流年2014 2016-06-05 10:21:17
我是用structs2框架写的
我的jsp页面代码
<tr>
<td width=40%>id:</td>
<td><input type="text" name="id" value="${user.id}"></td>
</tr>
<tr>
<td>用户名:</td>
<td><input type="text" name="username" value="${user.username}" ></td>
后台action代码:
public String update() throws Exception{
String username=request.getParameter("username");
String password=request.getParameter("password");
//int id=Integer.parseInt(""+request.getParameter("id"));
//int id= request.getParameter("id");
//System.out.println(id);
/*int id=Integer.parseInt(request.getParameter("id"));*/
int id=0;
try {id=Integer.parseInt(request.getParameter("id"));
}
catch(NumberFormatException e)
{//i=0;
e.printStackTrace();
}
UserDao userdao=new UserDao();
User u=new User();
u.setId(id);
u.setUsername(username);
u.setPassword(password);
userdao.update(u);
return SUCCESS;

}

当进行更新的时候老是报错,
六月 05, 2016 10:11:13 上午 org.apache.catalina.core.ApplicationDispatcher invoke
严重: Servlet.service() for servlet jsp threw exception
java.lang.NumberFormatException: For input string: "id"
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:173)
at javax.el.ListELResolver.getValue(ListELResolver.java:52)
at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:67)
at org.apache.el.parser.AstValue.getValue(AstValue.java:183)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:986)
at org.apache.jsp.updateUser_jsp._jspService(updateUser_jsp.java:95)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:487)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:412)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
at org.apache.struts2.dispatcher.ServletDispatcherResult.doExecute(ServletDispatcherResult.java:159)
at org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186)
at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:373)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:277)
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:133)
...全文
572 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 26 楼 antiwise 的回复:
http://www.myexception.cn/program/968935.html 跟你的错误一模一样,去瞅瞅吧
解决了 是这个问题 感谢!!
啊吼吼 2016-06-07
  • 打赏
  • 举报
回复
你的id传入的时候为空了吧 int 0 Integer null request.getParameter("id") 为空的时候会报你那个错误 你应该加一个判断
OPPPPOP 2016-06-07
  • 打赏
  • 举报
回复
http://www.myexception.cn/program/968935.html 跟你的错误一模一样,去瞅瞅吧
  • 打赏
  • 举报
回复
引用 24 楼 u013779774 的回复:
[quote=引用 22 楼 antiwise 的回复:] 很明显是jsp页面里的EL表达式写错了,比如${xx.id} 而你写成了'id',或者这个实体的setId方法, 可能成了 this.id = "id"
引用 20 楼 qnmdcsdn 的回复:
[quote=引用 19 楼 u013779774 的回复:] [quote=引用 17 楼 u013779774 的回复:] [quote=引用 12 楼 u011320740 的回复:] [quote=引用 11 楼 w1530520334 的回复:] [quote=引用 10 楼 u011320740 的回复:] [quote=引用 9 楼 w1530520334 的回复:] [quote=引用 8 楼 u011320740 的回复:] 我很不明白你修改为什么是new了一个user新对象?
User u=new User();
u.setId(id);
u.setUsername(username);
u.setPassword(password);
userdao.update(u);
应该是先去数据库通过id查找出来这个对象然后再set值修改才对吧
他前台传至 是{user.id } 不是应该 直接有对象有值吗 你这样是NEW了个新对象 穿进去的话 useranme应该是空 pwd 也应该是空 值有ID 是你自己写的[/quote] 晕,那段代码我copy你的呀,你写的呀[/quote] 你看错了吧。[/quote] 当我没说吧[/quote] ------------------------------------------------------------------------------------------------------------------------------------------------------------------- 已经用方法取到id传到页面了,现在从页面取过来的,id打印出来是正常的,后台数据也更新成功了,就是页面报错。 -------------------------------------------------------------------------------------------------------------------------------------------------------------------- [/quote] -------------------------------------------------------------------------------------------------------------------------------- 全打印出来都是正常的,谁能告诉我到底是哪错了??[/quote] 应该是你告诉我们哪错了,走到这不还很正常的,接下来报什么错了,你倒是说说,难道还是那个类型转换错误?[/quote] 对,还是那个错[/quote] 我觉得你可以把程序传上来让大家看看,这样一点点猜比较费劲
Intboy 2016-06-06
  • 打赏
  • 举报
回复
把你的id打印出来看是什么不就好了,或者直接debug啊。。。。
w1530520334 2016-06-06
  • 打赏
  • 举报
回复
引用 12 楼 u011320740 的回复:
[quote=引用 11 楼 w1530520334 的回复:] [quote=引用 10 楼 u011320740 的回复:] [quote=引用 9 楼 w1530520334 的回复:] [quote=引用 8 楼 u011320740 的回复:] 我很不明白你修改为什么是new了一个user新对象?
User u=new User();
u.setId(id);
u.setUsername(username);
u.setPassword(password);
userdao.update(u);
应该是先去数据库通过id查找出来这个对象然后再set值修改才对吧
他前台传至 是{user.id } 不是应该 直接有对象有值吗 你这样是NEW了个新对象 穿进去的话 useranme应该是空 pwd 也应该是空 值有ID 是你自己写的[/quote] 晕,那段代码我copy你的呀,你写的呀[/quote] 你看错了吧。[/quote] 当我没说吧[/quote] 哥们 抱歉我的意思hi 我引用错人了- - 。。
love蕾2012 2016-06-06
  • 打赏
  • 举报
回复
是不是应该把ID用String打印出来看一下,貌似是ID有问题
菜 头 2016-06-06
  • 打赏
  • 举报
回复
菜 头 2016-06-06
  • 打赏
  • 举报
回复
引用 11 楼 w1530520334 的回复:
[quote=引用 10 楼 u011320740 的回复:] [quote=引用 9 楼 w1530520334 的回复:] [quote=引用 8 楼 u011320740 的回复:] 我很不明白你修改为什么是new了一个user新对象?
User u=new User();
u.setId(id);
u.setUsername(username);
u.setPassword(password);
userdao.update(u);
应该是先去数据库通过id查找出来这个对象然后再set值修改才对吧
他前台传至 是{user.id } 不是应该 直接有对象有值吗 你这样是NEW了个新对象 穿进去的话 useranme应该是空 pwd 也应该是空 值有ID 是你自己写的[/quote] 晕,那段代码我copy你的呀,你写的呀[/quote] 你看错了吧。[/quote] 当我没说吧
w1530520334 2016-06-06
  • 打赏
  • 举报
回复
引用 10 楼 u011320740 的回复:
[quote=引用 9 楼 w1530520334 的回复:] [quote=引用 8 楼 u011320740 的回复:] 我很不明白你修改为什么是new了一个user新对象?
User u=new User();
u.setId(id);
u.setUsername(username);
u.setPassword(password);
userdao.update(u);
应该是先去数据库通过id查找出来这个对象然后再set值修改才对吧
他前台传至 是{user.id } 不是应该 直接有对象有值吗 你这样是NEW了个新对象 穿进去的话 useranme应该是空 pwd 也应该是空 值有ID 是你自己写的[/quote] 晕,那段代码我copy你的呀,你写的呀[/quote] 你看错了吧。
菜 头 2016-06-06
  • 打赏
  • 举报
回复
引用 9 楼 w1530520334 的回复:
[quote=引用 8 楼 u011320740 的回复:] 我很不明白你修改为什么是new了一个user新对象?
User u=new User();
u.setId(id);
u.setUsername(username);
u.setPassword(password);
userdao.update(u);
应该是先去数据库通过id查找出来这个对象然后再set值修改才对吧
他前台传至 是{user.id } 不是应该 直接有对象有值吗 你这样是NEW了个新对象 穿进去的话 useranme应该是空 pwd 也应该是空 值有ID 是你自己写的[/quote] 晕,那段代码我copy你的呀,你写的呀
w1530520334 2016-06-06
  • 打赏
  • 举报
回复
引用 8 楼 u011320740 的回复:
我很不明白你修改为什么是new了一个user新对象?
User u=new User();
u.setId(id);
u.setUsername(username);
u.setPassword(password);
userdao.update(u);
应该是先去数据库通过id查找出来这个对象然后再set值修改才对吧
他前台传至 是{user.id } 不是应该 直接有对象有值吗 你这样是NEW了个新对象 穿进去的话 useranme应该是空 pwd 也应该是空 值有ID 是你自己写的
菜 头 2016-06-06
  • 打赏
  • 举报
回复
我很不明白你修改为什么是new了一个user新对象?
User u=new User();
u.setId(id);
u.setUsername(username);
u.setPassword(password);
userdao.update(u);
应该是先去数据库通过id查找出来这个对象然后再set值修改才对吧
  • 打赏
  • 举报
回复
引用 5 楼 u013779774 的回复:
[quote=引用 3 楼 a241903820 的回复:] java.lang.NumberFormatException 这句话明确告诉了我们是数字格式异常,接着后面有 For input string: "id" 即是id的值转int类型出错 , 或者因为里面有其他字符不能转int , 或者有空格 , 你试试把这句改为
id=Integer.parseInt(request.getParameter("id").trim());
还是不行,后台修改数据成功了,但是报错,id输出也正常[/quote] 后台数据修改成功了,再报错就是其他地方的问题了吧
  • 打赏
  • 举报
回复
引用 22 楼 antiwise 的回复:
很明显是jsp页面里的EL表达式写错了,比如${xx.id} 而你写成了'id',或者这个实体的setId方法, 可能成了 this.id = "id"
引用 20 楼 qnmdcsdn 的回复:
[quote=引用 19 楼 u013779774 的回复:] [quote=引用 17 楼 u013779774 的回复:] [quote=引用 12 楼 u011320740 的回复:] [quote=引用 11 楼 w1530520334 的回复:] [quote=引用 10 楼 u011320740 的回复:] [quote=引用 9 楼 w1530520334 的回复:] [quote=引用 8 楼 u011320740 的回复:] 我很不明白你修改为什么是new了一个user新对象?
User u=new User();
u.setId(id);
u.setUsername(username);
u.setPassword(password);
userdao.update(u);
应该是先去数据库通过id查找出来这个对象然后再set值修改才对吧
他前台传至 是{user.id } 不是应该 直接有对象有值吗 你这样是NEW了个新对象 穿进去的话 useranme应该是空 pwd 也应该是空 值有ID 是你自己写的[/quote] 晕,那段代码我copy你的呀,你写的呀[/quote] 你看错了吧。[/quote] 当我没说吧[/quote] ------------------------------------------------------------------------------------------------------------------------------------------------------------------- 已经用方法取到id传到页面了,现在从页面取过来的,id打印出来是正常的,后台数据也更新成功了,就是页面报错。 -------------------------------------------------------------------------------------------------------------------------------------------------------------------- [/quote] -------------------------------------------------------------------------------------------------------------------------------- 全打印出来都是正常的,谁能告诉我到底是哪错了??[/quote] 应该是你告诉我们哪错了,走到这不还很正常的,接下来报什么错了,你倒是说说,难道还是那个类型转换错误?[/quote] 对,还是那个错
OPPPPOP 2016-06-06
  • 打赏
  • 举报
回复
很明显是jsp页面里的EL表达式写错了,比如${xx.id} 而你写成了'id',或者这个实体的setId方法, 可能成了 this.id = "id"
  • 打赏
  • 举报
回复
java.lang.NumberFormatException: For input string: "id" 很明显了啊 你字符串的内容就是“id”,怎么转int? 你看看是不是传参搞错了
  • 打赏
  • 举报
回复
引用 19 楼 u013779774 的回复:
[quote=引用 17 楼 u013779774 的回复:] [quote=引用 12 楼 u011320740 的回复:] [quote=引用 11 楼 w1530520334 的回复:] [quote=引用 10 楼 u011320740 的回复:] [quote=引用 9 楼 w1530520334 的回复:] [quote=引用 8 楼 u011320740 的回复:] 我很不明白你修改为什么是new了一个user新对象?
User u=new User();
u.setId(id);
u.setUsername(username);
u.setPassword(password);
userdao.update(u);
应该是先去数据库通过id查找出来这个对象然后再set值修改才对吧
他前台传至 是{user.id } 不是应该 直接有对象有值吗 你这样是NEW了个新对象 穿进去的话 useranme应该是空 pwd 也应该是空 值有ID 是你自己写的[/quote] 晕,那段代码我copy你的呀,你写的呀[/quote] 你看错了吧。[/quote] 当我没说吧[/quote] ------------------------------------------------------------------------------------------------------------------------------------------------------------------- 已经用方法取到id传到页面了,现在从页面取过来的,id打印出来是正常的,后台数据也更新成功了,就是页面报错。 -------------------------------------------------------------------------------------------------------------------------------------------------------------------- [/quote] -------------------------------------------------------------------------------------------------------------------------------- 全打印出来都是正常的,谁能告诉我到底是哪错了??[/quote] 应该是你告诉我们哪错了,走到这不还很正常的,接下来报什么错了,你倒是说说,难道还是那个类型转换错误?
  • 打赏
  • 举报
回复
引用 17 楼 u013779774 的回复:
[quote=引用 12 楼 u011320740 的回复:]
[quote=引用 11 楼 w1530520334 的回复:]
[quote=引用 10 楼 u011320740 的回复:]
[quote=引用 9 楼 w1530520334 的回复:]
[quote=引用 8 楼 u011320740 的回复:]
我很不明白你修改为什么是new了一个user新对象?
User u=new User();
u.setId(id);
u.setUsername(username);
u.setPassword(password);
userdao.update(u);

应该是先去数据库通过id查找出来这个对象然后再set值修改才对吧

他前台传至 是{user.id } 不是应该 直接有对象有值吗 你这样是NEW了个新对象 穿进去的话 useranme应该是空 pwd 也应该是空 值有ID 是你自己写的[/quote]
晕,那段代码我copy你的呀,你写的呀[/quote] 你看错了吧。[/quote]
当我没说吧[/quote]
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
已经用方法取到id传到页面了,现在从页面取过来的,id打印出来是正常的,后台数据也更新成功了,就是页面报错。
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
[/quote]
--------------------------------------------------------------------------------------------------------------------------------

全打印出来都是正常的,谁能告诉我到底是哪错了??
  • 打赏
  • 举报
回复
引用 16 楼 fengspg 的回复:
把你的id打印出来看是什么不就好了,或者直接debug啊。。。。
id打印出来是正常的,后台数据也更新成功了,就是页面报错
加载更多回复(7)

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧