最近在做项目,用此贴记录遇到的一些小问题
首先需要引入3个包,我看网上有人说只需要两个,但是我没成功,引入3个之后才没问题的
关于json的返回格式,如果想增加内容的话最好用一个map集合包裹住,最近用bootstrap table做分页,要求返回的json格式如下:
也就是说要增加”total”: 792,”rows”:这些东西,后面返回的是对象集合.
{"total": 792,"rows": [{"id":1,"name":"test1","price":"$1"},{"id":2,"name":"test2","price":"$2"},{"id":3,"name":"test3","price":"$3"},{"id":4,"name":"test4","price":"$4"},{"id":5,"name":"test5","price":"$5"},{"id":6,"name":"test6","price":"$6"},{"id":7,"name":"test7","price":"$7"},{"id":8,"name":"test8","price":"$8"},{"id":9,"name":"test9","price":"$9"},{"id":10,"name":"test10","price":"$10"}]}
因此我用map包裹住
@RequestMapping(value = "/getAllUser",method = RequestMethod.POST)
public @ResponseBody Map<String,Object> getAllCumUser(@RequestBody PageUtil pageUtil){
Map<String,Object> model = new HashMap<>();
//这里引入分页机制
PageHelper.startPage(pageUtil.getOffset()/pageUtil.getLimit()+1,pageUtil.getLimit());
List<CumUser> list = cumUserMapper.findAllUser(pageUtil);
PageInfo<CumUser> info = new PageInfo<>(list);
//这样做就可以返回上述那种格式的json了
model.put("total",info.getTotal());
model.put("rows",list);
return model;
}
关于接收json
可以用一个对象类来承载,在里面写上json的key这个属性值,实现set方法,spring MVC会自动注入值的
if (confirm("确定删除该用户?")){
//必须在这里设置一下,不然传输格式有问题
$.ajaxSetup({
contentType : 'application/json'
});
$.ajax(
{
type: 'post',//这里改为get也可以正常执行
url: '/system/cum_user_delete',
contentType:"application/json;charset=utf-8",
data: JSON.stringify(row),
success:function (data) {
if (data.delete == true){
//删除后刷新操作
$("#exampleTableToolbar2").bootstrapTable("refresh",{silent: true});
}else {
alert("系统出错,删除失败,可查看tomcat日志解决")
}
}
}
)
}
对应的spring MVC控制器
@RequestMapping(value = "/cum_user_delete",method = RequestMethod.POST)
public @ResponseBody Map<String,Boolean> deleteUserById(@RequestBody CumUser user){
cumUserMapper.deleteUserById(user.getId());
Map<String,Boolean> maps = new HashMap<>();
maps.put("delete",true);
return maps;
}
最近写项目,表单其他控件都不乱码,就这个textarea有问题,提交方法是post,最后只能手动转码才能解决这个问题
String name= new String(problem.getPro_content().getBytes("ISO8859-1"),"UTF-8");