首页>代码>s2sh开发的网页通讯录后台管理项目,学习用的>/JavaWebProject/src/com/kechengsheji/action/MemberInfoAction.java
package com.kechengsheji.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.UUID;

import org.apache.struts2.ServletActionContext;

import com.kechengsheji.dao.GroupClassDAO;
import com.kechengsheji.dao.MemberInfoDAO;
import com.kechengsheji.domain.GroupClass;
import com.kechengsheji.domain.MemberInfo;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class MemberInfoAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	/*图片字段photo参数接收*/
	 private File photoFile;
	 private String photoFileFileName;
	 private String photoFileContentType;
	 public File getPhotoFile() {
		return photoFile;
	}
	public void setPhotoFile(File photoFile) {
		this.photoFile = photoFile;
	}
	public String getPhotoFileFileName() {
		return photoFileFileName;
	}
	public void setPhotoFileFileName(String photoFileFileName) {
		this.photoFileFileName = photoFileFileName;
	}
	public String getPhotoFileContentType() {
		return photoFileContentType;
	}
	public void setPhotoFileContentType(String photoFileContentType) {
		this.photoFileContentType = photoFileContentType;
	}
    /*界面层需要查询的属性: 所属分组*/
    private GroupClass groupObj;
    public void setGroupObj(GroupClass groupObj) {
        this.groupObj = groupObj;
    }
    public GroupClass getGroupObj() {
        return this.groupObj;
    }

    /*界面层需要查询的属性: 姓名*/
    private String name;
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return this.name;
    }

    /*界面层需要查询的属性: 出生日期*/
    private String birthDate;
    public void setBirthDate(String birthDate) {
        this.birthDate = birthDate;
    }
    public String getBirthDate() {
        return this.birthDate;
    }

    /*界面层需要查询的属性: 身份证号*/
    private String cardNumber;
    public void setCardNumber(String cardNumber) {
        this.cardNumber = cardNumber;
    }
    public String getCardNumber() {
        return this.cardNumber;
    }

    /*界面层需要查询的属性: 家庭电话*/
    private String homeTelephone;
    public void setHomeTelephone(String homeTelephone) {
        this.homeTelephone = homeTelephone;
    }
    public String getHomeTelephone() {
        return this.homeTelephone;
    }

    /*界面层需要查询的属性: 手机号*/
    private String mobilePhone;
    public void setMobilePhone(String mobilePhone) {
        this.mobilePhone = mobilePhone;
    }
    public String getMobilePhone() {
        return this.mobilePhone;
    }

    /*当前第几页*/
    private int currentPage;
    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }
    public int getCurrentPage() {
        return currentPage;
    }

    /*一共多少页*/
    private int totalPage;
    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }
    public int getTotalPage() {
        return totalPage;
    }

    private int memberId;
    public void setMemberId(int memberId) {
        this.memberId = memberId;
    }
    public int getMemberId() {
        return memberId;
    }

    /*当前查询的总记录数目*/
    private int recordNumber;
    public void setRecordNumber(int recordNumber) {
        this.recordNumber = recordNumber;
    }
    public int getRecordNumber() {
        return recordNumber;
    }

    /*业务层对象*/
    MemberInfoDAO memberInfoDAO = new MemberInfoDAO();

    /*待操作的MemberInfo对象*/
    private MemberInfo memberInfo;
    public void setMemberInfo(MemberInfo memberInfo) {
        this.memberInfo = memberInfo;
    }
    public MemberInfo getMemberInfo() {
        return this.memberInfo;
    }

    /*跳转到添加MemberInfo视图*/
    public String AddView() {
        ActionContext ctx = ActionContext.getContext();
        /*查询所有的GroupClass信息*/
        GroupClassDAO groupClassDAO = new GroupClassDAO();
        List<GroupClass> groupClassList = groupClassDAO.QueryAllGroupClassInfo();
        ctx.put("groupClassList", groupClassList);
        return "add_view";
    }

    /*添加MemberInfo信息*/
    @SuppressWarnings("deprecation")
    public String AddMemberInfo() {
        ActionContext ctx = ActionContext.getContext();
        try {
            if(true) {
            GroupClassDAO groupClassDAO = new GroupClassDAO();
            GroupClass groupObj = groupClassDAO.GetGroupClassByGroupClassId(memberInfo.getGroupObj().getGroupClassId());
            memberInfo.setGroupObj(groupObj);
            }
            String path = ServletActionContext.getServletContext().getRealPath("/upload"); 
            /*处理图片上传*/
            String photoFileName = ""; 
       	 	if(photoFile != null) {
       	 		InputStream is = new FileInputStream(photoFile);
       			String fileContentType = this.getPhotoFileContentType();
       			if(fileContentType.equals("image/jpeg")  || fileContentType.equals("image/pjpeg"))
       				photoFileName = UUID.randomUUID().toString() +  ".jpg";
       			else if(fileContentType.equals("image/gif"))
       				photoFileName = UUID.randomUUID().toString() +  ".gif";
       			else {
       				ctx.put("error",  java.net.URLEncoder.encode("上传图片格式不正确!"));
       				return "error";
       			}
       			File file = new File(path, photoFileName);
       			OutputStream os = new FileOutputStream(file);
       			byte[] b = new byte[1024];
       			int bs = 0;
       			while ((bs = is.read(b)) > 0) {
       				os.write(b, 0, bs);
       			}
       			is.close();
       			os.close();
       	 	}
            if(photoFile != null)
            	memberInfo.setPhoto("upload/" + photoFileName);
            else
            	memberInfo.setPhoto("upload/NoImage.jpg");
            memberInfoDAO.AddMemberInfo(memberInfo);
            ctx.put("message",  java.net.URLEncoder.encode("MemberInfo添加成功!"));
            return "add_success";
        } catch (Exception e) {
            e.printStackTrace();
            ctx.put("error",  java.net.URLEncoder.encode("MemberInfo添加失败!"));
            return "error";
        }
    }

    /*查询MemberInfo信息*/
    public String QueryMemberInfo() {
        if(currentPage == 0) currentPage = 1;
        if(name == null) name = "";
        if(birthDate == null) birthDate = "";
        if(cardNumber == null) cardNumber = "";
        if(homeTelephone == null) homeTelephone = "";
        if(mobilePhone == null) mobilePhone = "";
        List<MemberInfo> memberInfoList = memberInfoDAO.QueryMemberInfoInfo(groupObj, name, birthDate, cardNumber, homeTelephone, mobilePhone, currentPage);
        /*计算总的页数和总的记录数*/
        memberInfoDAO.CalculateTotalPageAndRecordNumber(groupObj, name, birthDate, cardNumber, homeTelephone, mobilePhone);
        /*获取到总的页码数目*/
        totalPage = memberInfoDAO.getTotalPage();
        /*当前查询条件下总记录数*/
        recordNumber = memberInfoDAO.getRecordNumber();
        ActionContext ctx = ActionContext.getContext();
        ctx.put("memberInfoList",  memberInfoList);
        ctx.put("totalPage", totalPage);
        ctx.put("recordNumber", recordNumber);
        ctx.put("currentPage", currentPage);
        ctx.put("groupObj", groupObj);
        GroupClassDAO groupClassDAO = new GroupClassDAO();
        List<GroupClass> groupClassList = groupClassDAO.QueryAllGroupClassInfo();
        ctx.put("groupClassList", groupClassList);
        ctx.put("name", name);
        ctx.put("birthDate", birthDate);
        ctx.put("cardNumber", cardNumber);
        ctx.put("homeTelephone", homeTelephone);
        ctx.put("mobilePhone", mobilePhone);
        return "query_view";
    }

    /*前台查询MemberInfo信息*/
    public String FrontQueryMemberInfo() {
        if(currentPage == 0) currentPage = 1;
        if(name == null) name = "";
        if(birthDate == null) birthDate = "";
        if(cardNumber == null) cardNumber = "";
        if(homeTelephone == null) homeTelephone = "";
        if(mobilePhone == null) mobilePhone = "";
        List<MemberInfo> memberInfoList = memberInfoDAO.QueryMemberInfoInfo(groupObj, name, birthDate, cardNumber, homeTelephone, mobilePhone, currentPage);
        /*计算总的页数和总的记录数*/
        memberInfoDAO.CalculateTotalPageAndRecordNumber(groupObj, name, birthDate, cardNumber, homeTelephone, mobilePhone);
        /*获取到总的页码数目*/
        totalPage = memberInfoDAO.getTotalPage();
        /*当前查询条件下总记录数*/
        recordNumber = memberInfoDAO.getRecordNumber();
        ActionContext ctx = ActionContext.getContext();
        ctx.put("memberInfoList",  memberInfoList);
        ctx.put("totalPage", totalPage);
        ctx.put("recordNumber", recordNumber);
        ctx.put("currentPage", currentPage);
        ctx.put("groupObj", groupObj);
        GroupClassDAO groupClassDAO = new GroupClassDAO();
        List<GroupClass> groupClassList = groupClassDAO.QueryAllGroupClassInfo();
        ctx.put("groupClassList", groupClassList);
        ctx.put("name", name);
        ctx.put("birthDate", birthDate);
        ctx.put("cardNumber", cardNumber);
        ctx.put("homeTelephone", homeTelephone);
        ctx.put("mobilePhone", mobilePhone);
        return "front_query_view";
    }

    /*查询要修改的MemberInfo信息*/
    public String ModifyMemberInfoQuery() {
        ActionContext ctx = ActionContext.getContext();
        /*根据主键memberId获取MemberInfo对象*/
        MemberInfo memberInfo = memberInfoDAO.GetMemberInfoByMemberId(memberId);

        GroupClassDAO groupClassDAO = new GroupClassDAO();
        List<GroupClass> groupClassList = groupClassDAO.QueryAllGroupClassInfo();
        ctx.put("groupClassList", groupClassList);
        ctx.put("memberInfo",  memberInfo);
        return "modify_view";
    }

    /*查询要修改的MemberInfo信息*/
    public String FrontShowMemberInfoQuery() {
        ActionContext ctx = ActionContext.getContext();
        /*根据主键memberId获取MemberInfo对象*/
        MemberInfo memberInfo = memberInfoDAO.GetMemberInfoByMemberId(memberId);

        GroupClassDAO groupClassDAO = new GroupClassDAO();
        List<GroupClass> groupClassList = groupClassDAO.QueryAllGroupClassInfo();
        ctx.put("groupClassList", groupClassList);
        ctx.put("memberInfo",  memberInfo);
        return "front_show_view";
    }

    /*更新修改MemberInfo信息*/
    public String ModifyMemberInfo() {
        ActionContext ctx = ActionContext.getContext();
        try {
            if(true) {
            GroupClassDAO groupClassDAO = new GroupClassDAO();
            GroupClass groupObj = groupClassDAO.GetGroupClassByGroupClassId(memberInfo.getGroupObj().getGroupClassId());
            memberInfo.setGroupObj(groupObj);
            }
            String path = ServletActionContext.getServletContext().getRealPath("/upload"); 
            /*处理图片上传*/
            String photoFileName = ""; 
       	 	if(photoFile != null) {
       	 		InputStream is = new FileInputStream(photoFile);
       			String fileContentType = this.getPhotoFileContentType();
       			if(fileContentType.equals("image/jpeg") || fileContentType.equals("image/pjpeg"))
       				photoFileName = UUID.randomUUID().toString() +  ".jpg";
       			else if(fileContentType.equals("image/gif"))
       				photoFileName = UUID.randomUUID().toString() +  ".gif";
       			else {
       				ctx.put("error",  java.net.URLEncoder.encode("上传图片格式不正确!"));
       				return "error";
       			}
       			File file = new File(path, photoFileName);
       			OutputStream os = new FileOutputStream(file);
       			byte[] b = new byte[1024];
       			int bs = 0;
       			while ((bs = is.read(b)) > 0) {
       				os.write(b, 0, bs);
       			}
       			is.close();
       			os.close();
            memberInfo.setPhoto("upload/" + photoFileName);
       	 	}
            memberInfoDAO.UpdateMemberInfo(memberInfo);
            ctx.put("message",  java.net.URLEncoder.encode("MemberInfo信息更新成功!"));
            return "modify_success";
        } catch (Exception e) {
            e.printStackTrace();
            ctx.put("error",  java.net.URLEncoder.encode("MemberInfo信息更新失败!"));
            return "error";
       }
   }

    /*删除MemberInfo信息*/
    public String DeleteMemberInfo() {
        ActionContext ctx = ActionContext.getContext();
        try { 
            memberInfoDAO.DeleteMemberInfo(memberId);
            ctx.put("message",  java.net.URLEncoder.encode("MemberInfo删除成功!"));
            return "delete_success";
        } catch (Exception e) { 
            e.printStackTrace();
            ctx.put("error",  java.net.URLEncoder.encode("MemberInfo删除失败!"));
            return "error";
        }
    }

}
最近下载更多
and123456  LV11 2022年3月16日
644800782  LV4 2022年3月1日
刘可心  LV2 2021年3月20日
2215593477  LV9 2021年1月11日
liangge2115  LV27 2020年7月15日
双方各何必呢  LV13 2020年7月2日
EternalIy  LV4 2020年6月17日
大杰打劫  LV8 2020年3月24日
ljlssb  LV1 2019年12月16日
yawy007  LV4 2019年12月6日
最近浏览更多
哈哈ha zhh  LV2 2023年11月7日
haoghuang 2023年1月3日
暂无贡献等级
3473084268  LV9 2022年12月11日
欧神1327 2022年12月11日
暂无贡献等级
Yier123  LV8 2022年9月30日
北末qwert  LV2 2022年5月18日
暂无贡献等级
sunflower3 2022年5月13日
暂无贡献等级
xcwDeng0001  LV5 2022年3月13日
644800782  LV4 2022年3月1日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友