JavaWeb项目开发案例精粹-第2章投票系统-004action层

时间:2022-10-29 23:50:27

1.

 package com.sanqing.action;

 import java.util.UUID;

 import com.opensymphony.xwork2.ActionSupport;
import com.sanqing.bean.Vote;
import com.sanqing.bean.VoteOption;
import com.sanqing.dao.VoteDAO;
import com.sanqing.dao.VoteOptionDAO;
import com.sanqing.daoFactory.VoteDAOFactory;
import com.sanqing.daoFactory.VoteOptionDAOFactory; public class AddVoteAction extends ActionSupport {
private int channel; // 封装channel参数
private String voteName; // 封装voteName参数
private String[] voteOption;// 封装voteOption参数 public int getChannel() {
return channel;
} public void setChannel(int channel) {
this.channel = channel;
} public String getVoteName() {
return voteName;
} public void setVoteName(String voteName) {
this.voteName = voteName;
} public String[] getVoteOption() {
return voteOption;
} public void setVoteOption(String[] voteOption) {
this.voteOption = voteOption;
} public String execute() throws Exception {
VoteDAO voteDAO = VoteDAOFactory.getVoteDAOInstance();//获得VoteDAO实例
VoteOptionDAO voteOptionDAO =
VoteOptionDAOFactory.getVoteOptionDAOInstance();//获得voteOption实例
//首先保存投票,然后再保存投票选项
Vote vote = new Vote();
vote.setChannelID(channel);
vote.setVoteName(voteName);
voteDAO.addVote(vote);
//查询投票ID
int voteID = voteDAO.findVoteByName(voteName).getVoteID();
//保存投票选项
for(String voteOptionName : voteOption) {
VoteOption vp = new VoteOption();
vp.setVoteID(voteID);
vp.setVoteOptionName(voteOptionName);
voteOptionDAO.addVoteOption(vp);
}
return this.SUCCESS;
} }

2.

 package com.sanqing.action;

 import java.util.List;

 import com.opensymphony.xwork2.ActionSupport;
import com.sanqing.bean.VoteOption;
import com.sanqing.dao.VoteDAO;
import com.sanqing.dao.VoteOptionDAO;
import com.sanqing.daoFactory.VoteDAOFactory;
import com.sanqing.daoFactory.VoteOptionDAOFactory; public class DeleteVoteAction extends ActionSupport{
private int voteID; public int getVoteID() {
return voteID;
} public void setVoteID(int voteID) {
this.voteID = voteID;
} public String execute() throws Exception {
VoteDAO voteDAO = VoteDAOFactory.getVoteDAOInstance();
VoteOptionDAO voteOptionDAO = VoteOptionDAOFactory
.getVoteOptionDAOInstance();
//通过该投票ID查找该投票下的所有投票选项
List<VoteOption> voteOptions = voteOptionDAO.findVoteOptionByVoteID(voteID);
//循环进行删除
for(VoteOption voteOption : voteOptions) {
voteOptionDAO.deleteVoteOption(voteOption.getVoteOptionID());
}
//再删除该投票
voteDAO.deleteVote(voteID);
return this.SUCCESS;
}
}

3.

 package com.sanqing.action;

 import javax.servlet.http.Cookie;

 import org.apache.struts2.ServletActionContext;

 import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sanqing.bean.VoteOption;
import com.sanqing.dao.VoteOptionDAO;
import com.sanqing.daoFactory.VoteOptionDAOFactory; public class DoVoteAction extends ActionSupport{
private int voteOptionID;
private String otherOption;
private int voteID;
private int channelID; public int getChannelID() {
return channelID;
}
public void setChannelID(int channelID) {
this.channelID = channelID;
}
public int getVoteID() {
return voteID;
} public void setVoteID(int voteID) {
this.voteID = voteID;
} public int getVoteOptionID() {
return voteOptionID;
} public void setVoteOptionID(int voteOptionID) {
this.voteOptionID = voteOptionID;
} public String getOtherOption() {
return otherOption;
} public void setOtherOption(String otherOption) {
this.otherOption = otherOption;
} public String execute() throws Exception {
VoteOptionDAO voteOptionDAO = VoteOptionDAOFactory
.getVoteOptionDAOInstance();
//首先判断用户是否能进行投票
Cookie cookies[]= ServletActionContext.getRequest().getCookies();//取出cookies for(Cookie cookie : cookies) {//遍历cookies if(cookie.getValue().equals(Integer.toString(voteID))) {//如果用户已经投过票
this.addActionError("您今天已经投过票了,请明天再来!");
return this.INPUT;
}
}
//判断是否选择其他选项
if(voteOptionID == 0) {
//添加该选项
VoteOption voteOption = new VoteOption();
voteOption.setVoteID(voteID);
voteOption.setVoteOptionName(otherOption);
voteOption.setTicketNum(1);
voteOptionDAO.addVoteOption(voteOption);
}else {
//取出以前的投票选项
VoteOption voteOption = voteOptionDAO.findVoteOptionById(voteOptionID);
int ticketNum = voteOption.getTicketNum();
//更新选项的投票数
voteOption.setTicketNum(ticketNum + 1);
voteOptionDAO.updateVoteOption(voteOption);
//更新完成后,添加cookie,防止重复投票
Cookie cookie = new Cookie("hasVote" + voteID,Integer.toString(voteID));
ServletActionContext.getResponse().addCookie(cookie);
}
return this.SUCCESS;
} }

4.

 package com.sanqing.action;

 import java.util.ArrayList;
import java.util.List; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport;
import com.sanqing.bean.Vote;
import com.sanqing.bean.VoteOption;
import com.sanqing.bean.VoteResult;
import com.sanqing.dao.VoteDAO;
import com.sanqing.dao.VoteOptionDAO;
import com.sanqing.daoFactory.VoteDAOFactory;
import com.sanqing.daoFactory.VoteOptionDAOFactory;
import com.sanqing.util.Page;
import com.sanqing.util.PageUtil; public class ShowVoteAction extends ActionSupport {
private int currentPage; public int getCurrentPage() {
return currentPage;
} public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
} public String execute() throws Exception {
VoteDAO voteDAO = VoteDAOFactory.getVoteDAOInstance();
VoteOptionDAO voteOptionDAO = VoteOptionDAOFactory
.getVoteOptionDAOInstance();
int totalCount = voteDAO.findAllCount();
Page page = PageUtil.createPage(10, totalCount, currentPage);
List<Vote> votes = voteDAO.findAllVote(page);
List<VoteResult> voteResultList = new ArrayList<VoteResult>();
for (Vote vote : votes) {
List<VoteOption> voteOptions = voteOptionDAO
.findVoteOptionByVoteID(vote.getVoteID());
VoteResult voteResult = new VoteResult();
voteResult.setVote(vote);
voteResult.setVoteOptions(voteOptions);
voteResultList.add(voteResult);
}
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("voteResultList",voteResultList);
request.setAttribute("page",page);
return this.SUCCESS;
}
}

5.

 package com.sanqing.action;

 import java.util.ArrayList;
import java.util.List; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport;
import com.sanqing.bean.Vote;
import com.sanqing.bean.VoteOption;
import com.sanqing.bean.VoteResult;
import com.sanqing.dao.VoteDAO;
import com.sanqing.dao.VoteOptionDAO;
import com.sanqing.daoFactory.VoteDAOFactory;
import com.sanqing.daoFactory.VoteOptionDAOFactory;
import com.sanqing.util.Page;
import com.sanqing.util.PageUtil; public class ShowVoteByChannelAction extends ActionSupport {
private int channelID;
private int currentPage; public int getChannelID() {
return channelID;
} public void setChannelID(int channelID) {
this.channelID = channelID;
} public int getCurrentPage() {
return currentPage;
} public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
} public String execute() throws Exception {
VoteDAO voteDAO = VoteDAOFactory.getVoteDAOInstance();
VoteOptionDAO voteOptionDAO = VoteOptionDAOFactory
.getVoteOptionDAOInstance();
// 获得该频道下的记录数
int totalCount = voteDAO.fintCountByChannel(channelID);
// 设置分页信息
Page page = PageUtil.createPage(3, totalCount, currentPage);
//取得该频道下的记录
List<Vote> votes = voteDAO.findVoteByChannel(page, channelID);
//存放所有投票和投票选项
List<VoteResult> voteResultList = new ArrayList<VoteResult>();
for (Vote vote : votes) {
//查询该投票下的所有投票选项
List<VoteOption> voteOptions = voteOptionDAO
.findVoteOptionByVoteID(vote.getVoteID());
VoteResult voteResult = new VoteResult();
voteResult.setVote(vote);
voteResult.setVoteOptions(voteOptions);
voteResultList.add(voteResult);
}
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("voteResultList",voteResultList);
request.setAttribute("page",page);
return this.SUCCESS;
}
}

6.

 package com.sanqing.action;

 import java.util.List;

 import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset; import com.opensymphony.xwork2.ActionSupport;
import com.sanqing.bean.Vote;
import com.sanqing.bean.VoteOption;
import com.sanqing.dao.VoteDAO;
import com.sanqing.dao.VoteOptionDAO;
import com.sanqing.daoFactory.VoteDAOFactory;
import com.sanqing.daoFactory.VoteOptionDAOFactory; public class VoteResultAction extends ActionSupport {
private JFreeChart chart;
private int voteID;//投票ID public int getVoteID() {
return voteID;
} public void setVoteID(int voteID) {
this.voteID = voteID;
} public JFreeChart getChart() {
VoteDAO voteDAO = VoteDAOFactory.getVoteDAOInstance();//获得VoteDAO实例
VoteOptionDAO voteOptionDAO =
VoteOptionDAOFactory.getVoteOptionDAOInstance();//获得voteOption实例
//根据投票ID得到的投票
Vote vote = voteDAO.findVoteById(voteID);
String voteName = vote.getVoteName(); //得到投票名称 //根据投票ID得到所有的投票选项
List<VoteOption> voteOptions = voteOptionDAO.findVoteOptionByVoteID(voteID); DefaultCategoryDataset dcd = new DefaultCategoryDataset();//数据源 //设置数据
for(VoteOption voteOption : voteOptions) {
dcd.setValue(voteOption.getTicketNum(),"",voteOption.getVoteOptionName());
}
//使用工厂类创建柱状图
JFreeChart chart = ChartFactory.createBarChart3D(
voteName,
"投票选项",
"投票数",
dcd,
PlotOrientation.VERTICAL ,
false,
true,
false); return chart;
} public void setChart(JFreeChart chart) {
this.chart = chart;
} public String execute() throws Exception {
return SUCCESS;
}
}

JavaWeb项目开发案例精粹-第2章投票系统-004action层的更多相关文章

  1. JavaWeb项目开发案例精粹-第2章投票系统-006view层

    1.index.jsp <%@ page language="java" import="java.util.*" pageEncoding=" ...

  2. JavaWeb项目开发案例精粹-第2章投票系统-003Dao层

    1. package com.sanqing.dao; import java.util.List; import com.sanqing.bean.Vote; import com.sanqing. ...

  3. JavaWeb项目开发案例精粹-第2章投票系统-001设计

    1.项目结构 2.数据库设计 # MySQL-Front 5.0 (Build 1.0) /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE */; /*!40101 SET ...

  4. JavaWeb项目开发案例精粹-第2章投票系统-005实体层

    1. package com.sanqing.bean; /** * * 投票选项类 * */ public class VoteOption { private int voteOptionID; ...

  5. JavaWeb项目开发案例精粹-第2章投票系统-002配置文件及公共类

    1. <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5&qu ...

  6. JavaWeb项目开发案例精粹-第6章报价管理系统-05Action层

    0. <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC &quo ...

  7. JavaWeb项目开发案例精粹-第6章报价管理系统-07View层

    1. 2.back_index.html <HTML> <HEAD> <META HTTP-EQUIV="Content-Type" CONTENT= ...

  8. JavaWeb项目开发案例精粹-第6章报价管理系统-06po层

    1. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www ...

  9. JavaWeb项目开发案例精粹-第6章报价管理系统-04Service层

    1. package com.sanqing.service; import com.sanqing.dao.DAO; import com.sanqing.po.Customer; /** * 客户 ...

随机推荐

  1. v14&period;0&bsol;AspNet&bsol;Microsoft&period;Web&period;AspNet&period;Props 找不到

    错误 E:\Github\AutoMapper\src\AutoMapper\AutoMapper.CoreCLR.kproj : error  : 未找到导入的项目"C:\Program ...

  2. morse code

    morse code,摩斯电码,是一种时通时断的信号代码,通过不同的排列顺序来表达不同的英文字母.数字和标点符号. 摩斯电码,是一种早期的数字化通信形式,但是它不同于现代只使用0和1两种状态的二进制代 ...

  3. C&num; 拷贝目录

    public class DirectoryExtends { /// <summary> /// 拷贝目录 /// </summary> /// <param name ...

  4. qt-5&period;6&period;0 移植之qt文件系统的建立

    经过差不多两个星期的奋斗,终于在板子里面跑起来了qt 程序,虽然现在还没有把触摸屏驱动加上去,但是我相信已经不远了!!!!! 在前两篇的随笔里面 , 已经编译好了最纯净的文件系统以及交叉编译完成了qt ...

  5. WinForm 控件库

    1:Telerik 介绍: Telerik 是保加利亚的一个软件公司,专注于微软.Net平台的表示层与内容管理控件.Telerik 提供高度稳定性和丰富性能的组件产品,并可应用在非常严格的环境中. 现 ...

  6. 每次都觉得很神奇的JS

    匿名,函数对象... var staff = [ {name: 'abruzzi', age: 24}, {name: 'bajmine', age: 26}, {name: 'chris', age ...

  7. ubuntu卸载vmware

    下面方法摘抄网上方法,经过证实好使 lsmod——显示已载入系统的模块 ps aux : 显示其他用户启动的进程(a)   查看系统中属于自己的进程(x)         启动这个进程的用户和它启动的 ...

  8. GNSS相关网站汇总

    转载: https://blog.csdn.net/zzh_my/article/details/78449972 一.bernese 数据表文件下载 ftp://nfs.kasi.re.kr rin ...

  9. EXPRESS项目PM2启动NODE&lowbar;ENV传参数不生效问题解决方法

    expree项目开发完,涉及到不同环境,要在启动到时候就要配置好环境变量, packge.json文件如下: "scripts": { "dev": &quot ...

  10. Nowcoder 牛客练习赛23

    Preface 终于知道YKH他们为什么那么喜欢打牛客网了原来可以抽衣服 那天晚上有空就也去玩了下,刷了一波水TM的YKH就抽到了,我当然是没有了 题目偏水,好像都是1A的.才打了一个半小时,回家就直 ...