📄 tr_trainscoreservlet.java
字号:
package com.galaxy.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.galaxy.dao.TrainRecordDAO;
import com.galaxy.dao.UserInfoDAO;
import com.galaxy.dao.TrUiRsDAO;
import com.galaxy.vo.TrainRecordVO;
import com.galaxy.vo.UserInfoVO;
import com.galaxy.vo.TrUiRsIdVO;
import com.galaxy.vo.TrUiRsVO;
import com.galaxy.util.PageHelp;
public class TR_TrainScoreServlet extends HttpServlet {
public TR_TrainScoreServlet() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
}
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("gb2312");
response.setCharacterEncoding("gb2312");
String opraParam = request.getParameter("opraParam");
String condition = request.getParameter("condition");
String errorMsg = "";// 用于在页面上输出错误信息
PrintWriter out = response.getWriter();
int pageSize = 5;// 设置单页显示条数
String currentPage = "1";// 当前显示的页面号
TrUiRsDAO turDao = new TrUiRsDAO();
PageHelp pagehelp = new PageHelp();// 实例化PageHelp对象pageHelp
// list分支显示培训记录列表
if ("list".equals(opraParam)) {
pagehelp = turDao.getTrainScoreList(condition, pageSize, Integer
.parseInt(request.getParameter("currentPage")));
// 转发至列表页面
request.setAttribute("pagehelp", pagehelp);
request.setAttribute("currentPage", request
.getParameter("currentPage"));
request.getRequestDispatcher("TrainScoreList.jsp").forward(request,
response);
}
// query分支处理查询请求
else if ("query".equals(opraParam)) {
String cdstr = "";
// 获取列表页面传来的查询条件
String[] ckey = request.getParameterValues("keyparam");
String[] cvalue = request.getParameterValues("keyvalue");
for (int i = 0; i < ckey.length; i++) {
// 判断查询条件是否为空,如果用户没有在查询框中输入任何条件,则查出培训类别表中所有记录
// 查询均为模糊查询
if (!"".equals(cvalue[i])) {
UserInfoDAO userDao = new UserInfoDAO();
//UserInfoVO userVo = new UserInfoVO();
String ui_id = "";
// 用户培训记录表是用户和培训记录的多对多关系表。
// 同一个姓名可能对应多个培训记录,同一个记录可能对应多个用户。
// 所以在查询条件SQL语句连接上要作出判断。
if ("load_name".equals(ckey[i])) {
List userList = userDao
.queryByCondition("and ui_loadname like '%"
+ cvalue[i] + "%'");
// 如果不存在对应输入的用户名的用户,那么ui_id设置为0。因为存在的用户ui_id不可能为0。
if (userList.size() == 0)
cdstr += "and ui_id=0";
// 对应输入用户名的用户只有一个
if (userList.size() == 1) {
ui_id = ((UserInfoVO) userList.get(0)).getUiId()
.toString();
cdstr += "and ui_id in (" + ui_id + ")";
}
// 对应输入用户名的用户有多个
else {
cdstr += "and ui_id in (";
for (int j = 0; j < userList.size(); j++) {
ui_id = ((UserInfoVO) userList.get(j))
.getUiId().toString();
if (j == userList.size() - 1)
cdstr += ui_id;
else
cdstr += ui_id + ",";
}
cdstr += ")";
}
} else if ("real_name".equals(ckey[i])) {
List userList = userDao
.queryByCondition(" and ui_realname like '%"
+ cvalue[i] + "%'");
// 如果不存在对应输入的真实名的用户,那么ui_id设置为0。因为存在的用户ui_id不可能为0。
if (userList.size() == 0)
cdstr += "and ui_id=0";
// 对应输入真实名的用户只有一个
if (userList.size() == 1) {
ui_id = ((UserInfoVO) userList.get(0)).getUiId()
.toString();
cdstr += "and ui_id in (" + ui_id + ")";
}
// 对应输入真实名的用户有多个
else {
cdstr += "and ui_id in (";
for (int k = 0; k < userList.size(); k++) {
ui_id = ((UserInfoVO) userList.get(k))
.getUiId().toString();
if (k == userList.size() - 1)
cdstr += ui_id;
else
cdstr += ui_id + ",";
}
cdstr += ")";
}
} else
cdstr += " and " + ckey[i] + " like '%" + cvalue[i]
+ "%' ";
}
}
pagehelp = turDao.getTrainScoreList(cdstr, pageSize, 1);
// 转发至列表页面
request.setAttribute("pagehelp", pagehelp);
request.getRequestDispatcher("TrainScoreList.jsp").forward(request,
response);
}
// 当用户在菜单栏中点击培训类别维护时,跳入trainscore分支。显示培训人员成绩列表页面
else if ("trainscore".equals(opraParam)) {
pagehelp = turDao.getTrainScoreList("", pageSize, 1);
/** 通过userdb对象调用的getUserList方法等到要显示的内容,方法中的参数分别为查询条件,每页显示记录数,当前页码 */
request.setAttribute("pagehelp", pagehelp);
request.getRequestDispatcher("TrainScoreList.jsp").forward(request,
response);
}
// 用户在列表页面点击修改按钮后,detail分支进行处理,将请求转向更新页面
if (opraParam.equals("detail")) {
try {
String tcIds = request.getParameter("ch1");
String a[] = tcIds.split(",");
Long tr_id = Long.parseLong(a[0]);
Long ui_id = Long.parseLong(a[1]);
TrainRecordDAO trDao = new TrainRecordDAO();
UserInfoDAO userDao = new UserInfoDAO();
TrainRecordVO trVo = trDao.get(tr_id);
UserInfoVO userVo = (UserInfoVO) userDao.queryByCondition(
"and ui_id=" + ui_id + "").get(0);
TrUiRsIdVO turiVo = new TrUiRsIdVO();
turiVo.setTrainRecord(trVo);
turiVo.setUserInfo(userVo);
TrUiRsVO turVo = turDao.get(turiVo);
request.setAttribute("currentPage", request
.getParameter("currentPage"));
request.setAttribute("turInfo", turVo);
request.getRequestDispatcher("TrainScoreUpdate.jsp").forward(
request, response);
} catch (Exception e) {
// 写异常日志
System.out.println(e);
}
}
// 用户在修改页面点击提交,进入update分支处理,将更改后结果写入数据库
if (opraParam.equals("update")) {
// 培训记录中包含培训记录和用户的外键。
// 所以通过培训记录ID转化得到培训记录VO,封装到用户培训关系VO中
// 通过用户名转化得到用户VO,封装到用户培训关系VO中
Long tr_id = Long.parseLong(request.getParameter("tr_id"));
String loadname = request.getParameter("loadname");
TrainRecordDAO trDao = new TrainRecordDAO();
UserInfoDAO userDao = new UserInfoDAO();
TrainRecordVO trVo = trDao.get(tr_id);
UserInfoVO userVo = (UserInfoVO) userDao.queryByCondition(
"and ui_loadname= '" + loadname + "'").get(0);
TrUiRsIdVO turiVo = new TrUiRsIdVO();
turiVo.setTrainRecord(trVo);
turiVo.setUserInfo(userVo);
TrUiRsVO turVo = turDao.get(turiVo);
turVo.setUiIspass(request.getParameter("score"));
// 写入数据库
int i = turDao.update(turVo);
if (i > 0) {// 修改成功
request.setAttribute("turVo", turVo);
try {
pagehelp = turDao.getTrainScoreList("", pageSize, Integer
.parseInt(request.getParameter("currentPage")));
// 转发至列表页面
request.setAttribute("pagehelp", pagehelp);
request.setAttribute("currentPage", request
.getParameter("currentPage"));
request
.getRequestDispatcher(
"/training/TrainScoreList.jsp").forward(
request, response);
} catch (Exception e) {
// 写异常日志
// System.out.println("in the update :" + e);
}
} else {
errorMsg = "修改失败,请重试!!";
out.println(errorMsg);
}
}
}
public void init() throws ServletException {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -