LinkAction.java
上传用户:toby828
上传日期:2015-06-26
资源大小:8558k
文件大小:5k
源码类别:

Jsp/Servlet

开发平台:

Java

  1. package com.webtier;
  2. import javax.servlet.http.*;
  3. import com.domain.LinkForm;
  4. import org.apache.struts.action.*;
  5. import com.dao.LinkDao;
  6. import java.util.*;
  7. import com.tool.Chinese;
  8. import javax.servlet.ServletConfig;
  9. import javax.servlet.ServletException;
  10. import com.jspsmart.upload.SmartUpload;
  11. import java.io.UnsupportedEncodingException;
  12. import java.io.*;
  13. //网站连接的Action
  14. public class LinkAction
  15.     extends Action {
  16.   private LinkDao dao = null;
  17.   private int action;
  18.   public ActionForward execute(ActionMapping mapping,
  19.                                ActionForm form,
  20.                                HttpServletRequest request,
  21.                                HttpServletResponse response) throws
  22.       UnsupportedEncodingException {
  23.     request.setCharacterEncoding("gb2312");
  24.     this.action = Integer.parseInt(request.getParameter("action"));
  25.     dao = new LinkDao();
  26.     switch (action) {
  27.       case 0: {
  28.         return linkSelect(mapping, form, request, response); //对连接网站地址信息的全部查询
  29.       }
  30.       case 1: {
  31.         return linkForward(mapping, form, request, response); //通过方法转向添加信息的网页
  32.       }
  33.       case 2: {
  34.         return linkInsertForwardOne(mapping, form, request, response); //查询数据库中是否有这个网站信息
  35.       }
  36.       case 3: {
  37.        return linkInsert(mapping, form, request, response); //添加网站信息
  38.      }
  39.      case 4: {
  40.            return linkDelete(mapping, form, request, response); //删除网站信息
  41.      }
  42.     }
  43.     //  LinkForm linkForm = (LinkForm) form;
  44.     throw new java.lang.UnsupportedOperationException(
  45.         "Method $execute() not yet implemented.");
  46.   }
  47.   //删除网站信息
  48.   public ActionForward linkDelete(ActionMapping mapping,
  49.                                             ActionForm form,
  50.                                             HttpServletRequest request,
  51.                                             HttpServletResponse response) {
  52.    String path=request.getParameter("path");
  53.   dao.deleteLink(Integer.valueOf(request.getParameter("id")));
  54.     request.setAttribute("success","删除连接网站信息成功");
  55.     return mapping.findForward("linkOperation");
  56. }
  57.     //添加网站信息
  58.     public ActionForward linkInsert(ActionMapping mapping,
  59.                                               ActionForm form,
  60.                                               HttpServletRequest request,
  61.                                               HttpServletResponse response) {
  62.       LinkForm linkForm = (LinkForm) form;
  63.       linkForm.setLinkAddress(Chinese.chinese(request.getParameter("address")));
  64.       linkForm.setLinkName(Chinese.chinese(request.getParameter("name")));
  65.       linkForm.setLinkPicture(Chinese.chinese(request.getParameter("path")));
  66.       dao.insertLink(linkForm);
  67.       request.setAttribute("success","添加连接网站信息成功");
  68.       return mapping.findForward("linkOperation");
  69.   }
  70. //查询数据库中是否有这个网站信息
  71.   public ActionForward linkInsertForwardOne(ActionMapping mapping,
  72.                                             ActionForm form,
  73.                                             HttpServletRequest request,
  74.                                             HttpServletResponse response) {
  75.     String name = Chinese.chinese(request.getParameter("name"));
  76.     String address = Chinese.chinese(request.getParameter("address"));
  77.     LinkForm linkName = dao.selectOneName(name);
  78.     LinkForm linkAddress = dao.selectOneAddress(address);
  79.     if (linkName!=null) {
  80.       request.setAttribute("result", "此网站的名称已经存在");
  81.     }
  82.     else if (linkAddress != null) {
  83.        request.setAttribute("result","此网站的地址已经存在");
  84.     }
  85.     else {}
  86.     return mapping.findForward("linkInsertForwardOne");
  87.   }
  88.   //对连接网站地址信息的全部查询
  89.   public ActionForward linkSelect(ActionMapping mapping, ActionForm form,
  90.                                   HttpServletRequest request,
  91.                                   HttpServletResponse response) {
  92.     List list = dao.selectLink();
  93.     int pageNumber = list.size(); //计算出有多少条记录
  94.     int maxPage = pageNumber; //计算有多少页数
  95.     String number = request.getParameter("i");
  96.     if (maxPage % 7 == 0) {
  97.       maxPage = maxPage / 7;
  98.     }
  99.     else {
  100.       maxPage = maxPage / 7 + 1;
  101.     }
  102.     if (number == null) {
  103.       number = "0";
  104.     }
  105.     request.setAttribute("number", String.valueOf(number));
  106.     request.setAttribute("maxPage", String.valueOf(maxPage));
  107.     request.setAttribute("pageNumber", String.valueOf(pageNumber));
  108.     request.setAttribute("list", list);
  109.     return mapping.findForward("linkSelect");
  110.   }
  111. //通过方法转向添加信息的网页
  112.   public ActionForward linkForward(ActionMapping mapping, ActionForm form,
  113.                                    HttpServletRequest request,
  114.                                    HttpServletResponse response) {
  115.     return mapping.findForward("linkForward");
  116.   }
  117. }