java里添加员工信息_SSH_框架整合4--添加员工信息
SSH_框架整合4--添加员工信息
一、
1 index.jsp:添加:添加员工向信息:Add Employees' Information
2 因为在添加员工信息时要选择员工的部门信息,所以要先获取Department信息:
(1)com.atguigu.ssh.dao中:提取EmployeeDao.java中公共部分代码(getSession())作为父类BaseDao
1 SSH_框架整合4--添加员工信息
View Code
新建DepartmentDao类来获取Department集合信息:DepartmentDao.java
1 packagecom.atguigu.ssh.dao;2
3 importjava.util.List;4
5 importcom.atguigu.ssh.entities.Department;6
7 public class DepartmentDao extendsBaseDao{8
9 //获取查询到的Department集合
10 public ListgetAll(){11 String hql="FROM Department";12 returngetSession().createQuery(hql).list();13 }14 }
View Code
(2)com.atguigu.ssh.service包中:新建DepartmentService类:DepartmentService.java
1 packagecom.atguigu.ssh.service;2
3 importjava.util.List;4
5 importcom.atguigu.ssh.dao.DepartmentDao;6 importcom.atguigu.ssh.entities.Department;7
8 public classDepartmentService {9
10 privateDepartmentDao departmentDao;11 public voidsetDepartmentDao(DepartmentDao departmentDao) {12 this.departmentDao =departmentDao;13 }14
15 public ListgetAll(){16 returndepartmentDao.getAll();17 }18 }
View Code
(3) com.atguigu.ssh.actions包下的EmployeeAction.java:填写获取Department集合的方法并且放入request中(jsp页面中就可以通过request直接获取)
1 //3-1 查询Department
2 privateDepartmentService departmentService;3 public voidsetDepartmentService(DepartmentService departmentService) {4 this.departmentService =departmentService;5 }6 //3-2 实现添加员工信息的方法
7 publicString input(){8 request.put("departments", departmentService.getAll());9 returnINPUT;10 }
View Code
3 完善applicationContext-beans.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 scope="prototype">
24
25
26
27
View Code
4 完善struts.xml,添加显示添加页面语句:/WEB-INF/views/emp-input.jsp
5 WebContent-WEB-INF-views下新建emp-input.jsp
1
2 pageEncoding="UTF-8"%>
3
4
5
6
7
8
Insert title here9
10
11
Employee Input Page
12
13
14
15
16
17
18
19 listKey="id"listValue="departmentName"name="department.id"
20 label="Department">
21
22
23
24
25
View Code
二、
1 实现ModelDriven拦截器操作
(1)EmployeeAction实现ModelDriven,Preparable接口,添加实现方法:
1 //****
2 @Override3 public void prepare() throwsException {4 }5 //****
6 privateEmployee model;7 //****
8 @Override9 publicEmployee getModel() {10 returnmodel;11 }12 //**** 4 使用ModelDriven拦截器方法存储添加的信息
13 publicString save(){14 System.out.println(model);15 model.setCreateTime(newDate());16 employeeService.saveorUpdate(model);17 returnSUCCESS;18 }19 //****
20 public voidprepareSave(){21 model=newEmployee();22 }23
24 //**** 拦截器的方法
25 public voidprepareInput(){26
27 }
View Code
(2)Struts.xml配置下拦截器:
1
3
4
5
6 false
7
8
9
10
11
View Code
2 定制类型转换器:使输入的Birth字符串转化为Date类型
(1)新建包com.atguigu.ssh.converters:新建SSHDateConverter.java类
1 packagecom.atguigu.ssh.converters;2
3 importjava.text.DateFormat;4 importjava.text.ParseException;5 importjava.text.SimpleDateFormat;6 importjava.util.Date;7 importjava.util.Map;8
9 importorg.apache.struts2.util.StrutsTypeConverter;10
11 public class SSHDateConverter extendsStrutsTypeConverter {12
13 privateDateFormat dateFormat;14
15 publicSSHDateConverter()16 {17 dateFormat = new SimpleDateFormat("yyyy-MM-dd");18 }19
20 @Override21 publicObject convertFromString(Map context, String[] values, Class toClass) {22 if(toClass == Date.class){23 if(values !=null && values.length>0){24 String value=values[0];25 try{26 returndateFormat.parse(value);27 } catch(ParseException e) {28 e.printStackTrace();29 }30 }31 }32 returnvalues;33 }34
35 @Override36 publicString convertToString(Map context, Object o) {37 if(o instanceofDate){38 Date date=(Date)o;39 returndateFormat.format(date);40 }41 return null;42 }43
44 }
View Code
(2)src-cong下新建xwork-conversion.properties,配置类型转换器:
java.util.Date=com.atguigu.ssh.converters.SSHDateConverter
3 实现存储操作:
(1)EmployeeDao.java中添加save方法:
1 //3 添加、存储
2 public voidsaveOrUpadate(Employee employee){3 getSession().saveOrUpdate(employee);4 }
View Code
(2)EmployeeService.java
1 //3 添加、存储
2 public voidsaveorUpdate(Employee employee){3 employeeDao.saveOrUpadate(employee);4 }
View Code
(3)EmployeeAction.java:
1 //**** 4 使用ModelDriven拦截器方法存储添加的信息
2 publicString save(){3 System.out.println(model);4 model.setCreateTime(newDate());5 employeeService.saveorUpdate(model);6 returnSUCCESS;7 }8 //****
9 public voidprepareSave(){10 model=newEmployee();11 }
View Code
4 显示页面:格式化时间操作:
1
2
3
4
5
6
View Code
结构图:
文件:
1 EmployeeAction.java
1 packagecom.atguigu.ssh.actions;2
3 importjava.io.ByteArrayInputStream;4 importjava.io.InputStream;5 importjava.io.UnsupportedEncodingException;6 importjava.util.Date;7 importjava.util.Map;8
9 importorg.apache.struts2.interceptor.RequestAware;10
11 importcom.atguigu.ssh.entities.Employee;12 importcom.atguigu.ssh.service.DepartmentService;13 importcom.atguigu.ssh.service.EmployeeService;14 importcom.opensymphony.xwork2.ActionSupport;15 importcom.opensymphony.xwork2.ModelDriven;16 importcom.opensymphony.xwork2.Preparable;17
18 public class EmployeeAction extends ActionSupport implementsRequestAware,19 ModelDriven,Preparable{20
21 private static final long serialVersionUID = 1L;22
23 privateEmployeeService employeeService;24
25 public voidsetEmployeeService(EmployeeService employeeService){26 this.employeeService=employeeService;27 }28
29 //**** 4 使用ModelDriven拦截器方法存储添加的信息
30 publicString save(){31 System.out.println(model);32 model.setCreateTime(newDate());33 employeeService.saveorUpdate(model);34 returnSUCCESS;35 }36 //****
37 public voidprepareSave(){38 model=newEmployee();39 }40
41 //3-1 查询Department
42 privateDepartmentService departmentService;43 public voidsetDepartmentService(DepartmentService departmentService) {44 this.departmentService =departmentService;45 }46 //3-2 实现添加员工信息的方法
47 publicString input(){48 request.put("departments", departmentService.getAll());49 returnINPUT;50 }51
52 //**** 拦截器的方法
53 public voidprepareInput(){54
55 }56
57 //2 删除
58 privateInteger id;59 public voidsetId(Integer id) {60 this.id =id;61 }62 /*public String delete(){63 employeeService.delete(id);64 return SUCCESS;65 }*/
66 //2-1 使用Ajax方式删除
67 privateInputStream inputStream;68
69 publicInputStream getInputStream() {70 returninputStream;71 }72
73 publicString delete(){74 employeeService.delete(id);75 try{76 //删除成功
77 inputStream=new ByteArrayInputStream("1".getBytes("UTF-8"));78 } catch(UnsupportedEncodingException e) {79 //删除失败
80 try{81 inputStream=new ByteArrayInputStream("0".getBytes("UTF-8"));82 } catch(UnsupportedEncodingException e1) {83 //TODO Auto-generated catch block
84 e1.printStackTrace();85 }86 e.printStackTrace();87 }88 return "ajax-delete-success";89 }90 //1 查询
91 publicString list(){92 request.put("employees", employeeService.getAll());93 return "list";94 }95
96 //放到页面里
97 private Maprequest;98
99 @Override100 public void setRequest(Maparg0) {101 this.request=arg0;102 }103
104 //****
105 @Override106 public void prepare() throwsException {107 }108 //****
109 privateEmployee model;110 //****
111 @Override112 publicEmployee getModel() {113 returnmodel;114 }115
116 }
View Code
2 struts.xml
1 <?xml version="1.0" encoding="UTF-8" ?>
2 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
4 "http://struts.apache.org/dtds/struts-2.3.dtd">
5
6
7
8
9
10
11
12
14
15
16
17 false
18
19
20
21
22
23
24
25 method="{1}">
26 /WEB-INF/views/emp-list.jsp
27
28
29 text/html
30 inputStream
31
32 /WEB-INF/views/emp-input.jsp
33
34 /emp-list
35
36
37
38
View Code
3 emp-list.jsp
1
2 pageEncoding="UTF-8"%>
3
4
5
6
7
8
Insert title here9
10
11 //删除, 使用 ajax 的方式
12 $(function(){13 $(".delete").click(function(){14 varlastName=$(this).next(":input").val();15 varflag=confirm("是否要删除"+lastName+"的信息吗?");16 //确认删除, 使用 ajax 的方式
17 if(flag){18 //获取要删除的行
19 var$tr=$(this).parent().parent();20 varurl=this.href;21 varargs={"time":newDate()};22 $.post(url,args,function(data){23 //若 data 的返回值为 1, 则提示 删除成功, 且把当前行删除
24 if(data==1){25 alert("删除成功!");26 $tr.remove();27 }else{28 //若 data 的返回值不是 1, 提示删除失败.
29 alert("删除失败!");30 }31 });32 }33 return false;34 });35 })36
37
38
39
Employee List Page
40
41
42 没有员工信息43
44
45
46
47
ID48
LASTNAME49
EMAIL50
BIRTH51
CREATETIME52
DEPT53
54
55
56
${id}57
${lastName}58
${email }59
60
${birth}61
${createTime}62 --%>
63
64
65
66
67
68
69
70
${department.departmentName}71
72 Delete
73
74
75
76
77
78
79
80
View Code
4 emp-input.jsp
1
2 pageEncoding="UTF-8"%>
3
4
5
6
7
8
Insert title here9
10
11
Employee Input Page
12
13
14
15
16
17
18
19 listKey="id"listValue="departmentName"name="department.id"
20 label="Department">
21
22
23
24
25
View Code