atguigu.ssh-2.actions
2015-09-14 07:50
309 查看
package com.atguigu.ssh.actions;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Map;
import org.apache.struts2.interceptor.RequestAware;
import com.atguigu.ssh.entities.Employee;
import com.atguigu.ssh.service.DepartmentService;
import com.atguigu.ssh.service.EmployeeService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
public class EmployeeAction extends ActionSupport implements RequestAware,
ModelDriven<Employee>, Preparable{
/**
*
*/
private static final long serialVersionUID = 1L;
private EmployeeService employeeService;
public void setEmployeeService(EmployeeService employeeService) {
this.employeeService = employeeService;
}
private DepartmentService departmentService;
public void setDepartmentService(DepartmentService departmentService) {
this.departmentService = departmentService;
}
public String list() {
request.put("employees", employeeService.getAll());
return "list";
}
private Integer id;
public void setId(Integer id) {
this.id = id;
}
private InputStream inputStream;
public InputStream getInputStream() {
return inputStream;
}
public String delete() {
try {
employeeService.delete(id);
inputStream = new ByteArrayInputStream("1".getBytes("UTF-8"));
} catch (Exception e) {
e.printStackTrace();
try {
inputStream = new ByteArrayInputStream("0".getBytes("UTF-8"));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
}
return "ajax-success";
}
public String input(){
request.put("departments", departmentService.getAll());
return INPUT;
}
public void prepareInput(){
if(id != null){
model = employeeService.get(id);
}
}
public String save(){
if(id == null){
model.setCreateTime(new Date());
}
employeeService.saveOrUpdate(model);
return SUCCESS;
}
/**
* 可以根据 id 来判断为 save 方法准备的 model 是 new 的还是从数据库获取的!
*/
public void prepareSave(){
if(id == null){
model = new Employee();
}else{
model = employeeService.get(id);
}
}
private String lastName;
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String validateLastName() throws UnsupportedEncodingException{
if(employeeService.lastNameIsValid(lastName)){
inputStream = new ByteArrayInputStream("1".getBytes("UTF-8"));
}else{
inputStream = new ByteArrayInputStream("0".getBytes("UTF-8"));
}
return "ajax-success";
}
private Map<String, Object> request;
@Override
public void setRequest(Map<String, Object> arg0) {
this.request = arg0;
}
@Override
public void prepare() throws Exception {}
private Employee model;
@Override
public Employee getModel() {
return model;
}
}
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Map;
import org.apache.struts2.interceptor.RequestAware;
import com.atguigu.ssh.entities.Employee;
import com.atguigu.ssh.service.DepartmentService;
import com.atguigu.ssh.service.EmployeeService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
public class EmployeeAction extends ActionSupport implements RequestAware,
ModelDriven<Employee>, Preparable{
/**
*
*/
private static final long serialVersionUID = 1L;
private EmployeeService employeeService;
public void setEmployeeService(EmployeeService employeeService) {
this.employeeService = employeeService;
}
private DepartmentService departmentService;
public void setDepartmentService(DepartmentService departmentService) {
this.departmentService = departmentService;
}
public String list() {
request.put("employees", employeeService.getAll());
return "list";
}
private Integer id;
public void setId(Integer id) {
this.id = id;
}
private InputStream inputStream;
public InputStream getInputStream() {
return inputStream;
}
public String delete() {
try {
employeeService.delete(id);
inputStream = new ByteArrayInputStream("1".getBytes("UTF-8"));
} catch (Exception e) {
e.printStackTrace();
try {
inputStream = new ByteArrayInputStream("0".getBytes("UTF-8"));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
}
return "ajax-success";
}
public String input(){
request.put("departments", departmentService.getAll());
return INPUT;
}
public void prepareInput(){
if(id != null){
model = employeeService.get(id);
}
}
public String save(){
if(id == null){
model.setCreateTime(new Date());
}
employeeService.saveOrUpdate(model);
return SUCCESS;
}
/**
* 可以根据 id 来判断为 save 方法准备的 model 是 new 的还是从数据库获取的!
*/
public void prepareSave(){
if(id == null){
model = new Employee();
}else{
model = employeeService.get(id);
}
}
private String lastName;
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String validateLastName() throws UnsupportedEncodingException{
if(employeeService.lastNameIsValid(lastName)){
inputStream = new ByteArrayInputStream("1".getBytes("UTF-8"));
}else{
inputStream = new ByteArrayInputStream("0".getBytes("UTF-8"));
}
return "ajax-success";
}
private Map<String, Object> request;
@Override
public void setRequest(Map<String, Object> arg0) {
this.request = arg0;
}
@Override
public void prepare() throws Exception {}
private Employee model;
@Override
public Employee getModel() {
return model;
}
}
相关文章推荐
- 使用GPUImage实现iOS 7的模糊效果
- [LeetCode#250] Count Univalue Subtrees
- UItableView之间的相互传值实现
- LeetCode Unique Paths II
- LeetCode Unique Paths II
- 【APUE】Chapter7 Process Environment
- Simple PWM to Analog Circuit (0-10vdc)
- how to generate an analog output from a in-built pwm of Atmega 32AVR microcontrloller?
- How to modify analog output range of Arduino Due
- 使用request.getParameter()读取参数时出现的中文乱码现象
- UI高级----Images.xcassets
- 用EasyUI的一些感受
- Android--Handler+Looper+MessageQueue+Thread线程之间的通信
- UIView 类
- UITableView cell的重复使用
- JSP中 request.getRealPath("/xx/yy") 方法提示已经过时的替代方法
- android开发---ndk-build不自动删除外部库
- easyui datagrid使用
- 将Fresco源码导入到Android Studio,Build失败的解决办法
- 百度UEditor本地化配置 笔记