Jfinal 异常: java.lang.NumberFormatException: For input string: "id"

andying 发布于 2013/12/15 12:19
阅读 4K+
收藏 1

我把官方的demo改了改,想加个Author,然后想在“修改”页面中输出‘作者姓名’及‘作者性别‘,以此来练习一下多表关联。

BlogController中的edit方法更改如下: 

public void edit() {
  //setAttr("blog", Blog.dao.findById(getParaToInt()));
  String sqlString="select t1.*,t2.name as authorName,t2.sex as authorSex"
             +  "     from blog t1  left join author t2 on t1.author_id=t2.id "
             +  "     where  t1.id=? ";
  setAttr("blog", Blog.dao.find(sqlString, getParaToInt()+""))  ;
 
 }

运行报如下错误:

Jfinal 异常: java.lang.NumberFormatException: For input string: "id"

请问应如何更改?还不是明白,jfinal中是如何取参数值的,这个id值应如何取呢?且又是什么数据类型呢?

 

加载中
0
缪斯的情人
缪斯的情人
getPara("id")即可,默认的httpRequest请求的参数都是string类型,这也是http标准
0
andying
andying

还是不行,报这个异常 :

2013-12-15 12:58:22
[ERROR]-[Thread: http-8080-4]-[com.jfinal.core.ActionHandler.handle()]: /blog/edit/3
com.jfinal.render.RenderException: org.apache.jasper.JasperException: java.lang.NumberFormatException: For input string: "id"
at com.jfinal.render.JspRender.render(JspRender.java:58)
at com.jfinal.core.ActionHandler.handle(ActionHandler.java:92)
at com.jfinal.core.JFinalFilter.doFilter(JFinalFilter.java:72)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
Caused by: org.apache.jasper.JasperException: java.lang.NumberFormatException: For input string: "id"
at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:522)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:416)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:630)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
at com.jfinal.render.JspRender.render(JspRender.java:56)
... 14 more
Caused by: java.lang.NumberFormatException: For input string: "id"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:447)
at java.lang.Integer.parseInt(Integer.java:497)
at javax.el.ListELResolver.coerce(ListELResolver.java:166)
at javax.el.ListELResolver.getValue(ListELResolver.java:51)
at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:53)
at org.apache.el.parser.AstValue.getValue(AstValue.java:114)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:923)
at org.apache.jsp.blog.edit_jsp._jspService(edit_jsp.java:100)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
... 24 more


0
JFinal
JFinal
仔细看一下异常输出, 是 jsp 页面读取 id 值时有错误,先注释掉页面中有关 id 值的读取就清楚了
andying
andying
页面中读取id取有问题?感 觉是这句有问题: setAttr("blog", Blog.dao.find(sql, getParaToInt())) ;
0
andying
andying

找到原因了,改成findFirst()方法就行了。

setAttr("blog", Blog.dao.findFirst(sqlString, getParaToInt()+""))  ;

find()返回的是List<Blog>,而编辑时是一个Blog实体。

通过这个练习发现jfinal确实很方便,对于我这种偏好写原生sql语句真的很方便,特别企业级开发太多表要关联了,但我还有一点不明白,如果这种多表关联查出的实体,jfinal对实体update时是如何区分关联表字段的?因为关联表字段只是显示的时候需要,但在修改更新时是不需要保存至后台的,如这句:

getModel(Blog.class).update();



0
andying
andying

      经测试发现jfinal在表单提交时,只要在表单中用<input > 标记了的属性都会被作为原表的一个字段来更新,在关联表操作时,如上例中t2.authorName中就会被当成blog表的authorName来更新:

<label>作者</label>
<input type="text" name="blog.authorName" value="${blog.authorName}" />${titleMsg}

所以要换成其它标记(<label>).但在主子表关系的表单中,如“入库单”,介面上部分是入库单头信息,下部分是入库单的物料信息,此部分又是一个N多的关联查询语句,查询出来结果用<table>标签显示,这样的一种应用,我要新增一张入库单,下部分的入库单明细数据jfinal如何来处理保存数据至后台的呢?是如何跳过关联表的相应字段的(关联字段只是显示时需要)?


返回顶部
顶部