|
|
package servlet.admin;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.io.PrintWriter;
|
|
|
import java.sql.SQLException;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
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 javabean.Admin;
|
|
|
import net.sf.json.JSONArray;
|
|
|
import net.sf.json.JSONObject;
|
|
|
|
|
|
/**
|
|
|
* Servlet 实现类,用于管理员获取书籍列表
|
|
|
*
|
|
|
* 该 Servlet 处理管理员请求书籍列表的操作,支持分页、过滤条件查询。
|
|
|
* 返回符合条件的书籍数据以及总数,并以 JSON 格式返回给客户端。
|
|
|
*/
|
|
|
@WebServlet("/admin/bookList")
|
|
|
public class BookList extends HttpServlet {
|
|
|
|
|
|
/**
|
|
|
* 处理 GET 请求,获取书籍列表
|
|
|
*
|
|
|
* 该方法接受分页、限制和查询条件的参数,查询数据库中的书籍数据,
|
|
|
* 并将查询结果以 JSON 格式返回给客户端。
|
|
|
*
|
|
|
* @param req 请求对象
|
|
|
* @param resp 响应对象
|
|
|
* @throws ServletException 如果发生 Servlet 错误
|
|
|
* @throws IOException 如果发生 I/O 错误
|
|
|
*/
|
|
|
@Override
|
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
// 设置响应内容类型为 JSON,字符编码为 UTF-8
|
|
|
resp.setContentType("application/json; charset=utf8");
|
|
|
|
|
|
// 初始化响应的 JSON 对象
|
|
|
JSONObject json = new JSONObject();
|
|
|
|
|
|
// 定义返回的数据和状态码
|
|
|
String result = null;
|
|
|
Map<String, Object> map = null;
|
|
|
int code = 1;
|
|
|
String msg = "";
|
|
|
String data = "";
|
|
|
|
|
|
// 获取分页参数,默认值为 page=1 和 limit=10
|
|
|
String page = (String) req.getParameter("page");
|
|
|
String limit = (String) req.getParameter("limit");
|
|
|
|
|
|
// 获取查询条件参数(如果有)
|
|
|
String condition = (String) req.getParameter("condition");
|
|
|
String conditionValue = (String) req.getParameter("conditionValue");
|
|
|
|
|
|
// 创建条件过滤的 Map 对象
|
|
|
Map<String, String> where = new HashMap<String, String>();
|
|
|
|
|
|
// 如果没有传递分页参数,则默认设置为第一页和每页10条记录
|
|
|
if(page == null) {
|
|
|
page = "1";
|
|
|
}
|
|
|
if(limit == null) {
|
|
|
limit = "10";
|
|
|
}
|
|
|
|
|
|
// 如果传递了查询条件参数,则将其加入查询条件 Map 中
|
|
|
if(condition == null || conditionValue == null || condition.isEmpty() || conditionValue.isEmpty()) {
|
|
|
condition = null;
|
|
|
conditionValue = null;
|
|
|
} else {
|
|
|
where.put("condition", condition);
|
|
|
where.put("conditionValue", conditionValue);
|
|
|
}
|
|
|
|
|
|
// 实例化 Admin 对象来调用获取书籍列表的方法
|
|
|
Admin admin = new Admin();
|
|
|
try {
|
|
|
// 调用 getBookList 方法来获取书籍列表数据
|
|
|
map = admin.getBookList(page, limit, where);
|
|
|
result = (String) map.get("data"); // 获取查询结果
|
|
|
} catch (ClassNotFoundException | SQLException e) {
|
|
|
// 捕获数据库操作异常,设置错误消息
|
|
|
msg = "数据库获取信息失败";
|
|
|
}
|
|
|
|
|
|
// 如果没有获取到数据或数据为空,返回错误消息
|
|
|
if(result == null || result.isEmpty() || result.equals("1")) {
|
|
|
json.put("code", 1);
|
|
|
json.put("msg", "数据为空");
|
|
|
} else {
|
|
|
// 获取到数据,返回成功状态,并包含查询结果
|
|
|
json.put("code", 0);
|
|
|
json.put("msg", "success");
|
|
|
json.put("count", map.get("count")); // 返回总记录数
|
|
|
result = "[" + result + "]"; // 格式化数据为 JSON 数组
|
|
|
json.put("data", result); // 返回数据
|
|
|
}
|
|
|
|
|
|
// 将 JSON 响应数据写入输出流
|
|
|
PrintWriter out = resp.getWriter();
|
|
|
out.print(json.toString());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 处理 POST 请求,直接调用 doGet 方法来获取书籍列表
|
|
|
*
|
|
|
* @param req 请求对象
|
|
|
* @param resp 响应对象
|
|
|
* @throws ServletException 如果发生 Servlet 错误
|
|
|
* @throws IOException 如果发生 I/O 错误
|
|
|
*/
|
|
|
@Override
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
doGet(req, resp); // 对于 POST 请求直接调用 GET 请求处理方法
|
|
|
}
|
|
|
}
|