📄 manageroperateaction.java
字号:
package com.REP.action;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;
import com.REP.IService.IManagerService;
import com.REP.bean.Account;
import com.REP.excptions.AccountIsExistException;
import com.REP.excptions.AccountNotExistException;
public class ManagerOperateAction extends DispatchAction {
IManagerService managerservice;
//managerLogin方法用于处理管理员登陆验证的请求
public ActionForward managerLogin(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) {
//从页面表单获得管理员输入的用户名和密码
DynaActionForm dform = (DynaActionForm)form;
String loginName =dform.getString("loginName");
String password =dform.getString("password");
//如果输入的用户名和密码为空,将错误信息返回给输入页面给出错误提示
if(loginName.equals("")||password.equals("")){
ActionMessages errors = new ActionMessages();
if(!loginName.equals("")){
errors.add("password",new ActionMessage("error.password.required"));
}else if(!password.equals("")){
errors.add("loginName",new ActionMessage("error.loginName.required"));
}else{
errors.add("loginName",new ActionMessage("error.loginName.required"));
errors.add("password",new ActionMessage("error.password.required"));
}
saveErrors(request,errors);
return mapping.findForward("ManagerLogin");
}
//如果输入格式正确,则检验管理员输入的用户名和密码是否正确
Boolean checkresult = managerservice.checkUser(loginName,password);
//如果不存在输入的用户名和密码,则将错误信息返回到输入页面,显示出来
if(!checkresult){
ActionMessages errors = new ActionMessages();
//将异常或错误信息存入ActionMessages对象errors中
errors.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage("error.manangerlogin.managerNotExit"));
//把ActionMessages对象存入到request对象中
saveErrors(request,errors);
//跳转到错误处理页面
return mapping.findForward("ManagerLogin");
}
//如果输入的用户名和密码正确,则可以跳转到管理员查看和操作员工账户的页面
return mapping.findForward("GoViewAccount");
}
//viewAccount()方法用于取得员工的账户,并将账户列表发送到页面
public ActionForward viewAccount(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) {
List list = managerservice.findAllAccount();
request.setAttribute("accountList",list);
return mapping.findForward("ViewAccount");
}
//GoModifyAccount()方法用于处理修改账户的请求
public ActionForward GoModifyAccount(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws SQLException {
String id = request.getParameter("id");
Account account = managerservice.getAccountById(id);
String repastCard = account.getLoginName();
request.setAttribute("id",id);
request.setAttribute("oldRepastCard",repastCard);
return mapping.findForward("ModifyAccount");
}
//GoFillAccount()方法用于处理对员工账户充值的请求
public ActionForward GoFillAccount(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws SQLException {
String id = request.getParameter("id");
request.setAttribute("id",id);
return mapping.findForward("FillAccount");
}
public ActionForward fillAccount(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws SQLException {
DynaActionForm dform = (DynaActionForm)form;
String banlance = dform.getString("banlance");
String id = request.getParameter("id");
Account account = managerservice.getAccountById(id);
try {
managerservice.fillAccount(account,banlance);
} catch (AccountNotExistException e) {
ActionMessages errors = new ActionMessages();
//将异常或错误信息存入ActionMessages对象errors中
errors.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage("error.account.accountNotExist"));
//把ActionMessages对象存入到request对象中
saveErrors(request,errors);
//跳转到错误处理页面
return mapping.getInputForward();
}
return mapping.findForward("GoViewAccount");
}
public ActionForward delAccount(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws SQLException {
String id = request.getParameter("id");
Account account = managerservice.getAccountById(id);
try {
managerservice.delAccount(account);
} catch (AccountNotExistException e) {
ActionMessages errors = new ActionMessages();
//将异常或错误信息存入ActionMessages对象errors中
errors.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage("error.account.accountNotExist"));
//把ActionMessages对象存入到request对象中
saveErrors(request,errors);
//跳转到错误处理页面
return mapping.getInputForward();
}
return mapping.findForward("GoViewAccount");
}
public ActionForward modifyAccount(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws SQLException {
DynaActionForm dform = (DynaActionForm)form;
String repastCard = dform.getString("repastCard");
String id = request.getParameter("id");
try {
managerservice.modifyAccount(repastCard,id);
} catch (AccountIsExistException e) {
ActionMessages errors = new ActionMessages();
//将异常或错误信息存入ActionMessages对象errors中
errors.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage("error.account.accountIsExist"));
//把ActionMessages对象存入到request对象中
saveErrors(request,errors);
//跳转到错误处理页面
return mapping.getInputForward();
}
return mapping.findForward("GoViewAccount");
}
public IManagerService getManagerservice() {
return managerservice;
}
public void setManagerservice(IManagerService managerservice) {
this.managerservice = managerservice;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -