博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SSH_框架整合4--添加员工信息
阅读量:4610 次
发布时间:2019-06-09

本文共 12110 字,大约阅读时间需要 40 分钟。

SSH_框架整合4--添加员工信息

一、

1 index.jsp:添加:<a href="emp-input">添加员工向信息:Add Employees' Information</a>

2 因为在添加员工信息时要选择员工的部门信息,所以要先获取Department信息:

  (1)com.atguigu.ssh.dao中:提取EmployeeDao.java中公共部分代码(getSession())作为父类BaseDao

  
1 SSH_框架整合4--添加员工信息
View Code

    新建DepartmentDao类来获取Department集合信息:DepartmentDao.java

  
1 package com.atguigu.ssh.dao; 2  3 import java.util.List; 4  5 import com.atguigu.ssh.entities.Department; 6  7 public class DepartmentDao extends BaseDao{ 8  9     //获取查询到的Department集合10     public List
getAll(){11 String hql="FROM Department";12 return getSession().createQuery(hql).list();13 }14 }
View Code

  (2)com.atguigu.ssh.service包中:新建DepartmentService类:DepartmentService.java 

  
1 package com.atguigu.ssh.service; 2  3 import java.util.List; 4  5 import com.atguigu.ssh.dao.DepartmentDao; 6 import com.atguigu.ssh.entities.Department; 7  8 public class DepartmentService { 9 10     private DepartmentDao departmentDao;11     public void setDepartmentDao(DepartmentDao departmentDao) {12         this.departmentDao = departmentDao;13     }14     15     public List
getAll(){16 return departmentDao.getAll();17 }18 }
View Code

  (3) com.atguigu.ssh.actions包下的EmployeeAction.java:填写获取Department集合的方法并且放入request中(jsp页面中就可以通过request直接获取)

  
1 //3-1  查询Department 2     private DepartmentService departmentService; 3     public void setDepartmentService(DepartmentService departmentService) { 4         this.departmentService = departmentService; 5     } 6     //3-2 实现添加员工信息的方法 7     public String input(){ 8         request.put("departments", departmentService.getAll()); 9         return INPUT;10     }
View Code

3  完善applicationContext-beans.xml 

  
1 
2
5 6
7
8
9 10
11
12
13 14
15
16
17 18
19
20
21 22
24
25
26
27
View Code

4 完善struts.xml,添加显示添加页面语句:<result name="input">/WEB-INF/views/emp-input.jsp</result>

5  WebContent-WEB-INF-views下新建emp-input.jsp 

  
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2     pageEncoding="UTF-8"%> 3 <%@ taglib prefix="s" uri="/struts-tags" %>     4  5  6  7 
8 Insert title here 9 10 11

Employee Input Page

12 13
14
15
16
17 18
21
22
23 24 25
View Code

 二、

1 实现ModelDriven拦截器操作

   (1)EmployeeAction实现ModelDriven<Employee>,Preparable接口,添加实现方法: 

1 //**** 2     @Override 3     public void prepare() throws Exception { 4     } 5     //**** 6     private Employee model; 7     //**** 8     @Override 9     public Employee getModel() {10         return model;11     }12 //**** 4 使用ModelDriven拦截器方法存储添加的信息13     public String save(){14         System.out.println(model);15         model.setCreateTime(new Date());16                   employeeService.saveorUpdate(model);17         return SUCCESS;18     }19     //****20     public void prepareSave(){21         model=new Employee();22     }23 24 //**** 拦截器的方法25     public void prepareInput(){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 package com.atguigu.ssh.converters; 2  3 import java.text.DateFormat; 4 import java.text.ParseException; 5 import java.text.SimpleDateFormat; 6 import java.util.Date; 7 import java.util.Map; 8  9 import org.apache.struts2.util.StrutsTypeConverter;10 11 public class SSHDateConverter extends StrutsTypeConverter {12 13     private DateFormat dateFormat;14     15     public SSHDateConverter()16     {17         dateFormat = new SimpleDateFormat("yyyy-MM-dd");18     }19     20     @Override21     public Object 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                     return dateFormat.parse(value);27                 } catch (ParseException e) {28                     e.printStackTrace();29                 }30             }31         }32         return values;33     }34 35     @Override36     public String convertToString(Map context, Object o) {37         if(o instanceof Date){38             Date date=(Date)o;39             return dateFormat.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 void saveOrUpadate(Employee employee){3         getSession().saveOrUpdate(employee);4     }
View Code

  (2)EmployeeService.java

1 //3 添加、存储2     public void saveorUpdate(Employee employee){3         employeeDao.saveOrUpadate(employee);4     }
View Code

  (3)EmployeeAction.java: 

1 //**** 4 使用ModelDriven拦截器方法存储添加的信息 2     public String save(){ 3         System.out.println(model); 4         model.setCreateTime(new Date()); 5         employeeService.saveorUpdate(model); 6         return SUCCESS; 7     } 8     //**** 9     public void prepareSave(){10         model=new Employee();11     }
View Code

4 显示页面:格式化时间操作:

1 2                         
3 4 5
6
View Code

 

结构图:

  

文件:

1 EmployeeAction.java 

1 package com.atguigu.ssh.actions;  2   3 import java.io.ByteArrayInputStream;  4 import java.io.InputStream;  5 import java.io.UnsupportedEncodingException;  6 import java.util.Date;  7 import java.util.Map;  8   9 import org.apache.struts2.interceptor.RequestAware; 10  11 import com.atguigu.ssh.entities.Employee; 12 import com.atguigu.ssh.service.DepartmentService; 13 import com.atguigu.ssh.service.EmployeeService; 14 import com.opensymphony.xwork2.ActionSupport; 15 import com.opensymphony.xwork2.ModelDriven; 16 import com.opensymphony.xwork2.Preparable; 17  18 public class EmployeeAction extends ActionSupport implements RequestAware, 19 ModelDriven
,Preparable{ 20 21 private static final long serialVersionUID = 1L; 22 23 private EmployeeService employeeService; 24 25 public void setEmployeeService(EmployeeService employeeService){ 26 this.employeeService=employeeService; 27 } 28 29 //**** 4 使用ModelDriven拦截器方法存储添加的信息 30 public String save(){ 31 System.out.println(model); 32 model.setCreateTime(new Date()); 33 employeeService.saveorUpdate(model); 34 return SUCCESS; 35 } 36 //**** 37 public void prepareSave(){ 38 model=new Employee(); 39 } 40 41 //3-1 查询Department 42 private DepartmentService departmentService; 43 public void setDepartmentService(DepartmentService departmentService) { 44 this.departmentService = departmentService; 45 } 46 //3-2 实现添加员工信息的方法 47 public String input(){ 48 request.put("departments", departmentService.getAll()); 49 return INPUT; 50 } 51 52 //**** 拦截器的方法 53 public void prepareInput(){ 54 55 } 56 57 //2 删除 58 private Integer id; 59 public void setId(Integer id) { 60 this.id = id; 61 } 62 /*public String delete(){ 63 employeeService.delete(id); 64 return SUCCESS; 65 }*/ 66 //2-1 使用Ajax方式删除 67 private InputStream inputStream; 68 69 public InputStream getInputStream() { 70 return inputStream; 71 } 72 73 public String 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 public String list(){ 92 request.put("employees", employeeService.getAll()); 93 return "list"; 94 } 95 96 //放到页面里 97 private Map
request; 98 99 @Override100 public void setRequest(Map
arg0) {101 this.request=arg0;102 }103 104 //****105 @Override106 public void prepare() throws Exception {107 }108 //****109 private Employee model;110 //****111 @Override112 public Employee getModel() {113 return model;114 }115 116 }
View Code

2 struts.xml

1 
2 5 6
7 8
9
10 11
12
14
15
16
17
false18
19
20
21
22
23 24
26
/WEB-INF/views/emp-list.jsp
27 28
29
text/html30
inputStream31
32
/WEB-INF/views/emp-input.jsp
33 34
/emp-list
35
36
37 38
View Code

3 emp-list.jsp

1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2     pageEncoding="UTF-8"%> 3 <%@ taglib prefix="s" uri="/struts-tags" %>    4  5  6  7 
8 Insert title here 9 10 37 38 39

Employee List Page

40 41
42 没有员工信息43
44
45
55
46
47
48
49
50
51
52
53
54
56
57
58
59 <%-- 60
61
62 --%>63
66
69 70
71
75
76 77
ID LASTNAME EMAIL BIRTH CREATETIME DEPT
${id} ${lastName} ${email } ${birth} ${createTime} 64
65
67
68
${department.departmentName} 72 Delete73 74
78
79 80
View Code

4 emp-input.jsp 

1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2     pageEncoding="UTF-8"%> 3 <%@ taglib prefix="s" uri="/struts-tags" %>     4  5  6  7 
8 Insert title here 9 10 11

Employee Input Page

12 13
14
15
16
17 18
21
22
23 24 25
View Code

 

 

  

  

 

    

转载于:https://www.cnblogs.com/noaman/p/5885458.html

你可能感兴趣的文章
php写一个判断是否有cookie的脚本
查看>>
Mac配置Fiddler抓包工具
查看>>
转:Java并发集合
查看>>
Word截图PNG,并压缩图片大小
查看>>
Python项目对接CAS方案
查看>>
mysql产生随机数
查看>>
编程风格
查看>>
熟悉常用的Linux命令
查看>>
易之 - 我是个大师(2014年3月6日)
查看>>
Delphi中窗体的事件
查看>>
file_get_contents()获取https出现这个错误Unable to find the wrapper “https” – did
查看>>
Error:Syntax error: redirection unexpected
查看>>
linux vi编辑器
查看>>
js树形结构-----(BST)二叉树增删查
查看>>
contract
查看>>
FJUT ACM 1899 Largest Rectangle in a Histogram
查看>>
如何删除xcode项目中不再使用的图片资源
查看>>
编写用例文档
查看>>
解决WPF两个图片控件显示相同图片因线程占用,其中一个显示不全的问题
查看>>
寻觅Azure上的Athena和BigQuery (二):神奇的PolyBase
查看>>