今天做了什么:
使用了springmvc架构进行增删改查
package servlets;
import car.bean.CarInfo;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
* 接受客户端后缀为action的请求,并进行处理,并返回响应
*
* @author Leiyu
* @version 1.0
*
*/
@WebServlet("*.action")
public class AjaxController extends HttpServlet {
private static final long serialVersionUID = 1L;
public AjaxController() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String actionUrl = request.getServletPath(); // 获取客户端的访问URL地址信息
if (actionUrl.equals("/list.action")) { // 查询所有图书
ArrayList<CarInfo> list = CarInfo.getCarList(); // 调用BookInfo的getBookList方法完成
// 使用JSONArray对象将结果构建为json对象并输出到客户端
JSONArray jsonArray = new JSONArray();
for (int i = 0; i < list.size(); i++) {
CarInfo book = list.get(i);
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", book.getId());
map.put("logo", book.getLogo());
map.put("color", book.getColor());
map.put("name", book.getName());
JSONObject BookObj = new JSONObject(map);
jsonArray.put(BookObj);
}
// 向客户端返回json结果
response.getWriter().print(jsonArray.toString());
} else if (actionUrl.equals("/add.action")) { // 增加图书操作
CarInfo bi = new CarInfo();
System.out.println(55);
bi.setLogo(request.getParameter("logo"));
bi.setColor(request.getParameter("color"));
bi.setName(request.getParameter("name"));
int r = CarInfo.addCar(bi); // 调用BookInfo的addBook方法完成
// 向客户端返回结果
response.getWriter().print(r);
} else if (actionUrl.equals("/edit.action")) { // 编辑图书操作
String id = request.getParameter("id");
CarInfo bi = CarInfo.getCarById(id); // 调用BookInfo的getBookById方法完成
// 将该对象构建为json数据
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", bi.getId());
map.put("logo", bi.getLogo());
map.put("color", bi.getColor());
map.put("name", bi.getName());
JSONObject BookObj = new JSONObject(map);
// 向客户端返回结果
response.getWriter().print(BookObj.toString());
} else if (actionUrl.equals("/update.action")) { // 更新图书操作
CarInfo bi = new CarInfo();
bi.setId(request.getParameter("id"));
bi.setLogo(request.getParameter("logo"));
bi.setColor(request.getParameter("color"));
bi.setName(request.getParameter("name"));
int r = CarInfo.updateCar(bi);// 调用BookInfo的updateBook方法完成
response.getWriter().print(r); // 向客户端返回结果
} else if (actionUrl.equals("/delete.action")) { // 删除图书操作
String id = request.getParameter("id");
int r = CarInfo.deleteCar(id); // 调用BookInfo的deleteBook方法完成
response.getWriter().print(r); // 向客户端返回结果
}
}
}
package servlets;
import java.io.IOException;
import java.util.ArrayList;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;
import car.bean.CarInfo;
/**
* 用来接收客户端的后缀为do的请求
*
* @author Leiyu
* @version 1.0
*
*/
@WebServlet("*.do")
public class CarController extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String actionUrl = request.getServletPath(); // 获取客户请求的Servlet地址
if (actionUrl.equals("/index.do")) { // 查询所有图书
ArrayList<CarInfo> list = CarInfo.getCarList(); // 调用BookInfo的getBookList方法查询所有图书,赋值给list
request.setAttribute("list", list); // 在request增加属性list,其结果为list对象
request.getRequestDispatcher("/index.jsp").forward(request, response);// 重定向至index.jsp进行显示
} else if (actionUrl.equals("/addview.do")) { // 新增图书显示页面
request.getRequestDispatcher("add.html").forward(request, response);
} else if (actionUrl.equals("/add.do")) { // 新增图书
CarInfo bi = new CarInfo();
bi.setLogo(request.getParameter("logo"));
bi.setColor(request.getParameter("color"));
bi.setName(request.getParameter("name"));
int r = CarInfo.addCar(bi); // 调用BookInfo的addBook方法完成
if (r == 1)
request.getRequestDispatcher("success.html").forward(request, response); // 成功的话重定向至success.html
else
request.getRequestDispatcher("failure.html").forward(request, response); // 失败的话重定向至failure.html
} else if (actionUrl.equals("/edit.do")) { // 客户端要对指定id的图书进行修改
String id = request.getParameter("id");
System.out.println(id+666);
CarInfo bi = CarInfo.getCarById(id); // 调用BookInfo的getBookById方法获取图书信息,赋值给bi对象
request.setAttribute("bi", bi); // 将bi对象增加到request的属性中
request.getRequestDispatcher("edit.jsp").forward(request, response);// 重定向至edit.jsp进行显示
} else if (actionUrl.equals("/update.do")) { // 用户输入要修改的图书的信息之后需要保存到数据库
CarInfo bi = new CarInfo();
bi.setId(request.getParameter("id"));
bi.setLogo(request.getParameter("Logo"));
bi.setColor(request.getParameter("color"));
bi.setName(request.getParameter("name"));
int r = CarInfo.updateCar(bi);// 调用BookInfo的updateBook方法实现
if (r == 1)
request.getRequestDispatcher("/success.html").forward(request, response);// 成功的话重定向至success.html
else
request.getRequestDispatcher("/failure.html").forward(request, response);// 失败的话重定向至failure.html
} else if (actionUrl.equals("/delete.do")) { // 用户需要删除指定id的图书
String id = request.getParameter("id");
int r = CarInfo.deleteCar(id); // 调用BookInfo的deleteBook方法实现
if (r == 1)
request.getRequestDispatcher("/success.html").forward(request, response);// 成功的话重定向至success.html
else
request.getRequestDispatcher("/failure.html").forward(request, response);// 失败的话重定向至failure.html
}
}
}
遇到了什么困难:
不懂,看视频解决
明天准备做什么:
复习
代码量 200
时间 1.5小时