已爬过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]
我自己也遇到这个问题,没找到完美的解决方法,只好这样处理了
JS:
Java:
其实不用这样转的,我也差点就这么搞了,哈哈
js:
var arrays = [];
arrays.push(1);
arrays.push(2);
arrays.push(3);
@RequestMapping(value = "/dosth")
@ResponseBody
public String GetArrays(HttpServletRequest request,@RequestParam("arrays[]") List<Integer> arrays)
这样controller就可以注入arrays了,开始的时候@RequestParm(“arrays”)没有加数组标识符“[]”所以一直报“For input string: ""”
其实jquery传参为json的时候在json中封装了arrays这个Controller也是可以将arrays的值注入映射进来的