前端代码:
编号 | 姓名 | 性别 | 操作 |
效果:
后台代码:
package controller;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.TestUserDao;
import vo.TestUser;
/**
* Servlet implementation class TestUserControl
*/
@WebServlet("/TestUserControl")
public class TestUserControl extends HttpServlet {
private static final long serialVersionUID = 1L;
private TestUserDao tudao=new TestUserDao();
/**
* Default constructor.
*/
public TestUserControl() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置相应编码以及响应json对象
request.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
response.setCharacterEncoding("utf-8");
String action=request.getParameter("action");
switch(action){
case"getAllUserInfo":
List
//向前台响应
response.getWriter().print(JsonUtils.Utils.toJson(list));
;break;
case"getone":;break;
case"deleteOne":
int uid=Integer.parseInt(request.getParameter("uid"));
tudao.delete(uid);
response.getWriter().print("{\"msg\":\"删除成功!\"}");
;break;
case"addOneData":
String uname=request.getParameter("uname");
String usex=request.getParameter("usex");
if(tudao.insert(new TestUser(uname,usex))>0){
response.getWriter().print("{\"msg\":\"添加成功!\"}");
}else{
response.getWriter().print("{\"msg\":\"添加失败!\"}");
};break;
case"updateOneData":
int uuid=Integer.parseInt(request.getParameter("uid"));
String uuname=request.getParameter("uname");
String uusex=request.getParameter("usex");
if(tudao.update(new TestUser(uuid,uuname,uusex))>0){
response.getWriter().print("{\"msg\":\"修改成功!\"}");
}else{
response.getWriter().print("{\"msg\":\"修改失败!\"}");
}
break;
default:;break;
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
心得:该例子采用了artDialog 框架以及采用了张果老师传授的封装数据的理念,在前端只对外只暴露一个方法,有效的保护了数据的安全性以及严谨性。
artDialog下载:http://aui.github.io/artDialog/