JavaWeb项目开发案例精粹-第2章投票系统-004action层
1.
1 package com.sanqing.action; 2 3 import java.util.UUID; 4 5 import com.opensymphony.xwork2.ActionSupport; 6 import com.sanqing.bean.Vote; 7 import com.sanqing.bean.VoteOption; 8 import com.sanqing.dao.VoteDAO; 9 import com.sanqing.dao.VoteOptionDAO; 10 import com.sanqing.daoFactory.VoteDAOFactory; 11 import com.sanqing.daoFactory.VoteOptionDAOFactory; 12 13 public class AddVoteAction extends ActionSupport { 14 private int channel; // 封装channel参数 15 private String voteName; // 封装voteName参数 16 private String[] voteOption;// 封装voteOption参数 17 18 public int getChannel() { 19 return channel; 20 } 21 22 public void setChannel(int channel) { 23 this.channel = channel; 24 } 25 26 public String getVoteName() { 27 return voteName; 28 } 29 30 public void setVoteName(String voteName) { 31 this.voteName = voteName; 32 } 33 34 public String[] getVoteOption() { 35 return voteOption; 36 } 37 38 public void setVoteOption(String[] voteOption) { 39 this.voteOption = voteOption; 40 } 41 42 public String execute() throws Exception { 43 VoteDAO voteDAO = VoteDAOFactory.getVoteDAOInstance();//获得VoteDAO实例 44 VoteOptionDAO voteOptionDAO = 45 VoteOptionDAOFactory.getVoteOptionDAOInstance();//获得voteOption实例 46 //首先保存投票,然后再保存投票选项 47 Vote vote = new Vote(); 48 vote.setChannelID(channel); 49 vote.setVoteName(voteName); 50 voteDAO.addVote(vote); 51 //查询投票ID 52 int voteID = voteDAO.findVoteByName(voteName).getVoteID(); 53 //保存投票选项 54 for(String voteOptionName : voteOption) { 55 VoteOption vp = new VoteOption(); 56 vp.setVoteID(voteID); 57 vp.setVoteOptionName(voteOptionName); 58 voteOptionDAO.addVoteOption(vp); 59 } 60 return this.SUCCESS; 61 } 62 63 }
2.
1 package com.sanqing.action; 2 3 import java.util.List; 4 5 import com.opensymphony.xwork2.ActionSupport; 6 import com.sanqing.bean.VoteOption; 7 import com.sanqing.dao.VoteDAO; 8 import com.sanqing.dao.VoteOptionDAO; 9 import com.sanqing.daoFactory.VoteDAOFactory; 10 import com.sanqing.daoFactory.VoteOptionDAOFactory; 11 12 public class DeleteVoteAction extends ActionSupport{ 13 private int voteID; 14 15 public int getVoteID() { 16 return voteID; 17 } 18 19 public void setVoteID(int voteID) { 20 this.voteID = voteID; 21 } 22 23 public String execute() throws Exception { 24 VoteDAO voteDAO = VoteDAOFactory.getVoteDAOInstance(); 25 VoteOptionDAO voteOptionDAO = VoteOptionDAOFactory 26 .getVoteOptionDAOInstance(); 27 //通过该投票ID查找该投票下的所有投票选项 28 List<VoteOption> voteOptions = voteOptionDAO.findVoteOptionByVoteID(voteID); 29 //循环进行删除 30 for(VoteOption voteOption : voteOptions) { 31 voteOptionDAO.deleteVoteOption(voteOption.getVoteOptionID()); 32 } 33 //再删除该投票 34 voteDAO.deleteVote(voteID); 35 return this.SUCCESS; 36 } 37 }
3.
1 package com.sanqing.action; 2 3 import javax.servlet.http.Cookie; 4 5 import org.apache.struts2.ServletActionContext; 6 7 import com.opensymphony.xwork2.ActionContext; 8 import com.opensymphony.xwork2.ActionSupport; 9 import com.sanqing.bean.VoteOption; 10 import com.sanqing.dao.VoteOptionDAO; 11 import com.sanqing.daoFactory.VoteOptionDAOFactory; 12 13 public class DoVoteAction extends ActionSupport{ 14 private int voteOptionID; 15 private String otherOption; 16 private int voteID; 17 private int channelID; 18 19 public int getChannelID() { 20 return channelID; 21 } 22 public void setChannelID(int channelID) { 23 this.channelID = channelID; 24 } 25 public int getVoteID() { 26 return voteID; 27 } 28 29 public void setVoteID(int voteID) { 30 this.voteID = voteID; 31 } 32 33 public int getVoteOptionID() { 34 return voteOptionID; 35 } 36 37 public void setVoteOptionID(int voteOptionID) { 38 this.voteOptionID = voteOptionID; 39 } 40 41 public String getOtherOption() { 42 return otherOption; 43 } 44 45 public void setOtherOption(String otherOption) { 46 this.otherOption = otherOption; 47 } 48 49 public String execute() throws Exception { 50 VoteOptionDAO voteOptionDAO = VoteOptionDAOFactory 51 .getVoteOptionDAOInstance(); 52 //首先判断用户是否能进行投票 53 Cookie cookies[]= ServletActionContext.getRequest().getCookies();//取出cookies 54 55 for(Cookie cookie : cookies) {//遍历cookies 56 57 if(cookie.getValue().equals(Integer.toString(voteID))) {//如果用户已经投过票 58 this.addActionError("您今天已经投过票了,请明天再来!"); 59 return this.INPUT; 60 } 61 } 62 //判断是否选择其他选项 63 if(voteOptionID == 0) { 64 //添加该选项 65 VoteOption voteOption = new VoteOption(); 66 voteOption.setVoteID(voteID); 67 voteOption.setVoteOptionName(otherOption); 68 voteOption.setTicketNum(1); 69 voteOptionDAO.addVoteOption(voteOption); 70 }else { 71 //取出以前的投票选项 72 VoteOption voteOption = voteOptionDAO.findVoteOptionById(voteOptionID); 73 int ticketNum = voteOption.getTicketNum(); 74 //更新选项的投票数 75 voteOption.setTicketNum(ticketNum + 1); 76 voteOptionDAO.updateVoteOption(voteOption); 77 //更新完成后,添加cookie,防止重复投票 78 Cookie cookie = new Cookie("hasVote" + voteID,Integer.toString(voteID)); 79 ServletActionContext.getResponse().addCookie(cookie); 80 } 81 return this.SUCCESS; 82 } 83 84 }
4.
1 package com.sanqing.action; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import javax.servlet.http.HttpServletRequest; 7 8 import org.apache.struts2.ServletActionContext; 9 10 import com.opensymphony.xwork2.ActionSupport; 11 import com.sanqing.bean.Vote; 12 import com.sanqing.bean.VoteOption; 13 import com.sanqing.bean.VoteResult; 14 import com.sanqing.dao.VoteDAO; 15 import com.sanqing.dao.VoteOptionDAO; 16 import com.sanqing.daoFactory.VoteDAOFactory; 17 import com.sanqing.daoFactory.VoteOptionDAOFactory; 18 import com.sanqing.util.Page; 19 import com.sanqing.util.PageUtil; 20 21 public class ShowVoteAction extends ActionSupport { 22 private int currentPage; 23 24 public int getCurrentPage() { 25 return currentPage; 26 } 27 28 public void setCurrentPage(int currentPage) { 29 this.currentPage = currentPage; 30 } 31 32 public String execute() throws Exception { 33 VoteDAO voteDAO = VoteDAOFactory.getVoteDAOInstance(); 34 VoteOptionDAO voteOptionDAO = VoteOptionDAOFactory 35 .getVoteOptionDAOInstance(); 36 int totalCount = voteDAO.findAllCount(); 37 Page page = PageUtil.createPage(10, totalCount, currentPage); 38 List<Vote> votes = voteDAO.findAllVote(page); 39 List<VoteResult> voteResultList = new ArrayList<VoteResult>(); 40 for (Vote vote : votes) { 41 List<VoteOption> voteOptions = voteOptionDAO 42 .findVoteOptionByVoteID(vote.getVoteID()); 43 VoteResult voteResult = new VoteResult(); 44 voteResult.setVote(vote); 45 voteResult.setVoteOptions(voteOptions); 46 voteResultList.add(voteResult); 47 } 48 HttpServletRequest request = ServletActionContext.getRequest(); 49 request.setAttribute("voteResultList",voteResultList); 50 request.setAttribute("page",page); 51 return this.SUCCESS; 52 } 53 }
5.
1 package com.sanqing.action; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import javax.servlet.http.HttpServletRequest; 7 8 import org.apache.struts2.ServletActionContext; 9 10 import com.opensymphony.xwork2.ActionSupport; 11 import com.sanqing.bean.Vote; 12 import com.sanqing.bean.VoteOption; 13 import com.sanqing.bean.VoteResult; 14 import com.sanqing.dao.VoteDAO; 15 import com.sanqing.dao.VoteOptionDAO; 16 import com.sanqing.daoFactory.VoteDAOFactory; 17 import com.sanqing.daoFactory.VoteOptionDAOFactory; 18 import com.sanqing.util.Page; 19 import com.sanqing.util.PageUtil; 20 21 public class ShowVoteByChannelAction extends ActionSupport { 22 private int channelID; 23 private int currentPage; 24 25 public int getChannelID() { 26 return channelID; 27 } 28 29 public void setChannelID(int channelID) { 30 this.channelID = channelID; 31 } 32 33 public int getCurrentPage() { 34 return currentPage; 35 } 36 37 public void setCurrentPage(int currentPage) { 38 this.currentPage = currentPage; 39 } 40 41 public String execute() throws Exception { 42 VoteDAO voteDAO = VoteDAOFactory.getVoteDAOInstance(); 43 VoteOptionDAO voteOptionDAO = VoteOptionDAOFactory 44 .getVoteOptionDAOInstance(); 45 // 获得该频道下的记录数 46 int totalCount = voteDAO.fintCountByChannel(channelID); 47 // 设置分页信息 48 Page page = PageUtil.createPage(3, totalCount, currentPage); 49 //取得该频道下的记录 50 List<Vote> votes = voteDAO.findVoteByChannel(page, channelID); 51 //存放所有投票和投票选项 52 List<VoteResult> voteResultList = new ArrayList<VoteResult>(); 53 for (Vote vote : votes) { 54 //查询该投票下的所有投票选项 55 List<VoteOption> voteOptions = voteOptionDAO 56 .findVoteOptionByVoteID(vote.getVoteID()); 57 VoteResult voteResult = new VoteResult(); 58 voteResult.setVote(vote); 59 voteResult.setVoteOptions(voteOptions); 60 voteResultList.add(voteResult); 61 } 62 HttpServletRequest request = ServletActionContext.getRequest(); 63 request.setAttribute("voteResultList",voteResultList); 64 request.setAttribute("page",page); 65 return this.SUCCESS; 66 } 67 }
6.
1 package com.sanqing.action; 2 3 import java.util.List; 4 5 import org.jfree.chart.ChartFactory; 6 import org.jfree.chart.JFreeChart; 7 import org.jfree.chart.plot.PlotOrientation; 8 import org.jfree.data.category.DefaultCategoryDataset; 9 10 import com.opensymphony.xwork2.ActionSupport; 11 import com.sanqing.bean.Vote; 12 import com.sanqing.bean.VoteOption; 13 import com.sanqing.dao.VoteDAO; 14 import com.sanqing.dao.VoteOptionDAO; 15 import com.sanqing.daoFactory.VoteDAOFactory; 16 import com.sanqing.daoFactory.VoteOptionDAOFactory; 17 18 public class VoteResultAction extends ActionSupport { 19 private JFreeChart chart; 20 private int voteID;//投票ID 21 22 public int getVoteID() { 23 return voteID; 24 } 25 26 public void setVoteID(int voteID) { 27 this.voteID = voteID; 28 } 29 30 public JFreeChart getChart() { 31 VoteDAO voteDAO = VoteDAOFactory.getVoteDAOInstance();//获得VoteDAO实例 32 VoteOptionDAO voteOptionDAO = 33 VoteOptionDAOFactory.getVoteOptionDAOInstance();//获得voteOption实例 34 //根据投票ID得到的投票 35 Vote vote = voteDAO.findVoteById(voteID); 36 String voteName = vote.getVoteName(); //得到投票名称 37 38 //根据投票ID得到所有的投票选项 39 List<VoteOption> voteOptions = voteOptionDAO.findVoteOptionByVoteID(voteID); 40 41 DefaultCategoryDataset dcd = new DefaultCategoryDataset();//数据源 42 43 //设置数据 44 for(VoteOption voteOption : voteOptions) { 45 dcd.setValue(voteOption.getTicketNum(),"",voteOption.getVoteOptionName()); 46 } 47 //使用工厂类创建柱状图 48 JFreeChart chart = ChartFactory.createBarChart3D( 49 voteName, 50 "投票选项", 51 "投票数", 52 dcd, 53 PlotOrientation.VERTICAL , 54 false, 55 true, 56 false); 57 58 return chart; 59 } 60 61 public void setChart(JFreeChart chart) { 62 this.chart = chart; 63 } 64 65 public String execute() throws Exception { 66 return SUCCESS; 67 } 68 }
You can do anything you set your mind to, man!