📄 useraction.java
字号:
package com.cargo.crotrol;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.cargo.db.Userdb;
import com.cargo.db.Areadb;
import java.util.Collection;
import java.util.ArrayList;
import com.cargo.filter.Input;
import com.cargo.filter.Line;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: MR</p>
* @author BWM
* @version 1.0
*/
public class UserAction extends Action {
private Collection coll = new ArrayList();
private Userdb db = new Userdb();
private Areadb areadb = new Areadb();
private int t = 0;
private ActionErrors errors = new ActionErrors();
private Input in = new Input();
private Line online = new Line();
public ActionForward perform(ActionMapping mapping, ActionForm actionForm,
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) {
/**@todo: complete the business logic here, this is just a skeleton.*/
ActionForward forward = new ActionForward();
String action = mapping.getParameter();
UserForm form = (UserForm) actionForm;
if ("add".equalsIgnoreCase(action)) {
forward = Add(mapping, form, httpServletRequest,
httpServletResponse);
return forward;
} else if ("reg".equalsIgnoreCase(action)) {
forward = Reg(mapping, form, httpServletRequest,
httpServletResponse);
return forward;
} else if ("logon".equalsIgnoreCase(action)) {
forward = Logon(mapping, form, httpServletRequest,
httpServletResponse);
return forward;
}
forward = online.down("id", mapping, httpServletRequest, "errors");
if (forward != null && forward.getName().equals("errors")) {
return forward;
}
if ("".equalsIgnoreCase(action)) {
forward = mapping.findForward("errors");
} else if ("select".equalsIgnoreCase(action)) {
forward = Select(mapping, form, httpServletRequest,
httpServletResponse);
} else if ("save".equalsIgnoreCase(action)) {
forward = Save(mapping, form, httpServletRequest,
httpServletResponse);
} else if ("list".equalsIgnoreCase(action)) {
forward = List(mapping, actionForm, httpServletRequest,
httpServletResponse);
} else if ("del".equalsIgnoreCase(action)) {
forward = Del(mapping, actionForm, httpServletRequest,
httpServletResponse);
} else {
forward = mapping.findForward("errors");
}
return forward;
}
private ActionForward Del(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
String usedel=request.getParameter("userdel").toString();
t=db.delete(db.DELETE+"tb_user"+db.WHERE+"id='"+usedel+"'");
if (t<=0) {
errors.clear();
errors.add("deletefail", new ActionError("error.delete.fail"));
saveErrors(request, errors);
return (new ActionForward(mapping.getInput()));
}
request.setAttribute("info", "true");
return mapping.findForward("success");
}
private ActionForward Reg(ActionMapping mapping, UserForm form,
HttpServletRequest request,
HttpServletResponse response) {
coll = areadb.select();
if (coll == null || coll.isEmpty()) {
errors.clear();
errors.add("selectfail", new ActionError("error.manage.addarea"));
saveErrors(request, errors);
return (new ActionForward(mapping.getInput()));
}
request.setAttribute("Area", coll);
return mapping.findForward("success");
}
private ActionForward List(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
coll=db.select();
if(coll==null||coll.isEmpty()){
errors.clear();
errors.add("selectfail", new ActionError("error.user.null"));
saveErrors(request, errors);
}
request.setAttribute("User", coll);
return mapping.findForward("success");
}
private ActionForward Logon(ActionMapping mapping, UserForm form,
HttpServletRequest request,
HttpServletResponse response) {
HttpSession session = request.getSession();
errors.clear();
if (in.isEmpty(form.getUser().getUserid())) {
errors.add("id", new ActionError("error.id.null"));
}
if (in.isEmpty(form.getUser().getPassword())) {
errors.add("password", new ActionError("error.password.null"));
}
if (!errors.empty()) {
saveErrors(request, errors);
return new ActionForward("/user/logon.jsp");
}
t = db.getRowCount("tb_user" + db.WHERE + "id='" + form.getUser().getUserid() +
"' AND password='" + form.getUser().getPassword() +
"'");
if (t <= 0) {
errors.clear();
errors.add("logonfail", new ActionError("error.logon.fail"));
saveErrors(request, errors);
return (new ActionForward(mapping.getInput()));
}
session.setAttribute("id", form.getUser().getUserid());
return mapping.findForward("success");
}
private ActionForward Select(ActionMapping mapping, UserForm form,
HttpServletRequest request,
HttpServletResponse response) {
ActionForward forward = Reg(mapping, form, request, response);
if (forward.getName() == null ||
!forward.getName().equalsIgnoreCase("success")) {
errors.clear();
errors.add("selectfail", new ActionError("error.manage.addarea"));
}
coll = db.select(db.WHERE + "id='" + online.getUserId() + "'");
if (coll == null || coll.isEmpty()) {
errors.add("selectfail1", new ActionError("error.select.fail"));
}
if (!errors.empty()) {
saveErrors(request, errors);
return (new ActionForward(mapping.getInput()));
}
request.setAttribute("User", coll);
return mapping.findForward("success");
}
private ActionForward Save(ActionMapping mapping, UserForm form,
HttpServletRequest request,
HttpServletResponse response) {
t = db.getRowCount("tb_user" + db.WHERE + "id='" + form.getUser().getUserid() +
"'");
if (t <= 0) {
errors.clear();
errors.add("insertfail", new ActionError("error.select.fail"));
}
t = db.update(form.getUser());
if (t <= 0) {
errors.add("insertfail", new ActionError("error.insert.fail"));
}
if (!errors.empty()) {
saveErrors(request, errors);
return (new ActionForward(mapping.getInput()));
}
request.setAttribute("info","true");
return mapping.findForward("success");
}
private ActionForward Add(ActionMapping mapping, UserForm form,
HttpServletRequest request,
HttpServletResponse httpServletResponse) {
t = db.getRowCount("tb_user" + db.WHERE + "id='" + form.getUser().getUserid() +
"'");
if (t > 0) {
errors.clear();
errors.add("insertfail", new ActionError("error.insert.repeat"));
saveErrors(request, errors);
return (new ActionForward(mapping.getInput()));
}
t = db.insert(form.getUser());
if (t <= 0) {
errors.clear();
errors.add("insertfail", new ActionError("error.insert.fail"));
saveErrors(request, errors);
return (new ActionForward(mapping.getInput()));
}
return mapping.findForward("success");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -