JavaWeb项目开发案例精粹-第2章投票系统-004action层
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层的更多相关文章
- JavaWeb项目开发案例精粹-第2章投票系统-006view层
1.index.jsp <%@ page language="java" import="java.util.*" pageEncoding=" ...
- JavaWeb项目开发案例精粹-第2章投票系统-003Dao层
1. package com.sanqing.dao; import java.util.List; import com.sanqing.bean.Vote; import com.sanqing. ...
- JavaWeb项目开发案例精粹-第2章投票系统-001设计
1.项目结构 2.数据库设计 # MySQL-Front 5.0 (Build 1.0) /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE */; /*!40101 SET ...
- JavaWeb项目开发案例精粹-第2章投票系统-005实体层
1. package com.sanqing.bean; /** * * 投票选项类 * */ public class VoteOption { private int voteOptionID; ...
- JavaWeb项目开发案例精粹-第2章投票系统-002配置文件及公共类
1. <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5&qu ...
- JavaWeb项目开发案例精粹-第6章报价管理系统-05Action层
0. <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC &quo ...
- JavaWeb项目开发案例精粹-第6章报价管理系统-07View层
1. 2.back_index.html <HTML> <HEAD> <META HTTP-EQUIV="Content-Type" CONTENT= ...
- JavaWeb项目开发案例精粹-第6章报价管理系统-06po层
1. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www ...
- JavaWeb项目开发案例精粹-第6章报价管理系统-04Service层
1. package com.sanqing.service; import com.sanqing.dao.DAO; import com.sanqing.po.Customer; /** * 客户 ...
随机推荐
- VisualVM 监控
一:服务器端: 找到 jstatd 所在目录 find / -name jstatd 在此目录下添加 jstatd.all.policy 文件 cat /usr/java/jdk1.7.0_51/bi ...
- windows不能在本地计算机启动apache
今天,配置eclipse PHP studio 3.0的时候更改了apache http server 中的httpd.conf文件: 将DocumentRoot 的路径设错了,为一个不存在目录 .更 ...
- How to changes to Table & EDT Relations[AX2012]
Well I hope everyone is having a fine week so far. Oh Wednesdays, the furthermost point between two ...
- java中print\println\printf的区别
print\println\printf的区别 print将它的参数显示在命令窗口,并将输出光标定位在所显示的最后一个字符之后. println 将它的参数显示在命令窗口,并在结尾加上换行符,将输出光 ...
- [转]bat中的特殊字符,以及需要在bat中当做字符如何处理
bat中的特殊字符,以及需要在bat中当做字符如何处理 批处理.Bat 中特殊符号的实际作用,Windows 批处理中特殊符号的作用: @ \\隐藏命令的回显. ~ \\在for中表示使用增强的变量扩 ...
- TIME_WAIT引起Cannot assign requested address报错
1. 问题描述 有时候用redis客户端(php或者java客户端)连接Redis服务器,报错:"Cannot assign requested address." 原因是客户端 ...
- java排序集锦
java实现排序的一些方法,来自:http://www.javaeye.com/topic/548520 package sort; import java.util.Random; /** * 排序 ...
- 与MySQL交互(felixge/node-mysql)
目录 简介和安装 测试MySQL 认识一下Connection Options MYSQL CURD 插入 更新 查询 删除 Nodejs 调用带out参数的存储过程,并得到out参数返回值 结束数据 ...
- LintCode-Majority Number
Given an array of integers, the majority number is the number that occurs more than half of the size ...
- 10.31Daily Scrum
人员 任务分配完成情况 明天任务分配 王皓南 主网页的框架搭建,任务编号752 研究代码,学习相应语言,讨论设计思路 申开亮 学习数据库的操作,任务编号753 研究代码,学习相应语言,讨论设计思路 王 ...