javascript - Spring MVC对象属性为数组,js 传递数组参数问题
PHP中文网
PHP中文网 2017-04-11 10:35:35
[JavaScript讨论组]

Exception:java.lang.NumberFormatException: For input string: ""

已爬过stackoverflow无果,求指点
使用js Onject打包数据传递参数Code:

    function bindData(){
        viewModel.title = $('#title').val();
        if(viewModel.title==null||$.trim(viewModel.title).length<=0){
            $.toast("标题不能为空!");return false;
        }
        viewModel.content = $('#description').val();
        if(viewModel.content==null||$.trim(viewModel.content).length<=0){
            $.toast("描述不能为空!");return false;
        }
        var imgIds = [];
        $('#imageForm .imageIdHold').forEach(function(e,index){
            var id = $(e).val();
            imgIds.push(id);
        });
        if(imgIds.length>0){viewModel.imgIds =  imgIds;}
        return true;
    }

异步提交Code:

$('#btnSubmit button').click(function(){
            var isValid = bindData();
            $.showIndicator();
            if(isValid){
                $.ajax({
                    type:"post",
                    dataType:'json',
                    url:'/wx/feedback/save',
                    data:viewModel,
                    success:function(data){
                        $.hideIndicator();
                        if(data.success){
                            $.toast("反馈已提交,我们会尽快解决问题!");
                            setTimeout(function(){
                                location.href="/wx/feedback";
                            },2000);
                        }else{
                             $.toast("反馈提交失败!");
                        }
                    },error:function(XMLHttpRequest, textStatus, errorThrown){
                    $.hideIndicator();
                    $.toast("网络交互异常!");}
                });
            }
        });

后台接收Action:

    @RequestMapping(value="/save")
    @ResponseBody
    public Json save(FeedbackDto dto){
        Json json = new Json();
        json.setSuccess(false);
        UserDto user = super.getCurrentUser();
        try {
            boolean isSuccess = feedbackService.save(user,dto);
            json.setSuccess(isSuccess);
        }catch(Exception ex){
            logger.error("feedback wx indexController save Exception",ex);
            json.setMsg(ex.getMessage());
        }
        return json;
    }

FeedbackDto 实体代码:

public class FeedbackDto extends AbstractDto {

    private Long id;

    private String title;

    private String content;

    private User user;

    private Long userId;

    private FeedbackStatus status;

    private FeedbackType type;

    private String[] imgIds;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getContent() {
        return content;
    }

    public FeedbackType getType() {
        return type;
    }

    public void setType(FeedbackType type) {
        this.type = type;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public FeedbackStatus getStatus() {
        return status;
    }

    public void setStatus(FeedbackStatus status) {
        this.status = status;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String[] getImgIds() {
        return imgIds;
    }

    public void setImgIds(String[] imgIds) {
        this.imgIds = imgIds;
    }
}

异常信息:

java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[na:1.8.0_91]
    at java.lang.Integer.parseInt(Integer.java:592) ~[na:1.8.0_91]
    at java.lang.Integer.parseInt(Integer.java:615) ~[na:1.8.0_91]
    at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:327) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:280) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:95) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.validation.DataBinder.applyPropertyValues(DataBinder.java:834) ~[spring-context-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.validation.DataBinder.doBind(DataBinder.java:730) ~[spring-context-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.web.bind.WebDataBinder.doBind(WebDataBinder.java:189) ~[spring-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
PHP中文网
PHP中文网

认证高级PHP讲师

全部回复(0)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号