首页>代码>Struts2 spring mybatis框架整合实例>/SSM/src/com/test/action/EmpAction.java
package com.test.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.test.common.Util;
import com.test.dao.IEmpDao;
import com.test.entity.Empinfo;

public class EmpAction extends ActionSupport {

	private IEmpDao empdao;
	private String result;

	public IEmpDao getEmpdao() {
		return empdao;
	}

	public void setEmpdao(IEmpDao empdao) {
		this.empdao = empdao;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	private int id;
	private String emplogin;
	private String emppass;
	private String empname;
	private String empdeptid;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getEmplogin() {
		return emplogin;
	}

	public void setEmplogin(String emplogin) {
		this.emplogin = Util.changeEncoding(emplogin);
	}

	public String getEmppass() {
		return emppass;
	}

	public void setEmppass(String emppass) {
		this.emppass = emppass;
	}

	public String getEmpname() {
		return empname;
	}

	public void setEmpname(String empname) {
		this.empname = Util.changeEncoding(empname);
	}

	public String getEmpdeptid() {
		return empdeptid;
	}

	public void setEmpdeptid(String empdeptid) {
		this.empdeptid = empdeptid;
	}
	
	public String[] sid;
	public String[] getSid() {
		return sid;
	}
	public void setSid(String[] sid) {
		this.sid = sid;
	}

	public String queryEmp() {
		Map<String, String> content = new HashMap<String, String>();
		content.put("emplogin", "%"+Util.changeEncoding(emplogin)+"%");
		content.put("empname", "%"+Util.changeEncoding(empname)+"%");
		content.put("empdeptid", "%"+Util.changeEncoding(empdeptid)+"%");
		Empinfo emp = (Empinfo)ActionContext.getContext().getSession().get("currentemp");
		if(null==emp){
			try {
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("result", "error");
				jsonObject.put("dataSets", "没有指定数据");
				HttpServletResponse response = ServletActionContext.getResponse();
				response.setCharacterEncoding("UTF-8");
				response.setContentType("text/plain");
				result = jsonObject.toString();
				System.out.println(result);
				this.setResult(result);
				response.getWriter().append(result);
			} catch (IOException e) {
				System.out.println("转换json异常");
			}
		}else{
		content.put("deptid", emp.getEmpdeptid());
		System.out.println(content);
		List<Empinfo> list = empdao.queryEmp(content);
		System.out.println(list);
		if (list.size() > 0) {
			try {
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("result", "success");
				jsonObject.put("dataSets", list);
				HttpServletResponse response = ServletActionContext.getResponse();
				response.setCharacterEncoding("UTF-8");
				response.setContentType("text/plain");
				result = jsonObject.toString();
				System.out.println(result);
				this.setResult(result);
				response.getWriter().append(result);
			} catch (IOException e) {
				System.out.println("转换json异常");
			}
		} else {
			try {
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("result", "error");
				jsonObject.put("dataSets", "没有指定数据");
				HttpServletResponse response = ServletActionContext.getResponse();
				response.setCharacterEncoding("UTF-8");
				response.setContentType("text/plain");
				result = jsonObject.toString();
				System.out.println(result);
				this.setResult(result);
				response.getWriter().append(result);
			} catch (IOException e) {
				System.out.println("转换json异常");
			}
		}}
		setNull();
		return NONE;
	}

	public String getEmp() {
		String []ids = sid;
		String idsstr = "0";
		for(int i=0;i<ids.length;i++){
			idsstr=ids[i];
		}
		HttpServletRequest request = ServletActionContext.getRequest();
		Empinfo emp = empdao.getEmp(idsstr);
		if (null == emp) {
			emp = new Empinfo();
		}
		ServletActionContext.getActionContext(request).getSession()
				.put("empinfo", emp);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/plain");
		System.out.println(result);
		this.setResult(result);
		setNull();
		return "success";
	}

	public String addEmp() {
		Map<String, String> content = new HashMap<String, String>();
		content.put("emplogin", emplogin);
		content.put("emppass", emppass);
		content.put("empname", empname);
		content.put("empdeptid", empdeptid);
		int num = empdao.addEmp(content);
		if(num>0){
			result="添加用户信息成功";
		}else{
			result="添加用户信息失败";
		}
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/plain");
		System.out.println(result);
		this.setResult(result);
		setNull();
		return "success";
	}

	public String updateEmp() {
		return "success";
	}

	public String saveEmp() {
		Map<String, String> content = new HashMap<String, String>();
		content.put("id", id+"");
		content.put("emplogin", emplogin);
		content.put("emppass", emppass);
		content.put("empname", empname);
		content.put("empdeptid", empdeptid);
		int num = empdao.saveEmp(content);
		if(num>0){
			result="更新用户信息成功";
		}else{
			result="更新用户信息失败";
		}
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/plain");
		System.out.println(result);
		this.setResult(result);
		setNull();
		return "success";
	}

	public String deleteEmp() {
		String []ids = sid;
//		String idsstr="0";
//		for(int i=0;i<ids.length;i++){
//			idsstr += ","+ids[i];
//		}
		int num = empdao.deleteEmp(ids);
		if(num>0){
			result="删除用户信息成功";
		}else{
			result="删除用户信息失败";
		}
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/plain");
		System.out.println(result);
		this.setResult(result);
		setNull();
		return "success";
	}
	public void setNull(){
		id=0;
		emplogin="";
		emppass="";
		empname="";
		empdeptid="";
	}
}
最近下载更多
1763316068  LV2 2019年6月12日
dingdehong  LV10 2018年12月27日
lxl1234  LV9 2018年10月28日
陈铳123456  LV1 2018年7月4日
zxd456  LV7 2018年5月28日
1579199398  LV2 2018年4月1日
xiaodada  LV7 2017年12月11日
huixian  LV1 2017年10月20日
CHK141  LV2 2017年10月16日
1147870524  LV2 2017年10月1日
最近浏览更多
rembiu 2022年9月12日
暂无贡献等级
谢谢谢谢谢谢你  LV6 2022年7月19日
三好青年  LV6 2021年10月25日
暖光女神  LV11 2020年5月12日
冰羽123456789 2020年3月23日
暂无贡献等级
houliukou  LV37 2020年2月21日
朱帅宇  LV1 2019年12月28日
xingxing1234  LV10 2019年8月6日
只要有你  LV8 2019年6月27日
louis黑椒糊  LV7 2019年6月17日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友