时间:2021-07-01 10:21:17 帮助过:12人阅读
定义emp类:
package com.sxt.mvcpro.vo; import java.io.Serializable; import java.util.Date; public class Emp implements Serializable { private Integer empno; private String ename; private String job; private Double sal; private Date hiredate; private Integer mgr; private Double comm; private Integer deptno; private String img; public Emp() { super(); } public Integer getEmpno() { return empno; } public void setEmpno(Integer empno) { this.empno = empno; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public String getJob() { return job; } @Override public String toString() { return "Emp [empno=" + empno + ", ename=" + ename + ", job=" + job + ", sal=" + sal + ", hiredate=" + hiredate + ", mgr=" + mgr + ", comm=" + comm + ", deptno=" + deptno + ", img=" + img + "]"; } public void setJob(String job) { this.job = job; } public Double getSal() { return sal; } public void setSal(Double sal) { this.sal = sal; } public Date getHiredate() { return hiredate; } public void setHiredate(Date hiredate) { this.hiredate = hiredate; } public Integer getMgr() { return mgr; } public void setMgr(Integer mgr) { this.mgr = mgr; } public Double getComm() { return comm; } public void setComm(Double comm) { this.comm = comm; } public Integer getDeptno() { return deptno; } public void setDeptno(Integer deptno) { this.deptno = deptno; } public String getImg() { return img; } public void setImg(String img) { this.img = img; } public Emp(Integer empno, String ename, String job, Double sal, Date hiredate, Integer mgr, Double comm, Integer deptno, String img) { this.empno = empno; this.ename = ename; this.job = job; this.sal = sal; this.hiredate = hiredate; this.mgr = mgr; this.comm = comm; this.deptno = deptno; this.img = img; } public Emp(Integer empno, String ename, String job, Double sal, Date hiredate, Integer mgr, Double comm, Integer deptno) { this.empno = empno; this.ename = ename; this.job = job; this.sal = sal; this.hiredate = hiredate; this.mgr = mgr; this.comm = comm; this.deptno = deptno; } }
调整业务层父类(EmpServlet)
package com.sxt.mvcpro.servlet; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.sxt.mvcpro.dao.service.IEmpService; import com.sxt.mvcpro.dao.service.impl.EmpServiceImpl; import com.sxt.mvcpro.factory.ServiceFactory; import com.sxt.mvcpro.vo.Emp; @SuppressWarnings("serial") @WebServlet(urlPatterns = { "/emp/*", "/dept/*" }) public class EmpServlet extends BaseServlet { private IEmpService empService = (IEmpService) ServiceFactory.getInstance(EmpServiceImpl.class); @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String pathInfo = req.getPathInfo(); try { if ("/regist".equals(pathInfo)) { this.regist(req, resp); } } catch (Exception e) { e.printStackTrace(); } } public void regist(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException { super.saveFile(req, resp); // 将基本信息保存到数据库中 Emp emp = null; try { emp = super.initObj(req, Emp.class); emp.setImg(super.newFileName); System.out.println(emp); if (empService.addEmp(emp)) { // 保存添加雇员的信息 req.setAttribute("emp", emp); req.getRequestDispatcher("/pages/emp.jsp").forward(req, resp); } } catch (Exception e) { e.printStackTrace(); } System.out.println(emp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } @Override public String getDir() { return "empImgs/"; }
前端客户端输入界面代码:(newLogin.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <base href="/Mvcpro/"/> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="emp/regist" method="POST" enctype="multipart/form-data"> <fieldset> <legend>请注册</legend> <br>编号:<input type="text" name="empno" value="7550"><br> <br>姓名:<input type="text" name="ename" value="水电费"><br> <br>职位:<input type="text" name="job" value="上单"><br> <br>薪资:<input type="text" name="sal"value="20000"><br> <br>领导编号:<input type="text" name="mgr"value="2343"><br> <br>入职日期:<input type="text" name="hiredate"value="2019-07-04"><br> <br>佣金:<input type="text" name="comm"value="1000"><br> <br>部门编号:<input type="text" name="deptno"value="40"><br> <br>照 片:<input type="file" name="img"><br> <br><input type="submit" value="提交"><input type="reset" value="重置"> </fieldset> </form> </body> </html>
前端客户端输出界面代码:(emp.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <base href="/MvcPro/"/> </head> <body> <h1> 显示雇员的信息:${emp} </h1> <table border="1"> <tr> <td>编号</td><td>姓名</td><td>职位</td><td>薪资</td><td>领导编号</td><td>入职日期</td><td>佣金</td><td>部门编号</td><td>照片</td> </tr> <tr> <td>${emp.empno}</td><td>${emp.ename}</td><td>${emp.job}</td><td>${emp.sal}</td><td>${emp.mgr}</td><td>${emp.hiredate}</td><td>${emp.comm}</td><td>${emp.deptno}</td><td><img src="${emp.img}"></td> </tr> </table> </body> </html>
调试效果图:
混合表单文件上传到数据库(基于TOMCAT)
标签:factory config 代码 ack parse date content list char