撸完代码,成功运行的那一刻,激动、兴奋、高兴地点了个外卖,,琢磨着要记录下来遇到的坑,但是突然发现。不知道该写什么了。。那就先写几个记忆比较深刻的坑,记录下来提醒自己,以及帮助遇到同样问题的旁友
前面已经配置好其他环境,现在就是在iframe中使用BootStrapTable
用户管理页面:增加,批量删除,修改,分页,修改是使用模态框获取一行数据
爬爬遇到的坑
1、使用BootStrapTable传递到后台时,分页时,queryParam方法传递pageNumber和pageSize传递到后台死活传不过去,找了好久,
method:"POST",
//极为重要,缺失无法执行queryParams,传递page参数
contentType : "application/x-www-form-urlencoded",
dataType:"json",
url:'/user/pageInfo',
queryParams:queryParam,
pagination:true,//显示分页条:页码,条数等
striped:true,//隔行变色
pageNumber:1,//首页页码
pageSize:10,//分页,页面数据条数
method:"POST",
//极为重要,缺失无法执行queryParams,传递page参数
contentType : "application/x-www-form-urlencoded",
dataType:"json",
url:'/user/pageInfo',
queryParams:queryParam,
pagination:true,//显示分页条:页码,条数等
striped:true,//隔行变色
pageNumber:1,//首页页码
pageSize:10,//分页,页面数据条数
加上这句
contentType : "application/x-www-form-urlencoded",
就搞定了,这是我的情况。。
2、BootStrapTable获取一个表单中填入的值
var param = $("#addUserForm").serializeArray();
BootStrapTable获取选中一行数据
var rows = $("#dataGrid").bootstrapTable('getSelections');
//获取选中行的数据
var rows = $("#dataGrid").bootstrapTable('getSelections');
if(rows.length!=1){
document.getElementById("tipContent").innerText="请选择一行数据";
$("#Tip").modal('show');
}
else{
var row = rows[0];
$('#editId').val(row.id);
$('#editAccount').val(row.account);
$('#editPassword').val(row.password);
$('#editName').val(row.name);
$('#editSex').val(row.sex);
$('#editEmail').val(row.email);
$('#editPhone').val(row.phone);
$('#editStates').val(row.states);
$('#editCreated_at').val(row.created_at);
$("#editModal").modal("show");
}
//获取选中行的数据
var rows = $("#dataGrid").bootstrapTable('getSelections');
if(rows.length!=1){
document.getElementById("tipContent").innerText="请选择一行数据";
$("#Tip").modal('show');
}
else{
var row = rows[0];
$('#editId').val(row.id);
$('#editAccount').val(row.account);
$('#editPassword').val(row.password);
$('#editName').val(row.name);
$('#editSex').val(row.sex);
$('#editEmail').val(row.email);
$('#editPhone').val(row.phone);
$('#editStates').val(row.states);
$('#editCreated_at').val(row.created_at);
$("#editModal").modal("show");
}
#editId是模态框里面input的id,row.id中的id是模态框一个input中的name属性。
4、后台传递从数据库查到的数据需要转为json
List
int total = users.size(); PageHelper.startPage(pageNumber,pageSize); Listusers =userService.getUserList(); int total = users.size(); PageHelper.startPage(pageNumber,pageSize); List pageInfo=userService.getUserList(); JSONObject result = new JSONObject(); result.put("rows",pageInfo); result.put("total",total); System.out.println(result.toJSONString()); return result.toJSONString(); pageInfo=userService.getUserList(); JSONObject result = new JSONObject(); result.put("rows",pageInfo); result.put("total",total); System.out.println(result.toJSONString()); return result.toJSONString();
导入import com.alibaba.fastjson.JSONObject;
5、批量删除无非存入多个id,到后台循环调用删除。
function deleteUser(){
var rows = $("#dataGrid").bootstrapTable("getSelections");
var ids = [];
var len = rows.length;
debugger;
for(var i=0;i
后台获取ajax传的数组
String[] list=request.getParameterValues("ids");
6、SpringBoot中templates使用thymeleaf时,<,>都报错,使用了<,用了for,最后还是放到了别的包下面。这样在html里面就不会使用<,>
就记得这么些了。。
贴代码:index.html
Insert title here
用户管理
引入user.js
/**
*
*/
function deleteUser(){
var rows = $("#dataGrid").bootstrapTable("getSelections");
var ids = [];
var len = rows.length;
debugger;
for(var i=0;i
后台Controller
package com.damionew.website.controller.back;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSONObject;
import com.damionew.website.model.back.User;
import com.damionew.website.service.back.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.PageHelper;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/insert")
public void insert(User user) {
System.out.println("before");
userService.addUser(user);
System.out.println("after");
}
@RequestMapping("/index")
public String index(User user) {
return "back/user/index";
}
@ResponseBody
@RequestMapping(value="getUser",produces="html/text;charset=UTF-8")
public String queryUserList() {
List users =userService.getUserList();
System.out.println();
JSONObject result = new JSONObject();
result.put("rows", users);
System.out.println(result.toJSONString());
return result.toJSONString();
}
@ResponseBody
@RequestMapping(value="pageInfo",produces="html/text;charset=UTF-8")
public String pageInfo(@RequestParam int pageNumber,int pageSize,HttpServletResponse response) {
response.setContentType("text/json");
response.setCharacterEncoding("utf-8");
List users =userService.getUserList();
int total = users.size();
PageHelper.startPage(pageNumber,pageSize);
List pageInfo=userService.getUserList();
JSONObject result = new JSONObject();
result.put("rows",pageInfo);
result.put("total",total);
System.out.println(result.toJSONString());
return result.toJSONString();
}
@ResponseBody
@RequestMapping("/addUser")
public String addUser(User user) {
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
String time = dateFormat.format(date);
user.setCreated_at(time);
user.setStates("1");
System.out.println(user.getName());
userService.addUser(user);
System.out.println("success");
JSONObject result = new JSONObject();
result.put("state", "success");
return result.toJSONString();
}
@ResponseBody
@RequestMapping("/updateUser")
public String updateUser(User user) {
userService.updateUser(user);
JSONObject result = new JSONObject();
result.put("state", "success");
return result.toJSONString();
}
@ResponseBody
@RequestMapping("/deleteUser")
public String deleteUser(HttpServletRequest request) {
String[] list=request.getParameterValues("ids");
System.out.println(list);
for (int i = 0; i < list.length; i++) {
String id = list[i];
System.out.println(id);
userService.deleteUser(id);
}
JSONObject result = new JSONObject();
result.put("state", "success");
return result.toJSONString();
}
}
UserMapper.xml
很多朋友留言要源码,已经上传到码云: https://gitee.com/Damionew/website.git
至于其他的都放在GitHub上面了,毕竟2018年底可以创建免费个人私有仓库啦