采用的框架 Struts2+Spring4+Hbiernate4.
目录结构
:
EmployeeAction:
1 package com.xx.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 import org.apache.struts2.interceptor.RequestAware; 9 import com.opensymphony.xwork2.ActionSupport; 10 import com.opensymphony.xwork2.ModelDriven; 11 import com.opensymphony.xwork2.Preparable; 12 import com.xx.ssh.entities.Employee; 13 import com.xx.ssh.service.DepartmentService; 14 import com.xx.ssh.service.EmployeeService; 15 16 public class EmployeeAction extends ActionSupport implements RequestAware, 17 ModelDriven<Employee>, Preparable { 18 19 private static final long serialVersionUID = 1L; 20 21 private EmployeeService employssService; 22 23 public void setEmployssService(EmployeeService employssService) { 24 this.employssService = employssService; 25 } 26 27 private DepartmentService departmentService; 28 29 public void setDepartmentService(DepartmentService departmentService) { 30 this.departmentService = departmentService; 31 } 32 33 public String list() { 34 request.put("employees", employssService.getAll()); 35 System.out.println("request: " + request.size()); 36 return "list"; 37 } 38 39 private Integer id; 40 41 public void setId(Integer id) { 42 this.id = id; 43 } 44 45 private InputStream inputStream; 46 47 public InputStream getInputStream() { 48 return inputStream; 49 } 50 //回调函数。判断是否删除 51 public String delete() { 52 try { 53 employssService.delete(id); 54 inputStream = new ByteArrayInputStream("1".getBytes("UTF-8")); 55 } catch (Exception e) { 56 e.printStackTrace(); 57 try { 58 inputStream = new ByteArrayInputStream("0".getBytes("UTF-8")); 59 } catch (UnsupportedEncodingException e1) { 60 e1.printStackTrace(); 61 } 62 } 63 return "ajax-success"; 64 } 65 66 private String lastName; 67 68 public void setLastName(String lastName) { 69 this.lastName = lastName; 70 } 71 //回调函数。判断用户名是否存在。 72 public String validateLastName() { 73 try { 74 if (employssService.lastNameIsValid(lastName)) { 75 76 inputStream = new ByteArrayInputStream("1".getBytes("utf-8")); 77 } else { 78 79 inputStream = new ByteArrayInputStream("0".getBytes("utf-8")); 80 } 81 } catch (Exception e) { 82 83 } 84 return "ajax-success"; 85 } 86 87 private Employee model; 88 89 /* 90 * 可以根椐ID来判断为save方法准备的model是new的还是数据库获取的。 91 */ 92 public void prepareSave() { 93 if (id == null) { 94 model = new Employee(); 95 } else { 96 model = employssService.get(id); 97 } 98 } 99 100 public String save() { 101 102 if (id == null) { 103 model.setCreateTime(new Date()); 104 105 } 106 employssService.saveOrUpdate(model); 107 return SUCCESS; 108 } 109 110 public String input() { 111 request.put("departments", departmentService.getAll()); 112 return INPUT; 113 } 114 115 public void prepareInput() { 116 if (id != null) { 117 model = employssService.get(id); 118 } 119 120 } 121 122 private Map<String, Object> request; 123 124 @Override 125 public void setRequest(Map<String, Object> arg0) { 126 this.request = arg0; 127 128 } 129 130 @Override 131 public Employee getModel() { 132 133 return model; 134 } 135 136 @Override 137 public void prepare() throws Exception { 138 139 } 140 141 }