码迷,mamicode.com
首页 > 数据库 > 详细

struts2和DBUtils结合做的一个投票系统练习

时间:2017-07-30 13:47:51      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:针对   port   项目   页面   sel   dex   入口   script   1.4   

需求分析:

题目:投票

功能要求

具体要求如下:

项目首页显示所有投票结果,按照创建时间降序排列

 技术分享

点击主题进入到查看页面,每次点击后,查看次数都增加1.查看页面效果图如下。百分比计算是根据总票数和该主题人数计算出的

 技术分享

当点击“我要投票”时,先判断用户是否选择了一项,如果没有选择给出下面提示

 技术分享

如果用户正确操作,对该选项的投票次数加1,并让主题表subjects中相关主题的投票人数加1。完成把最总结过显示到相关页面中

技术分享

给定如下数据表结构

 技术分享

-------投票系统入口------
-------index.jsp-------
<%
@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>投票系统入口</title> </head> <body> <a href="${pageContext.request.contextPath }/vote_findAll.action">入口</a> </body> </html>
--------------主题页面展示--------------------------
--------------votelist.jsp------------------------

<%
@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>查看投票信息</title> </head> <body> <table border="1px" align="center"> <tr align="center"> <td>序号</td> <td>主题</td> <td>投票/查看</td> <td>创建时间</td> </tr> <c:forEach items="${sublist }" var="vote"> <tr> <td>${vote.id }</td> <td><a href="${pageContext.request.contextPath }/vote_selectVote.action?id=${ vote.id }">${ vote.title }</a></td> <td>${vote.totalVotes }/${vote.viewTimes }</td> <td>${vote.createDate }</td> </tr> </c:forEach> </table> </body> </html>
-----------------------投票页面-----------------------------
--------------------------------handlevote.jsp----------------------------------
<%
@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script type="text/javascript" src="js/jquery-2.1.4.js"></script> <title>投票页面</title> </head> <body> <center> <input type="hidden" value="${subjects.id }" id="sid"> <h3>${subjects.title }</h3> ${subjects.viewTimes}次查看,共有${subjects.totalVotes }人投票 <form method="post" action="${pageContext.request.contextPath }/vote_finishVote.action?optContent=${op.optContent}&sid=${subjects.id }"> <table> <c:forEach items="${optlist }" var="op"> <tr> <td>${op.id }</td> <td><input type="radio" name="optContent" value="${op.optContent }">${op.optContent}</td> <td>${op.vote }票</td> <td><fmt:formatNumber type="number" value="${op.vote/subjects.totalVotes*100 }" maxFractionDigits="2"/>%</td> </tr> </c:forEach> <tr> <td colspan="3"><input type="submit" value="我要投票" onclick="return chooseVote()"/><td> <td><a href="${pageContext.request.contextPath }/vote_findAll.action">返回首页</a></td> </tr> </table> </form> </center> </body> <script type="text/javascript"> function chooseVote(){ var content = $("input[name=‘optContent‘]:checked").val(); if(content == null){ alert("请从列表中选择一项完成投票!!!"); return false; } return true; } </script> </html>
--------------------------投票完成后展示的页面--------------------------------
--------------------------finishhandle.jsp---------------------------------
<%
@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>完成</title> </head> <body> <h3>完成投票:${subjects.title }</h3> ${subjects.viewTimes}次查看,共有${subjects.totalVotes }人投票 <table> <c:forEach items="${optlist }" var="op"> <tr> <td>${op.id }</td> <td>${op.vote }票</td> <td><fmt:formatNumber type="number" value="${op.vote/subjects.totalVotes*100 }" maxFractionDigits="2"/>%</td> </tr> </c:forEach> <tr> <td><a href="${pageContext.request.contextPath }/vote_findAll.action">返回首页</a></td> </tr> </table> </body> </html>

Struts.jsp和struts-vote.jsp

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <!-- 配置常量-->
    <!-- 配置struts2编码常量只针对post提交方式 
         get提交方式的乱码问题需要我们手动解决
    -->
    <constant name="struts.i18n.encoding" value="UTF-8"/>
    <!-- 配置访问action的后缀名 -->
    <constant name="struts.action.extension" value="action,,"/>
    <!-- 
        struts.devMode:struts2的开发者模式
        在开发阶段:我们一般都会设置为true,这样我们在修改struts.xml的时候,就不用重启服务器了
        生产环境,设置为false
     -->
    <constant name="struts.devMode" value="true"/>
    <!-- 引入关联配置文件 -->
    <include file="struts-vote.xml"/>

</struts>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <package name="bamaw" namespace="/" extends="struts-default">
        <action name="vote_*" class="com.tbamaw.web.action.VoteAction" method="{1}">
            <result name="queryAll">/WEB-INF/jsp/votelist.jsp</result>
            <result name="selectVote">/WEB-INF/jsp/handlevote.jsp</result>
            <result name="finishVote">/WEB-INF/jsp/finishhandle.jsp</result>
        </action>
    </package>
</struts>

action

package com.tbamaw.web.action;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import com.tbamaw.domain.Options;
import com.tbamaw.domain.Subjects;
import com.tbamaw.service.OptionsService;
import com.tbamaw.service.SubjectsService;

public class VoteAction extends ActionSupport{

    private static final long serialVersionUID = 1L;
    private int id;
    private int sid;
    private String optContent;
    private Subjects subjects = new Subjects();
    private SubjectsService subjectsService = new SubjectsService();
    private List<Subjects> sublist = new ArrayList<Subjects>();
    private List<Options> optlist = new ArrayList<Options>();
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getSid() {
        return sid;
    }
    public void setSid(int sid) {
        this.sid = sid;
    }
    public Subjects getSubjects() {
        return subjects;
    }
    public void setSubjects(Subjects subjects) {
        this.subjects = subjects;
    }
    public List<Options> getOptlist() {
        return optlist;
    }
    public void setOptlist(List<Options> optlist) {
        this.optlist = optlist;
    }
    public List<Subjects> getSublist() {
        return sublist;
    }
    public void setSublist(List<Subjects> sublist) {
        this.sublist = sublist;
    }
    public String getOptContent() {
        return optContent;
    }
    public void setOptContent(String optContent) {
        this.optContent = optContent;
    }
    /**
     * 查询首页信息 subject
     * @return 跳转到votelist.jsp
     * @throws SQLException
     */
    public String findAll() throws SQLException {
        sublist = subjectsService.findAll();
        return "queryAll";
    }
    
    /**
     * 选择投票
     * @return 跳转到投票页面
     * @throws SQLException 
     */
    public String selectVote() throws SQLException {
        subjectsService.checkCount(id);
        optlist = OptionsService.findForOptionsBySid(id);
        subjects = subjectsService.findById(id);
        return "selectVote";
    }
    
    /**
     * 投票完成后执行
     * @return 跳转到finishVote对应的页面
     * @throws SQLException 
     * 问题:通过jsp传过来的optContent中存在“,”;经过各种尝试,没能实现去掉逗号
     *        只能采取optContent.substring(optContent.indexOf(",")+2)这种方法;
     * 
     */
    public String finishVote() throws SQLException {
        
        //用户正确操作,对该选项的投票次数加1
        optContent = optContent.substring(optContent.indexOf(",")+2);
        OptionsService.updateVoteNumber(optContent,sid);
        //给主题表subjects中相关主题的投票人数加1,然后响应到页面
        subjectsService.updateTotalVotes(sid);
        //再查询一次,核算投票百分比
        optlist = OptionsService.findForOptionsBySid(sid);
        subjects = subjectsService.findById(sid);
        return "finishVote";
        
    }
}

service(OptionsService和SubjectsService)

package com.tbamaw.service;

import java.sql.SQLException;
import java.util.List;

import com.tbamaw.dao.OptionsDao;
import com.tbamaw.domain.Options;

public class OptionsService {
    
    /**
     * 通过options中的sid查出options里面的内容
     * @param id = sid
     * @return 
     * @throws SQLException 
     */
    public static List<Options> findForOptionsBySid(int id) throws SQLException {
        return OptionsDao.findForOptionsBySid(id);
    }
    
    /**
     * 用户正确操作,对该选项的投票次数加1
     * @param optContent 选项内容
     * @param id = sid
     * @throws SQLException 
     */
    public static void updateVoteNumber(String optContent, int sid) throws SQLException {
        OptionsDao.updateVoteNumber(optContent,sid);
    }

}
package com.tbamaw.service;

import java.sql.SQLException;
import java.util.List;
import com.tbamaw.dao.SubjectsDao;
import com.tbamaw.domain.Subjects;

public class SubjectsService {
    
    private SubjectsDao subjectsDao = new SubjectsDao();
    
    /**
     * 查询首页信息
     * @return voteDao.findAll();
     * @throws SQLException
     */
    public List<Subjects> findAll() throws SQLException {
        return subjectsDao.findAll();
    }
    /**
     * 增加查看次数
     * @param id
     * @throws SQLException 
     */
    public void checkCount(int id) throws SQLException {
        subjectsDao.checkCount(id);
    }
    
    /**
     * 通过sub的id查询跳转过来的查看次数和投票数
     * @param id
     * @return
     * @throws SQLException 
     */
    public Subjects findById(int id) throws SQLException {
        return subjectsDao.findById(id);
    }
    
    /**
     * 给主题表subjects中相关主题的投票人数加1
     * @param id = sid
     * @return subjects
     * @throws SQLException 
     */
    public void updateTotalVotes(int id) throws SQLException {
        subjectsDao.updateTotalVotes(id);
    }    
}

dao(OptionsDao和SubjectsDao)

public class OptionsDao {

    /**
     * 通过options中的sid查出options里面的内容
     * @param id = sid
     * @return 
     * @throws SQLException 
     */
    public static List<Options> findForOptionsBySid(int id) throws SQLException {
        QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select * from options where sid =?";
        return qr.query(sql, new BeanListHandler<Options>(Options.class),id);
    }
    
    /**
     * 用户正确操作,对该选项的投票次数加1
     * @param optContent 选项内容
     * @param id = sid
     * @throws SQLException 
     */
    public static void updateVoteNumber(String optContent, int sid) throws SQLException {
        QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "update options set vote=(vote+1) where sid=? and optContent=?";
        qr.update(sql,sid,optContent);
        
    }

}
public class SubjectsDao {
    
    /**
     * 查询首页
     * @return
     * @throws SQLException
     */
    public List<Subjects> findAll() throws SQLException {
        QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select * from subjects";
        return qr.query(sql, new BeanListHandler<Subjects>(Subjects.class));
    }
    
    /**
     * 增加查看次数
     * @param id
     * @throws SQLException 
     */
    public void checkCount(int id) throws SQLException {
        QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "update subjects set viewTimes=(viewTimes+1) where id=?";
        qr.update(sql,id);
    }
    
    /**
     * 通过sub的id查询跳转过来的查看次数和投票数
     * @param id
     * @return
     * @throws SQLException 
     */
    public Subjects findById(int id) throws SQLException {
        QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select * from subjects where id = ?";
        return qr.query(sql, new BeanHandler<Subjects>(Subjects.class),id);
    }
    
    /**
     * 给主题表subjects中相关主题的投票人数加1
     * @param id = sid
     * @return subjects
     * @throws SQLException 
     */
    public void updateTotalVotes(int id) throws SQLException {
        QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "update subjects set totalVotes=(totalVotes+1) where id=?";
        qr.update(sql,id);
    }
    
}

domain

public class Options {
    private int id;//选项id
    private String optContent;//选项内容
    private int vote;//得票数
    private int sid;//主题id
    public Options() {}
    
    public Options(String optContent, int vote, int sid) {
        this.optContent = optContent;
        this.vote = vote;
        this.sid = sid;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getOptContent() {
        return optContent;
    }
    public void setOptContent(String optContent) {
        this.optContent = optContent;
    }
    public int getVote() {
        return vote;
    }
    public void setVote(int vote) {
        this.vote = vote;
    }
    public int getSid() {
        return sid;
    }
    public void setSid(int sid) {
        this.sid = sid;
    }    
}
public class Subjects {
    
    private int id;//主题id
    private String title;//主题名
    private int totalVotes;//投票人数
    private int viewTimes;//查看次数
    private Date createDate;//创建时间
    public Subjects() {}
    
    public Subjects(String title, int totalVotes, int viewTimes, Date createDate) {
        this.title = title;
        this.totalVotes = totalVotes;
        this.viewTimes = viewTimes;
        this.createDate = createDate;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public int getTotalVotes() {
        return totalVotes;
    }
    public void setTotalVotes(int totalVotes) {
        this.totalVotes = totalVotes;
    }
    public int getViewTimes() {
        return viewTimes;
    }
    public void setViewTimes(int viewTimes) {
        this.viewTimes = viewTimes;
    }
    public Date getCreateDate() {
        return createDate;
    }
    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }
}

结构图:

技术分享

 首页主题展示

技术分享

判断

技术分享

投票

技术分享

投票完成

技术分享

 

struts2和DBUtils结合做的一个投票系统练习

标签:针对   port   项目   页面   sel   dex   入口   script   1.4   

原文地址:http://www.cnblogs.com/bamaw/p/7258762.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!