viperson 2010-05-06 22:23
浏览 267
已采纳

Struts怎么捕获NumberFormatException

大家好,我试了两天
 Struts2.18  myEclipse8.0

我自定义了一个类型转换器[code="java"]

public Object convertFromString(Map arg0, String[] arg1, Class arg2) {
Integer inte=null;
for(int i=0;i<arg1.length;i++)
{
String [] tempStr = arg1[i].split("-");
StringBuilder sb = new StringBuilder();
for(int j=0;j< tempStr.length;j++)
sb.append(tempStr[j]);

        inte =new Integer(sb.toString());


    }

    return inte;}

[/code]
当我传入正确的参数时 他能正常的工作 但是当我输入字母时 如aaa 时.后台就会打印一大条的错误
ognl.MethodFailedException: Method "setId" failed for object com.wugang.User@1e9c82e [java.lang.NoSuchMethodException: com.wugang.User.setId([Ljava.lang.String;)]
at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1265)
at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:1454)
at ognl.ObjectPropertyAccessor.setPossibleProperty(ObjectPropertyAccessor.java:85)
at ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:162)
at com.opensymphony.xwork2.ognl.accessor.ObjectAccessor.setProperty(ObjectAccessor.java:28)
at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:2225)
at ognl.ASTProperty.setValueBody(ASTProperty.java:127)
at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:220)
at ognl.SimpleNode.setValue(SimpleNode.java:301).....
java.lang.NoSuchMethodException: com.wugang.User.setId([Ljava.lang.String;)
at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1206)
at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:1454)
at ognl.ObjectPropertyAccessor.setPossibleProperty(ObjectPropertyAccessor.java:85)
at ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:162)
at com.opensymphony.xwork2.ognl.accessor.ObjectAccessor.setProperty(ObjectAccessor.java:28)
at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:2225)
at ognl.ASTProperty.setValueBody(ASTProperty.java:127)
at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:220)
at ognl.SimpleNode.setValue(SimpleNode.java:301)
at ognl.ASTChain.setValueBody(ASTChain.java:227)

当然 我应用了conversionError 拦截器 而且filederrors 字段里有错误的信息 但为什么后台还有错误呢 不是被拦截器捕获了后台就应该没有错了么 还是我自己理解错了 求大家帮帮我

  • 写回答

4条回答 默认 最新

  • 拽拽的初行者 2010-05-07 13:01
    关注

    [color=blue][b]捕获异常,然后抛出[/b][/color]
    [code="java"]try{
    inte =new Integer(sb.toString());

    }catch(NumberFormatException numb){
    throw new com.opensymphony.xwork2.conversion.TypeConversionException("Failed")
    }[/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格