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. 加载页面遮挡耗时操作任务页面--第三方开源--AndroidProgressLayout

    在Android的开发中,往往有这种需求,比如一个耗时的操作,联网获取网络图片.内容,数据库耗时读写等等,在此耗时操作过程中,开发者也许不希望用户再进行其他操作(其他操作可能会引起逻辑混乱),而此时需 ...

  2. Windows7鼠标右键里没有新建文本文件的选项,解决办法

    1.“开始”->“运行”,输入"regedit",打开注册表编辑器 2.展开HKEY_CLASSES_ROOT,找到.txt 3.选中.txt,查看右侧窗格的“默认值”是不是 ...

  3. wpf MVVM ViewModel 关闭View显示

    上次说到了不同wpf窗体之间的交互,这个在MVVM模式之中用起来会方便很多 下面我来说下在ViewModel中关闭View的方法,其实也很简单的,注释照样不写,一看就懂的 public partial ...

  4. Oracle逻辑体系:数据文件黑盒的内在洞天

    select username,session_num,tablespace from v$sort_usage; Block: 块的组成 Header:包含数据块的概要信息:块地址,块属于哪个段,还 ...

  5. plateform_driver_register和plateform_device_register区别

    设备与驱动的两种绑定方式:在设备注册时进行绑定及在驱动注册时进行绑定. 以一个USB设备为例,有两种情形: (1)先插上USB设备并挂到总线中,然后在安装USB驱动程序过程中从总线上遍历各个设备,看驱 ...

  6. iOS 8安装教程图解

    苹果最新移动设备操作系统iOS 8终于在今天开放下载.相较于iOS 7,iOS 8此次最大的变化包括苹果全新的健康应用.允许用户跨设备操作的连续性(Continuity)功能等. iOS 8的整体软件 ...

  7. iOS 进阶 第十三天(0414)

    0414 画线.圆和圆弧的第二种方法 5中方法画矩形: 第二种画图形的方法: 之前做的方法都是先把要画的元素缓存到图形上下文CGConteextRef中去,现在第二种方法也缓存到CGPath中去.其实 ...

  8. 备份apt目录节省下载时间

    备份与清理      * 备份快速设置已下载的部分软件包,以便重装系统再次使用,免去重新下载的时间   tar cizvf backup.tar.gz /var/cache/apt/archives ...

  9. 3563: DZY Loves Chinese - BZOJ

    Description神校XJ之学霸兮,Dzy皇考曰JC.摄提贞于孟陬兮,惟庚寅Dzy以降.纷Dzy既有此内美兮,又重之以修能.遂降临于OI界,欲以神力而凌♂辱众生. 今Dzy有一魞歄图,其上有N座祭 ...

  10. 解决java写入xml报错org.w3c.dom.DOMException:DOM002 Illeg

    Exception is -- > org.w3c.dom.DOMException: DOM002 Illegal character 字符不被允许 org.w3c.dom.DOMExcept ...