EmpAddAction.java
上传用户:jishiqi_cj
上传日期:2022-08-08
资源大小:24765k
文件大小:3k
- package StudyNote;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.struts.action.Action;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- import javax.servlet.ServletContext;
- import javax.sql.DataSource;
- import java.sql.Connection;
- import java.sql.Statement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.*;
- import javax.servlet.http.*;
- import org.apache.struts.validator.DynaValidatorForm;
- import org.apache.struts.action.ActionMessage;
- import org.apache.struts.action.ActionMessages;
- public final class EmpAddAction extends Action{
- public ActionForward execute(
- ActionMapping mapping,
- ActionForm form,
- HttpServletRequest request,
- HttpServletResponse response) throws Exception {
-
- DynaValidatorForm empForm = (DynaValidatorForm) form;
- String name = (String)empForm.get("name");
- String sex = (String)empForm.get("sex");
- String birthday = (String)empForm.get("birthday");
- String tel = (String)empForm.get("tel");
- Integer depId = (Integer)empForm.get("depId");
- String duty = (String)empForm.get("duty");
- String degree = (String)empForm.get("degree");
- String exp = (String)empForm.get("exp");
-
- HttpSession session = request.getSession();
- Vector empList = new Vector();
-
- ServletContext context = servlet.getServletContext();
- DataSource dataSource =
- (DataSource)context.getAttribute(Constants.DATASOURCE_KEY);
-
- DB db = new DB(dataSource);
-
- String PageForward;
- Emp emp=new Emp();
- emp.setName(name) ;
- emp.setSex(sex) ;
- emp.setDuty(duty) ;
- emp.setTel(tel) ;
- emp.setDepId(depId.intValue()) ;
- emp.setBirthday(birthday) ;
- emp.setDegree(degree) ;
- emp.setExp(exp) ;
-
- ActionMessages errors = new ActionMessages();
- //避免重复提交
- if (!isTokenValid(request)){
- PageForward="toEmpQueryResult";
- saveToken(request);
- }
- else {
- resetToken(request); //避免重复提交
- if (emp.Insert(db)){
- empList = Emp.Search(db,depId.intValue(),"");
- session.setAttribute(Constants.EMP_LIST_KEY,empList);
- PageForward="toEmpQueryResult";
- }
- else{
- errors.add(ActionMessages.GLOBAL_MESSAGE,
- new ActionMessage("errors.insertFail"));
- if (!errors.isEmpty()) {
- saveErrors(request, errors);
- }
- PageForward="toWrong";
- }
- }
-
- db.close();
- return (mapping.findForward(PageForward));
- }
- }