|
|
package com.inks.hb.authinfo.controller;
|
|
|
//11111
|
|
|
import com.google.gson.Gson;
|
|
|
import com.inks.hb.authinfo.pojo.AuthInfo;
|
|
|
import com.inks.hb.authinfo.service.AuthService;
|
|
|
import com.inks.hb.authinfo.service.AuthServiceImpl;
|
|
|
import com.inks.hb.common.PojotoGson;
|
|
|
|
|
|
import javax.servlet.annotation.WebServlet;
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.IOException;
|
|
|
import java.io.PrintWriter;
|
|
|
import java.sql.SQLException;
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
/**
|
|
|
* 分页查询权限表的Servlet
|
|
|
* 如果查询过程中出现异常,则统一返回'数据查询出现异常'
|
|
|
* 返回的数据类型为PojotoGson
|
|
|
*/
|
|
|
@WebServlet(value = "/AuthInfoServlet", name = "AuthInfoServlet")
|
|
|
public class AuthInfoServlet extends HttpServlet {
|
|
|
// 重写doPost方法,调用doGet方法以处理POST请求
|
|
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
|
this.doGet(request, response);
|
|
|
}
|
|
|
|
|
|
// 重写doGet方法,处理GET请求
|
|
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
|
// 设置请求和响应的字符编码
|
|
|
request.setCharacterEncoding("utf-8");
|
|
|
response.setContentType("text/html;charset=utf-8");
|
|
|
// 获取响应输出流
|
|
|
PrintWriter out = response.getWriter();
|
|
|
|
|
|
// 从请求中获取分页参数和操作类型
|
|
|
int page = Integer.parseInt(request.getParameter("page")); // 当前页码
|
|
|
int limit = Integer.parseInt(request.getParameter("limit")); // 每页的数据量
|
|
|
int make = Integer.parseInt(request.getParameter("make")); // 操作类型
|
|
|
|
|
|
// 创建AuthService实例
|
|
|
AuthService service = new AuthServiceImpl();
|
|
|
|
|
|
// 初始化返回信息
|
|
|
String code = "0"; // 状态码,默认为0表示成功
|
|
|
String msg = "数据查询正常"; // 状态信息,默认为正常
|
|
|
String count = ""; // 数据总数,初始化为空字符串
|
|
|
ArrayList<AuthInfo> list = new ArrayList<>(); // 数据内容列表
|
|
|
|
|
|
// 声明单个全局属性
|
|
|
int authId; // 权限ID
|
|
|
String authItem = ""; // 权限名称
|
|
|
String isRead; // 可读
|
|
|
String isWrite; // 可写
|
|
|
String isChange; // 可改
|
|
|
String isDelete; // 可删
|
|
|
AuthInfo authInfo = null; // AuthInfo对象
|
|
|
|
|
|
try {
|
|
|
// 根据操作类型执行不同的逻辑
|
|
|
if (make == 2) {
|
|
|
// 修改权限信息
|
|
|
authId = Integer.parseInt(request.getParameter("authId"));
|
|
|
authItem = request.getParameter("authItem");
|
|
|
isRead = request.getParameter("isRead");
|
|
|
isWrite = request.getParameter("isWrite");
|
|
|
isChange = request.getParameter("isChange");
|
|
|
isDelete = request.getParameter("isDelete");
|
|
|
authInfo = new AuthInfo(authId, authItem, isRead, isWrite, isChange, isDelete);
|
|
|
service.updateAuthInfo(authInfo); // 更新权限信息
|
|
|
} else if (make == 3) {
|
|
|
// 搜索权限信息
|
|
|
authItem = request.getParameter("authItem");
|
|
|
authInfo = service.query(authItem); // 查询权限信息
|
|
|
list.clear(); // 清空列表
|
|
|
list.add(authInfo); // 添加查询结果到列表
|
|
|
}
|
|
|
|
|
|
// 根据操作类型获取数据
|
|
|
if (make != 3) {
|
|
|
list = service.query(page, limit); // 分页查询权限信息
|
|
|
count = String.valueOf(service.queryAuthInfoNum()); // 获取权限信息总数
|
|
|
} else {
|
|
|
// 如果搜索结果为空,则设置count为0,否则为1
|
|
|
count = (authInfo.getAuthId() == 0) ? "0" : "1";
|
|
|
}
|
|
|
} catch (SQLException e) {
|
|
|
// 捕获到SQLException异常,设置错误状态码和信息
|
|
|
code = "1";
|
|
|
msg = "数据查询出现异常";
|
|
|
e.printStackTrace(); // 打印异常堆栈信息
|
|
|
} finally {
|
|
|
// 创建PojotoGson对象并序列化为JSON字符串
|
|
|
PojotoGson pojotoGson = new PojotoGson(code, msg, count, list);
|
|
|
Gson gson = new Gson();
|
|
|
out.print(gson.toJson(pojotoGson)); // 输出JSON字符串
|
|
|
}
|
|
|
}
|
|
|
}
|