數據庫采用Mysql
開源包使用JSTL+JFreeChart
項目結構如下
action
ChartServlet
VoteAction
VoteDeleteAction
VoteEditAction
VoteitemDeleteAction
VoteitemEditAction
VoteitemListAction
VoteitemSaveAction
VoteManageAction
VoteSaveAction
dao
IVoteDAO
VoteDAO
sql
script
model
vote
voteItem
Vote
Voteitems
util
DAOFactory
HibernateDAO
HibernateFilter
HibernateUtil
SelectConst
VoteFunction
ChartServlet
package cn
import java
import java
import javax
import javax
import javax
import javax
import org
import org
import org
import org
import org
import org
import cn
import cn
import cn
import cn
public class ChartServlet extends HttpServlet …{
/** *//**
* Constructor of the object
*/
public ChartServlet() …{
super()
}
/** *//**
* Destruction of the servlet
*/
public void destroy() …{
super
// Put your code here
}
public void doGet(HttpServletRequest request
throws ServletException
doPost( request
}
public void doPost(HttpServletRequest request
throws ServletException
String id=request
IVoteDAO votedao=DAOFactory
Vote vote=votedao
JFreeChart chart=getChart(vote)
response
ChartUtilities
}
public static JFreeChart getChart(Vote vote)…{
JFreeChart chart=null;
if(vote
DefaultPieDataset date=new DefaultPieDataset()
Iterator iter=vote
while(iter
Voteitems vi=(Voteitems)iter
date
}
if(
chart=ChartFactory
}else…{
chart=ChartFactory
}
}else…{
DefaultCategoryDataset date=new DefaultCategoryDataset()
Iterator it=vote
while(it
Voteitems vi=(Voteitems)it
date
}
if(
chart=ChartFactory
}
else…{
chart=ChartFactory
}
}
return chart;
}
public void init() throws ServletException …{
// Put your code here
}
}
VoteAction
/**//*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass
*/
package cn
import java
import javax
import javax
import org
import org
import org
import org
import cn
import cn
import cn
import cn
/** *//**
* MyEclipse Struts
* Creation date:
*
* XDoclet definition:
* @struts
* @struts
*/
public class VoteAction extends Action …{
/**//*
* Generated Methods
*/
/** *//**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping
HttpServletRequest request
String id=request
IVoteDAO voteDao=DAOFactory
Vote vote=voteDao
String[] voteitem=request
if(voteitem!=null&voteitem
for(int i=
Iterator iter=vote
while(iter
Voteitems vi=(Voteitems)iter
if(voteitem[i]
vi
break;
}
}
}
}
request
return mapping
}
} VoteDeleteAction
/**//*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass
*/
package cn
import javax
import javax
import org
import org
import org
import org
import cn
import cn
import cn
/** *//**
* MyEclipse Struts
* Creation date:
*
* XDoclet definition:
* @struts
* @struts
*/
public class VoteDeleteAction extends Action …{
/**//*
* Generated Methods
*/
/** *//**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping
HttpServletRequest request
IVoteDAO voteDao=DAOFactory
String id=request
Vote vote=voteDao
voteDao
request
return mapping
}
}
/**//*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass
*/
package cn
import javax
import javax
import org
import org
import org
import org
import cn
import cn
import cn
/** *//**
* MyEclipse Struts
* Creation date:
*
* XDoclet definition:
* @struts
* @struts
*/
public class VoteEditAction extends Action …{
/**//*
* Generated Methods
*/
/** *//**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping
HttpServletRequest request
IVoteDAO voteDao=DAOFactory
String id=request
Vote vote=voteDao
request
return mapping
}
}/**//*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass
*/
package cn
import javax
import javax
import org
import org
import org
import org
import cn
import cn
import cn
import cn
/** *//**
* MyEclipse Struts
* Creation date:
*
* XDoclet definition:
* @struts
* @struts
*/
public class VoteitemDeleteAction extends Action …{
/**//*
* Generated Methods
*/
/** *//**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping
HttpServletRequest request
IVoteDAO voteDao=DAOFactory
String id=request
String voteid=voteDao
Voteitems voteitem=voteDao
voteDao
Vote vote=voteDao
vote
request
return mapping
}
}/**//*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass
*/
package cn
import javax
import javax
import org
import org
import org
import org
import cn
import cn
import cn
import cn
/** *//**
* MyEclipse Struts
* Creation date:
*
* XDoclet definition:
* @struts
* @struts
*/
public class VoteitemEditAction extends Action …{
/**//*
* Generated Methods
*/
/** *//**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping
HttpServletRequest request
IVoteDAO voteDao=DAOFactory
String id=request
Voteitems viforup = voteDao
request
Vote vote = viforup
request
request
return mapping
}
}/**//*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass
*/
package cn
import javax
import javax
import org
import org
import org
import org
import cn
import cn
import cn
/** *//**
* MyEclipse Struts
* Creation date:
*
* XDoclet definition:
* @struts
* @struts
*/
public class VoteitemListAction extends Action …{
/**//*
* Generated Methods
*/
/** *//**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping
HttpServletRequest request
IVoteDAO voteDao=DAOFactory
String id=request
Vote vote=voteDao
request
return mapping
}
}/**//*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass
*/
package cn
import javax
import javax
import org
import org
import org
import org
import cn
import cn
import cn
/** *//**
* MyEclipse Struts
* Creation date:
*
* XDoclet definition:
* @struts
* @struts
*/
public class VoteitemSaveAction extends Action …{
/**//*
* Generated Methods
*/
/** *//**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping
HttpServletRequest request
IVoteDAO voteDao=DAOFactory
String voteid=request
String id=request
String title=request
String votenum=request
Voteitems voteitem=new Voteitems()
Vote vote=voteDao
if(id
voteitem
voteitem
voteitem
vote
voteDao
}else…{
voteitem=voteDao
voteitem
voteitem
voteitem
vote
voteDao
}
request
//request
return mapping
}
}/**//*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass
*/
package cn
import java
import javax
import javax
import org
import org
import org
import org
import cn
import cn
/** *//**
* MyEclipse Struts
* Creation date:
*
* XDoclet definition:
* @struts
*/
public class VoteManageAction extends Action …{
public ActionForward execute(ActionMapping mapping
HttpServletRequest request
IVoteDAO voteDao=DAOFactory
List votes=voteDao
request
return mapping
}
}/**//*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass
*/
package cn
import javax
import javax
import org
import org
import org
import org
import cn
import cn
import cn
/** *//**
* Creation date:
*
* XDoclet definition:
* @struts
* @struts
*/
public class VoteSaveAction extends Action …{
/**//*
* Generated Methods
*/
/** *//**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping
HttpServletRequest request
IVoteDAO voteDao=DAOFactory
String id=request
String name=request
String title=request
String votetype=request
String pictype=request
Vote vote=new Vote()
if(id
vote
vote
vote
vote
vote
voteDao
}
else
…{
vote=voteDao
vote
vote
vote
vote
voteDao
}
request
return mapping
}
}package cn
import java
import cn
import cn
public interface IVoteDAO …{
public void saveVote(Vote vote)
public void saveVoteItem(Voteitems voteitem)
public void updateVote(Vote vote)
public void updateVoteItem(Voteitems voteitem)
public void deleteVote(Vote vote)
public void deleteVoteItem(Voteitems voteitem)
public Vote getVote(String id)
public Vote getVoteByName(String name)
public List getAllVotes()
public Voteitems getVoteItem(String id)
}
package cn
import java
import cn
import cn
import cn
public class VoteDAO extends HibernateDAO implements IVoteDAO …{
public void deleteVote(Vote vote) …{
super
}
public void deleteVoteItem(Voteitems voteitem) …{
super
}
public List getAllVotes() …{
String hql=
return super
}
public Vote getVote(String id) …{
return (Vote)super
}
public Vote getVoteByName(String name) …{
String hql=
return (Vote)super
}
public Voteitems getVoteItem(String id) …{
return (Voteitems)super
}
public void saveVote(Vote vote) …{
super
}
public void saveVoteItem(Voteitems voteitem) …{
super
}
public void updateVote(Vote vote) …{
super
}
public void updateVoteItem(Voteitems voteitem) …{
super
}
}
<?xml version=
<!DOCTYPE hibernate
<hibernate
<class name=
<id name=
<generator class=
</id>
<property name=
</property>
<property name=
</property>
<property name=
</property>
<property name=
</property>
<set name=
<key column=
<one
</set>
</class>
</hibernate
<?xml version=
<!DOCTYPE hibernate
<hibernate
<class name=
<id name=
<generator class=
</id>
<property name=
</property>
<property name=
</property>
<many
</class>
</hibernate
package cn
import java
public class Vote …{
private String id;
private String name;
private String title;
private String votetype;
private String pictype;
private Set voteitems;
public String getId() …{
return id;
}
public void setId(String id) …{
this
}
public String getName() …{
return name;
}
public void setName(String name) …{
this
}
public String getPictype() …{
return pictype;
}
public void setPictype(String pictype) …{
this
}
public String getTitle() …{
return title;
}
public void setTitle(String title) …{
this
}
public Set getVoteitems() …{
return voteitems;
}
public void setVoteitems(Set voteitems) …{
this
}
public String getVotetype() …{
return votetype;
}
public void setVotetype(String votetype) …{
this
}
}
package cn
public class Voteitems …{
private String id;
private String title;
private Integer votenum;
private Vote vote;
public String getId() …{
return id;
}
public void setId(String id) …{
this
}
public String getTitle() …{
return title;
}
public void setTitle(String title) …{
this
}
public Vote getVote() …{
return vote;
}
public void setVote(Vote vote) …{
this
}
public Integer getVotenum() …{
return votenum;
}
public void setVotenum(Integer votenum) …{
this
}
public void increaseVotenum()…{
int num=getVotenum()
setVotenum(new Integer(num))
}
}
CREATE TABLE `vote` (
`id` varchar(
`name` varchar(
`title` varchar(
`votetype` varchar(
`pictype` varchar(
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gb
CREATE TABLE `voteitem` (
`id` varchar(
`title` varchar(
`votenum` varchar(
`vote_id` varchar(
PRIMARY KEY (`id`)
KEY `fk_vote_no` (`vote_id`)
) ENGINE=InnoDB DEFAULT CHARSET=gb
package cn
import cn
public class DAOFactory …{
public static IVoteDAO getVoteDAO()…{
return (IVoteDAO)getInstance(
}
static Object getInstance(String className)…{
try …{
Class cls=Class
return cls
} catch (Exception e) …{
e
return null;
}
}
}
package cn
import java
import org
public class HibernateDAO …{
public void saveObject(Object obj)…{
getSession()
}
public void updateObject(Object obj)…{
getSession()
}
public List getObjects(String hql)…{
List result=getSession()
return result;
}
public Object getObject(String hql)…{
Object result=getSession()
return result;
}
public Object getObject(Class cls
return getSession()
}
public void deleteObject(Object obj)…{
getSession()
}
protected Session getSession()…{
return HibernateUtil
}
}
package cn
import java
import javax
import javax
import javax
import javax
import javax
import javax
import javax
import mons
import mons
import org
import cn
/** *//**
* 用於進行Hibernate事務處理的Servlet過濾器
*
* @author galaxy
*/
public class HibernateFilter implements Filter …{
private static Log log = LogFactory
/** *//**
* 過濾器的主要方法
* 用於實現Hibernate事務的開始和提交
*/
public void doFilter(ServletRequest request
ServletResponse response
FilterChain chain)
throws IOException
…{
// 得到SessionFactory對象的實例
SessionFactory sf = HibernateUtil
try
…{
// 開始一個新的事務
log
sf
log
// 設置用戶請求的編碼格式
request
// Call the next filter (continue request processing)
chain
// 提交事務
log
sf
}
catch (Throwable ex)
…{
ex
try
…{
// 會滾事務
log
sf
}
catch (Throwable rbEx)
…{
log
}
// 拋出異常
throw new ServletException(ex)
}
}
/** *//**
* Servlet過濾器的初始化方法
* 可以讀取配置文件中設置的配置參數
*/
public void init(FilterConfig filterConfig) throws ServletException …{}
/** *//**
* Servlet的銷毀方法
* 用於釋放過濾器所申請的資源
*/
public void destroy() …{}
}package cn
import javax
import javax
import mons
import mons
import org
import org
import org
import org
/** *//**
* 基礎的Hibernate輔助類
* <p>
* 通過靜態的初始化代碼來讀取Hibernate啟動參數
* <tt>Configuration</tt>和<tt>SessionFactory</tt>對象
* <p>
*
* @author galaxy
*/
public class HibernateUtil
…{
private static Log log = LogFactory
// 指定定義******屬性名
private static final String INTERCEPTOR_CLASS =
// 靜態Configuration和SessionFactory對象的實例(全局唯一的)
private static Configuration configuration;
private static SessionFactory sessionFactory;
static
…{
// 從缺省的配置文件創建SessionFactory
try
…{
// 創建默認的Configuration對象的實例
// 如果你不使用JDK
// 來創建Configuration()對象的實例
configuration = new Configuration()
// 讀取hibernate
nfigure()
// 如果在配置文件中配置了******
String interceptorName = configuration
if (interceptorName != null)
…{
Class interceptorClass =
HibernateUtil
Interceptor interceptor = (Interceptor)interceptorClass
configuration
}
if (configuration
…{
// 讓Hibernate將SessionFacory綁定到JNDI
configuration
}
else
…{
// 使用靜態變量來保持SessioFactory對象的實例
sessionFactory = configuration
}
}
catch (Throwable ex)
…{
// 輸出異常信息
log
ex
throw new ExceptionInInitializerError(ex)
}
}
/** *//**
* 返回原始的Configuration對象的實例
*
* @return Configuration
*/
public static Configuration getConfiguration()
…{
return configuration;
}
/** *//**
* 返回全局的SessionFactory對象的實例
*
* @return SessionFactory
*/
public static SessionFactory getSessionFactory()
…{
SessionFactory sf = null;
String sfName = configuration
if ( sfName != null)
…{
log
try
…{
sf = (SessionFactory) new InitialContext()
}
catch (NamingException ex)
…{
throw new RuntimeException(ex)
}
}
else
…{
sf = sessionFactory;
}
if (sf == null)
throw new IllegalStateException(
return sf;
}
/** *//**
* 關閉當前的SessionFactory並且釋放所有的資源
*/
public static void shutdown()
…{
log
// Close caches and connection pools
getSessionFactory()
// Clear static variables
configuration = null;
sessionFactory = null;
}
/** *//**
* 使用靜態的Configuration對象來重新構建SessionFactory
*/
public static void rebuildSessionFactory()
…{
log
rebuildSessionFactory(configuration)
}
/** *//**
* 使用指定的Configuration對象來重新構建SessionFactory對象
*
* @param cfg
*/
public static void rebuildSessionFactory(Configuration cfg)
…{
log
synchronized(sessionFactory)
…{
if (sessionFactory != null && !sessionFactory
sessionFactory
if (cfg
cfg
else
sessionFactory = cfg
configuration = cfg;
}
}
/** *//**
* 在當前SessionFactory中注冊一個******
*/
public static SessionFactory registerInterceptorAndRebuild(Interceptor interceptor)
…{
log
configuration
rebuildSessionFactory()
return getSessionFactory()
}
/** *//**
* 獲得******對象
*
* @return Interceptor
*/
public static Interceptor getInterceptor()
…{
return configuration
}
/** *//**
* 提交當前事務
*/
public static void commitAndBeginTransaction()
…{
sessionFactory
sessionFactory
}
}
package cn
import java
import java
public class SelectConst …{
public static final HashMap votetypes;
public static final HashMap pictypes;
static…{
votetypes=new HashMap()
votetypes
votetypes
pictypes=new HashMap()
pictypes
pictypes
pictypes
pictypes
}
public static String getVoteTypeOptions(String defaultValue)…{
return getOptions(votetypes
}
public static String getVoteTypeTitle(String key)…{
return (String)votetypes
}
public static String getPicTypeoptions(String defaultValue)…{
return getOptions(pictypes
}
public static String getPicTypeTitle(String key)…{
return (String)pictypes
}
public static String getOptions(HashMap options
StringBuffer sf=new StringBuffer()
Iterator keys=options
while(keys
String key=(String)keys
sf
sf
if(key
sf
}else…{
sf
}
sf
sf
}
return sf
}
}
package cn
import java
import cn
import cn
import cn
public class VoteFunction …{
public static String votetype(String votetype)…{
return SelectConst
}
public static String votetypeoptions(String defaultValue)…{
return SelectConst
}
public static String pictype(String pictype)…{
return SelectConst
}
public static String pictypeoptions(String defaultValue)…{
return SelectConst
}
public static String display(String votename)…{
StringBuffer buf=new StringBuffer()
buf
buf
IVoteDAO voteDao=DAOFactory
Vote vote=voteDao
if(vote!=null)…{
buf
Iterator iter=vote
int i=
while(iter
Voteitems vi=(Voteitems)iter
String control=
+vote
+
+vote
+vi
buf
}
}
buf
buf
buf
buf
buf
buf
buf
buf
return buf
}
public static String line(String line)…{
return
}
}
<?xml version=
<!DOCTYPE hibernate
<!
<hibernate
<session
<property name=
<property name=
jdbc:mysql://localhost:
</property>
<property name=
<property name=
<property name=
com
</property>
<property name=
org
</property>
<property name=
<mapping resource=
<mapping resource=
</session
</hibernate
index
<%…@page contentType=
<%…@ taglib prefix=
<html>
<head>
<title>投票系統</title>
</head>
<body>
<center>
<table>
<tr>
<td>
<img src=
</td>
<td>
<h
${vote:display(
</td>
</tr>
</table>
</center>
</body>
</html> ote_add_update
<%…@page contentType=
<%…@ taglib prefix=
<html>
<head>
<%…
String context=request
%>
<meta http
<title>發布投票</title>
<script type=
<script type=
function checkValid(form)…{
var checks=[[form
if(!isRequired(checks))…{
return false;
}
return true;
}
function gomanager()…{
window
}
</script>
</head>
<center>
<h
<p>
<table>
<form method=
<tr>
<td width=
<td><input type=
</tr>
<tr>
<td>投票標識</td>
<td><input type=
</tr>
<tr>
<td>投票類型</td>
<td>
<select name=
${vote:votetypeoptions(vote
</select>
</td>
</tr>
<tr>
<td>圖形類型</td>
<td>
<select name=
${vote:pictypeoptions(vote
</select>
</td>
</tr>
<tr>
<td colspan=
<input type=
<input type=
<input type=
<input type=
</td>
</tr>
</form>
</table>
<p>
<img src=
</center>
</html> vote_list
<%…@ taglib uri=
<%…@ taglib uri=
<%…@page contentType=
<html>
<head>
<%…
String context=request
%>
<title>投票管理系統</title>
</head>
<body>
<center>
<h
<table border=
<tr>
<td colspan=
</tr>
<tr>
<td align=
<td align=
<td align=
<td align=
<td align=
</tr>
<c:forEach var=
<tr>
<td>${vote
<td>${vote
<td>${vote:votetype(vote
<td>${vote:pictype(vote
<td align=
<a href=
<a href=
<a href=
</td>
</tr>
</c:forEach>
</table>
<p>
<a href=
<p>
<img src=
</center>
</body>
</html> vote_result
<%…@page contentType=
<%…@ taglib prefix=
<%…@ taglib prefix=
<html>
<head>
<title>投票結果</title>
</head>
<body>
<center>
<h
<h
<img src=
<table width=
<tr>
<td align=
<td align=
</tr>
<c:forEach var=
<tr>
<td>${voteitem
<td>${voteitem
</tr>
</c:forEach>
<tr>
<td colspan=
<a href=
</td>
</tr>
</table>
</center>
</body>
</html>
voteitem_add_update
<%…@ taglib prefix=
<%…@ taglib prefix=
<%…@page contentType=
<%…@page isELIgnored=
<html>
<head>
<%…
String context=request
%>
<script type=
<script type=
function checkValid(form)…{
var checks=[[form
if(isRequired(checks))…{
return true;
}
return false;
}
function gomanager()…{
window
}
</script>
<title>維護投票條目</title>
</head>
<body>
<center>
<h
<table>
<tr>
<td><font color=
<td valign=
<img src=
</td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td align=
</tr>
<tr>
<td>投票標識
</tr>
<tr>
<td>投票名稱
</tr>
<tr>
<td>投票類型
</tr>
<tr>
<td>圖形類型
</tr>
<tr>
<td> </td>
</tr>
<c:if test=
<tr>
<td align=
</tr>
<tr>
<td>
<table width=
<tr>
<td align=
<td align=
<td align=
</tr>
<c:forEach var=
<tr>
<td>${voteitem
<td>${voteitem
<td align=
<a href=
<a href=
</td>
</tr>
</c:forEach>
</table>
</td>
</tr>
<tr>
<td> </td>
</tr>
</c:if>
<tr>
<td>請輸入投票選項的信息
</tr>
<tr>
<td>
<table width=
<form method=
<tr>
<td>選項
<td><input type=
</tr>
<tr>
<td>票數
<td><input type=
</tr>
<tr>
<td colspan=
<input type=
<input type=
<input type=
<input type=
<input type=
</td>
</tr>
</form>
</table>
</td>
</tr>
</table>
</center>
</body>
</html>
common
function isRequired( checked )
…{
for( var i=
…{
var input = checked[i][
if( input
…{
alert( checked[i][
input
return false;
}
}
return true;
}
struts
<?xml version=
<!DOCTYPE struts
<struts
<data
<form
<global
<global
</global
<action
<action path=
<forward name=
</action>
<action path=
<forward name=
</action>
<action path=
<forward name=
</action>
<action path=
<forward name=
</action>
<action path=
<forward name=
</action>
<action path=
<forward name=
</action>
<action path=
<forward name=
</action>
<action path=
<forward name=
</action>
<action path=
<forward name=
</action>
</action
<message
</struts
w
<?xml version=
<web
<filter>
<filter
<filter
</filter>
<filter
<filter
<url
</filter
<servlet>
<servlet
<servlet
<init
<param
<param
</init
<init
<param
<param
</init
<init
<param
<param
</init
<load
</servlet>
<servlet
<servlet
<url
</servlet
<servlet>
<servlet
<servlet
</servlet>
<servlet
<servlet
<url
</servlet
<jsp
<taglib>
<taglib
<taglib
</taglib>
</jsp
</web
vote
<?xml version=
<taglib xmlns=
xmlns:xsi=
xsi:schemaLocation=
version=
<description>vote function library</description>
<display
<tlib
<short
<uri>;/uri>
<function>
<name>votetype</name>
<function
<function
</function>
<function>
<name>pictype</name>
<function
<function
</function>
<function>
<name>votetypeoptions</name>
<function
<function
</function>
<function>
<name>pictypeoptions</name>
<function
<function
</function>
<function>
<name>display</name>
<function
<function
</function>
</taglib>
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28698.html