AfficheAction.java
资源名称:(J2EE)oa.rar [点击查看]
上传用户:lm2018
上传日期:2015-12-12
资源大小:30449k
文件大小:12k
源码类别:
Jsp/Servlet
开发平台:
Java
- //Created by MyEclipse Struts
- // XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.1.0/xslt/JavaClass.xsl
- package com.oa.module.pub.affiche;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.PrintWriter;
- import java.util.Enumeration;
- import java.util.Hashtable;
- import java.util.List;
- import java.util.Map;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- import org.apache.struts.actions.DispatchAction;
- import org.apache.struts.upload.FormFile;
- import com.oa.module.office.user.Tuser;
- import com.oa.util.ToolUtil;
- import com.oa.util.XPage;
- /**
- * MyEclipse Struts
- * Creation date: 01-21-2008
- *
- * XDoclet definition:
- * @struts.action path="/affiche" name="afficheForm" input="/form/affiche.jsp" parameter="method" scope="request" validate="true"
- */
- public class AfficheAction extends DispatchAction{
- // --------------------------------------------------------- Instance Variables
- private AfficheDAO affichedao;
- // --------------------------------------------------------- Methods
- /**
- * Method query
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return ActionForward
- */
- public ActionForward query(
- ActionMapping mapping,
- ActionForm form,
- HttpServletRequest request,
- HttpServletResponse response) {
- AfficheForm actionForm = (AfficheForm)form;
- //刷新数据库
- affichedao.renovateDataBase();
- //获取页面参数
- String pagenum = request.getParameter("currentPage");
- int currentPage = 1;
- try {
- currentPage = Integer.parseInt(pagenum);
- } catch (Exception e) {
- currentPage=1;
- }
- int count = 2;
- System.out.println(currentPage);
- XPage page = affichedao.getAfficheList("query",null, null,null, currentPage,count);
- request.setAttribute("apage",page);
- String datetime = ToolUtil.getNowDate();
- request.setAttribute("datetime",datetime);
- return mapping.findForward("list");
- }
- /**
- * Method show
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return ActionForward
- */
- public ActionForward show(
- ActionMapping mapping,
- ActionForm form,
- HttpServletRequest request,
- HttpServletResponse response) {
- String id = request.getParameter("id");
- int aid = ToolUtil.parseInt(id);
- Map list = affichedao.getAfficheById(aid);
- request.setAttribute("affiche",list);
- String task = request.getParameter("task");
- if(task!=null&&task.equals("look")){
- return mapping.findForward("show");
- }else{
- return mapping.findForward("editshow");
- }
- }
- /**
- * Method editlist
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return ActionForward
- */
- public ActionForward editlist(
- ActionMapping mapping,
- ActionForm form,
- HttpServletRequest request,
- HttpServletResponse response) {
- AfficheForm afficheForm = (AfficheForm)form;
- //刷新数据库
- affichedao.renovateDataBase();
- String atitle = request.getParameter("atitle");
- String begintime = request.getParameter("begin");
- String endtime = request.getParameter("end");
- //获取页面参数
- String pagenum = request.getParameter("currentPage");
- int currentPage = 1;
- try {
- currentPage = Integer.parseInt(pagenum);
- } catch (Exception e) {
- currentPage=1;
- }
- int count = 2;
- XPage page = affichedao.getAfficheList("editlist",atitle, begintime, endtime,currentPage,count);
- request.setAttribute("apage",page);
- String datetime = ToolUtil.getNowDate();
- request.setAttribute("datetime",datetime);
- return mapping.findForward("editlist");
- }
- /**
- * Method add
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return ActionForward
- * @throws IOException
- */
- public ActionForward add(
- ActionMapping mapping,
- ActionForm form,
- HttpServletRequest request,
- HttpServletResponse response) throws IOException {
- AfficheForm afficheForm = (AfficheForm)form;
- String filename = "";
- String filepath = "/upload/affiche/";
- String dirPath = getServlet().getServletContext().getRealPath("/")
- + "/upload/affiche/";
- Hashtable fileh = afficheForm.getMultipartRequestHandler()
- .getFileElements();
- for (Enumeration e = fileh.keys(); e.hasMoreElements();) {
- String key = (String) e.nextElement();
- try {
- FormFile file = (FormFile) fileh.get(key);
- filename = filename + file.getFileName().trim()+","; // 文件名
- InputStream stream = file.getInputStream();// 把文件读入
- String path = servlet.getServletContext().getRealPath(
- "/upload/affiche/");// 取当前系统路径
- OutputStream bos = new FileOutputStream(path + "/"
- + file.getFileName());// 建立一个上传文件的输出流
- int bytesRead = 0;
- byte[] buffer = new byte[8192];
- while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
- bos.write(buffer, 0, bytesRead);// 将文件写入服务器
- }
- bos.close();
- stream.close();
- } catch (Exception ex) {
- // logger.debug("出错了", ex);
- ex.printStackTrace();
- }
- }
- afficheForm.setAffixname(filename);//设置文件名称信息
- afficheForm.setAffixpath(filepath);//设置文件路径信息
- String nowdate = ToolUtil.getNowDate();//获得当前日期
- afficheForm.setAsendTime(nowdate);
- HttpSession session = request.getSession();
- Tuser user = (Tuser) session.getAttribute("user");
- afficheForm.setFuno(user.getUno());
- PrintWriter out = response.getWriter();
- String contextPath = request.getContextPath();
- if(affichedao.addAffiche(afficheForm)){
- out.print("<script>");
- out.print("alert('添加公告成功!');");
- out.print("location.href ='"+contextPath+"/affiche.do?method=editlist'");
- out.print("</script>");
- }else{
- out.print("<script>");
- out.print("alert('添加公告失败!');");
- out.print("history.back();");
- out.print("</script>");
- }
- return null;
- }
- /**
- * Method edit
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return ActionForward
- * @throws IOException
- */
- public ActionForward edit(
- ActionMapping mapping,
- ActionForm form,
- HttpServletRequest request,
- HttpServletResponse response) throws IOException {
- AfficheForm afficheForm = (AfficheForm)form;
- String filename = "";
- String filepath = "/upload/affiche/";
- String dirPath = getServlet().getServletContext().getRealPath("/")
- + "/upload/affiche/";
- Hashtable fileh = afficheForm.getMultipartRequestHandler()
- .getFileElements();
- for (Enumeration e = fileh.keys(); e.hasMoreElements();) {
- String key = (String) e.nextElement();
- try {
- FormFile file = (FormFile) fileh.get(key);
- filename = filename + file.getFileName().trim()+","; // 文件名
- InputStream stream = file.getInputStream();// 把文件读入
- String path = servlet.getServletContext().getRealPath(
- "/upload/affiche/");// 取当前系统路径
- OutputStream bos = new FileOutputStream(path + "/"
- + file.getFileName());// 建立一个上传文件的输出流
- int bytesRead = 0;
- byte[] buffer = new byte[8192];
- while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
- bos.write(buffer, 0, bytesRead);// 将文件写入服务器
- }
- bos.close();
- stream.close();
- } catch (Exception ex) {
- // logger.debug("出错了", ex);
- ex.printStackTrace();
- }
- }
- afficheForm.setAffixname(filename);//设置文件名称信息
- afficheForm.setAffixpath(filepath);//设置文件路径信息
- PrintWriter out = response.getWriter();
- String contextPath = request.getContextPath();
- if(affichedao.updateAffiche(afficheForm)){
- out.print("<script>");
- out.print("alert('修改公告成功!');");
- out.print("location.href ='"+contextPath+"/affiche.do?method=editlist'");
- out.print("</script>");
- }else{
- out.print("<script>");
- out.print("alert('修改公告失败!');");
- out.print("history.back();");
- out.print("</script>");
- }
- return null;
- }
- /**
- * Method update 取出公告信息到修改页面
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return ActionForward
- */
- public ActionForward update(
- ActionMapping mapping,
- ActionForm form,
- HttpServletRequest request,
- HttpServletResponse response) {
- String id = request.getParameter("id");
- int aid = ToolUtil.parseInt(id);
- Map list = affichedao.getAfficheById(aid);
- request.setAttribute("affiche",list);
- return mapping.findForward("edit");
- }
- /**
- * Method delete
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return ActionForward
- * @throws IOException
- */
- public ActionForward delete(
- ActionMapping mapping,
- ActionForm form,
- HttpServletRequest request,
- HttpServletResponse response) throws IOException {
- String id = request.getParameter("id");
- int aid = ToolUtil.parseInt(id);
- PrintWriter out = response.getWriter();
- String contextPath = request.getContextPath();
- //获取上传的文件路径
- Map map = affichedao.getAfficheById(aid);
- String filename = (String) map.get("affixname");
- String[] names = filename.split(",");
- String realpath = getServlet().getServletContext().getRealPath("");// 上下文物理路径
- String filepath = (String) map.get("affixpath");// 存取文件相对路径
- for (int i = 0; i < names.length; i++) {
- String path = filepath + names[i];
- System.out.println(path);
- if (path!=null&&!path.equals("")){
- File o = new File(realpath+"/"+path);
- if (o.isFile()) {
- o.delete();
- }
- }
- }
- if(affichedao.deleteAfficheById(aid)){
- out.print("<script>");
- out.print("alert('删除公告成功!');");
- out.print("location.href ='"+contextPath+"/affiche.do?method=editlist'");
- out.print("</script>");
- }else{
- out.print("<script>");
- out.print("alert('删除公告失败!');");
- out.print("history.back();");
- out.print("</script>");
- }
- return null;
- }
- /**
- * Method audit
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return ActionForward
- * @throws IOException
- */
- public ActionForward audit(
- ActionMapping mapping,
- ActionForm form,
- HttpServletRequest request,
- HttpServletResponse response) throws IOException {
- String id = request.getParameter("id");
- int aid = ToolUtil.parseInt(id);
- String flag = request.getParameter("flag");
- if(flag!=null&&flag.equals("ok")){
- flag = "1";
- }else{
- flag = "-1";
- }
- PrintWriter out = response.getWriter();
- String contextPath = request.getContextPath();
- if(affichedao.auditAfficheById(aid,flag)){
- out.print("<script>");
- out.print("alert('审核操作成功!');");
- out.print("location.href ='"+contextPath+"/affiche.do?method=auditlist'");
- out.print("</script>");
- }else{
- out.print("<script>");
- out.print("alert('审核操作失败!');");
- out.print("history.back();");
- out.print("</script>");
- }
- return null;
- }
- /**
- * Method auditlist
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return ActionForward
- */
- public ActionForward auditlist(
- ActionMapping mapping,
- ActionForm form,
- HttpServletRequest request,
- HttpServletResponse response) {
- //刷新数据库
- affichedao.renovateDataBase();
- List list = affichedao.getNotAudit();
- request.setAttribute("affiche",list);
- return mapping.findForward("auditlist");
- }
- /**
- * Method auditshow
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return ActionForward
- */
- public ActionForward auditshow(
- ActionMapping mapping,
- ActionForm form,
- HttpServletRequest request,
- HttpServletResponse response) {
- String id = request.getParameter("id");
- int aid = ToolUtil.parseInt(id);
- Map list = affichedao.getAfficheById(aid);
- request.setAttribute("affiche",list);
- return mapping.findForward("audit");
- }
- public AfficheDAO getAffichedao() {
- return affichedao;
- }
- public void setAffichedao(AfficheDAO affichedao) {
- this.affichedao = affichedao;
- }
- }