java 动漫论坛
可以实现的功能和正常论坛功能一致,有游客、用户、管理员登录,可进行帖子管理
实现效果
发布新帖子:
分区管理:
管理员登录:
回复帖子:
浏览界面:
代码实现
package com.action;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.struts2.ServletActionContext;
import com.dao.TForumsDAO;
import com.model.TForums;
import com.opensymphony.xwork2.ActionSupport;
public class forumsAction extends ActionSupport {
/**
* 分区
*/
private static String typeGroup = "group";
/**
* 版块
*/
private static String typeForum= "forum";
/**
* 子版块
*/
private static String typeSub = "sub";
private int fid;
private String type;
private String name;
private int fup;
private String message;
private String path;
private TForumsDAO forumsDAO;
public int getFid() {
return fid;
}
public void setFid(int fid) {
this.fid = fid;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getFup() {
return fup;
}
public void setFup(int fup) {
this.fup = fup;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public TForumsDAO getForumsDAO() {
return forumsDAO;
}
public void setForumsDAO(TForumsDAO forumsDAO) {
this.forumsDAO = forumsDAO;
}
public String forumsMana(){
String txt = "";
if(typeForum.equals(type)){
//添加版块
txt = "版块管理";
}else if(typeSub.equals(type)){
txt = "子版块管理";
}else{
txt = "分区管理";
}
List forumsList = getForumsByType(type);
Map request=(Map)ServletActionContext.getContext().get("request");
request.put("forumsList", forumsList);
request.put("txt", txt);
return ActionSupport.SUCCESS;
}
public String toAdd(){
String txt = "";
String td = "";
List forumsList = new ArrayList();
if(typeForum.equals(type)){
//添加版块
forumsList = getForumsByType(typeGroup);//获取分区
txt = "添加版块";
td = "上级分区";
}else if(typeSub.equals(type)){
forumsList = getForumsByType(typeForum);//获取版块
txt = "添加子版块";
td = "上级版块";
}else{
txt = "添加分区";
}
Map request=(Map)ServletActionContext.getContext().get("request");
request.put("type", type);
request.put("forumsList", forumsList);
request.put("txt", txt);
request.put("td", td);
return "toAdd";
}
public String doAdd(){
TForums forums = new TForums();
forums.setType(type);
forums.setName(name);
if(typeGroup.equals(type)){
forums.setFup(0);
}else{
forums.setFup(fup);
}
forums.setStatus(new Short("1"));
forumsDAO.save(forums);
this.setMessage("操作成功");
this.setPath("forumsMana.action?type="+type);
return "succeed";
}
public String toEdit(){
TForums forums = forumsDAO.findById(fid);
String txt = "";
String td = "";
List forumsList = new ArrayList();
if(typeForum.equals(forums.getType())){
//添加版块
forumsList = getForumsByType(typeGroup);//获取分区
txt = "编辑版块";
td = "上级分区";
}else if(typeSub.equals(forums.getType())){
forumsList = getForumsByType(typeForum);//获取版块
txt = "编辑子版块";
td = "上级版块";
}else{
txt = "编辑分区";
}
Map request=(Map)ServletActionContext.getContext().get("request");
request.put("forumInfo", forums);
request.put("forumsList", forumsList);
request.put("txt", txt);
request.put("td", td);
return "toEdit";
}
public String doEdit(){
TForums forums = forumsDAO.findById(fid);
forums.setType(type);
forums.setName(name);
if(typeGroup.equals(type)){
forums.setFup(0);
}else{
forums.setFup(fup);
}
forumsDAO.getHibernateTemplate().update(forums);
this.setMessage("操作成功");
this.setPath("forumsMana.action?type="+type);
return "succeed";
}
public String doDel(){
TForums forums = forumsDAO.findById(fid);
forums.setStatus(new Short("0"));
forumsDAO.getHibernateTemplate().update(forums);
this.setMessage("操作成功");
this.setPath("forumsMana.action?type="+forums.getType());
return "succeed";
}
private List getForumsByType(String type){
return forumsDAO.getHibernateTemplate().find("from TForums where type='"+type+"' and status=1");
}
}
package com.action;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;
import com.dao.TForumsDAO;
import com.model.TForums;
import com.model.TTopic;
import com.opensymphony.xwork2.ActionSupport;
public class indexAction {
private TForumsDAO forumsDAO;
public String index()
{
Map request=(Map)ServletActionContext.getContext().get("request");
request.put("forumsList", getForums());
return ActionSupport.SUCCESS;
}
private List getForums(){
List groups = forumsDAO.getHibernateTemplate().find("from TForums where type='group' and status=1");
Session session = forumsDAO.getSessionFactory().openSession();
for (Object object : groups) {
TForums group = (TForums)object;
List forums = forumsDAO.getHibernateTemplate().find("from TForums where fup="+group.getFid()+" and status=1");
for (Object object2 : forums) {
TForums forum = (TForums)object2;
List subs = forumsDAO.getHibernateTemplate().find("from TForums where fup="+forum.getFid()+" and status=1");
String getTopicList = "select ta.pid,ta.author,ta.subject,ta.addtime from t_topic ta " +
"left join t_forums tb on ta.fid=tb.fid where tb.fup='"+forum.getFid()+"' order by addtime desc limit 1";
List topicList = session.createSQLQuery(getTopicList).list();
if(topicList!=null && topicList.size()>0){
Object[] objTopic = (Object[])topicList.get(0);
Date addtime = (Date)objTopic[3];
TTopic topic = new TTopic();
topic.setPid((Integer)objTopic[0]);
topic.setAuthor((String)objTopic[1]);
topic.setSubject((String)objTopic[2]);
topic.setAddtime(addtime);
List pList = new ArrayList();
pList.add(topic);
forum.setTopicList(pList);
if(newTopic(addtime))
forum.setStyle("class=new");
else
forum.setStyle("");
}else{
forum.setStyle("");
}
//获取主题数题
String getTopicNum = "select count(1) from t_topic ta left join t_forums tb on ta.fid=tb.fid where tb.fup='"+forum.getFid()+"'";
String getThreadNum = "select count(1) from t_threads ta left join t_forums tb on ta.fid=tb.fid where tb.fup='"+forum.getFid()+"'";
forum.setChild(subs);
forum.setTopicNum(((BigInteger)session.createSQLQuery(getTopicNum).list().get(0)).intValue());
forum.setThreadNum(((BigInteger)session.createSQLQuery(getThreadNum).list().get(0)).intValue());
}
group.setChild(forums);
}
session.close();
return groups;
}
/**
* 判断是否有新主题
* @return
*/
private boolean newTopic(Date addtime){
boolean result = false;
Calendar calendar = Calendar.getInstance();
calendar.setTime(addtime);
long timeadd = calendar.getTimeInMillis();
calendar.setTime(new Date());
long timethis = calendar.getTimeInMillis();
long theday = (timethis - timeadd) / (1000 * 60 * 60 * 24);
if(theday<7)
result = true;
return result;
}
public TForumsDAO getForumsDAO() {
return forumsDAO;
}
public void setForumsDAO(TForumsDAO forumsDAO) {
this.forumsDAO = forumsDAO;
}
}
package com.action;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;
import com.dao.TForumsDAO;
import com.dao.TThreadsDAO;
import com.dao.TTopicDAO;
import com.model.TForums;
import com.model.TThreads;
import com.model.TTopic;
import com.model.TUser;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class threadAction extends ActionSupport{
private TForumsDAO forumsDAO;
private TTopicDAO topicDAO;
private TThreadsDAO threadsDAO;
private int pid;
private int fid;
private String subject;
private String content;
private String message;
private String path;
public String toAddThread(){
TForums forums = forumsDAO.findById(fid);
Map request=(Map)ServletActionContext.getContext().get("request");
request.put("forums", forums);
return ActionSupport.SUCCESS;
}
public String threadAdd()
{
Map session= ActionContext.getContext().getSession();
TUser user = (TUser)session.get("user");
Date date = new Date();
TTopic topic = new TTopic();
topic.setFid(fid);
topic.setAuthor(user.getUserName());
topic.setAuthorid(user.getId());
topic.setSubject(subject);
topic.setView(0);
topic.setReplies(0);
topic.setAddtime(date);
topicDAO.save(topic);
int pid = topic.getPid();
TThreads threads = new TThreads();
threads.setPid(pid);
threads.setFid(fid);
threads.setFtype(0);
threads.setAuthor(user.getUserName());
threads.setAuthorid(user.getId());
threads.setSubject(subject);
threads.setContent(content);
threads.setAddtime(date);
threadsDAO.save(threads);
this.setMessage("主题发布成功");
this.setPath("threadview.action?pid="+pid);
return "succeed";
}
public String threadview()
{
String sql="update TTopic set view=view+1 where pid="+pid;
topicDAO.getHibernateTemplate().bulkUpdate(sql);
TTopic topic = topicDAO.findById(pid);
TForums forums = forumsDAO.findById(topic.getFid());
Session session = threadsDAO.getSessionFactory().openSession();
String getMain = "select tid,author,subject,content,addtime from t_threads where pid="+pid+" order by addtime limit 1";
Object[] objThreadsMain = (Object[])session.createSQLQuery(getMain).list().get(0);
TThreads threadsMain = new TThreads();
threadsMain.setTid((Integer)objThreadsMain[0]);
threadsMain.setAuthor((String)objThreadsMain[1]);
threadsMain.setSubject((String)objThreadsMain[2]);
threadsMain.setContent((String)objThreadsMain[3]);
threadsMain.setAddtime((Date)objThreadsMain[4]);
List threads = threadsDAO.getHibernateTemplate().find("from TThreads where pid="+pid+" and ftype=1 order by addtime");
Map request=(Map)ServletActionContext.getContext().get("request");
request.put("topic", topic);
request.put("forums", forums);
request.put("threadsMain", threadsMain);
request.put("threadsList", threads);
session.close();
return SUCCESS;
}
public String replace()
{
Map session= ActionContext.getContext().getSession();
TUser user = (TUser)session.get("user");
String sql="update TTopic set replies=replies+1 where pid="+pid;
topicDAO.getHibernateTemplate().bulkUpdate(sql);
TThreads threads = new TThreads();
threads.setPid(pid);
threads.setFid(fid);
threads.setFtype(1);
threads.setAuthor(user.getUserName());
threads.setAuthorid(user.getId());
threads.setSubject(subject);
threads.setContent(content);
threads.setAddtime(new Date());
threadsDAO.save(threads);
this.setMessage("主题回复成功");
this.setPath("threadview.action?pid="+pid);
return "succeed";
}
public TTopicDAO getTopicDAO() {
return topicDAO;
}
public void setTopicDAO(TTopicDAO topicDAO) {
this.topicDAO = topicDAO;
}
public TThreadsDAO getThreadsDAO() {
return threadsDAO;
}
public void setThreadsDAO(TThreadsDAO threadsDAO) {
this.threadsDAO = threadsDAO;
}
public TForumsDAO getForumsDAO() {
return forumsDAO;
}
public void setForumsDAO(TForumsDAO forumsDAO) {
this.forumsDAO = forumsDAO;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public int getFid() {
return fid;
}
public void setFid(int fid) {
this.fid = fid;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
package com.action;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;
import com.dao.TForumsDAO;
import com.dao.TThreadsDAO;
import com.dao.TTopicDAO;
import com.model.TForums;
import com.model.TThreads;
import com.model.TTopic;
import com.opensymphony.xwork2.ActionSupport;
public class topicAction extends ActionSupport{
private TForumsDAO forumsDAO;
private TTopicDAO topicDAO;
private TThreadsDAO threadsDAO;
private int fid;
private int pid;
private String message;
private String path;
public String topicMana(){
List topicList = new ArrayList();
String sql = "select ta.pid,author,name,`subject`,view,replies,addtime from t_topic ta left join t_forums tb on ta.fid=tb.fid";
Session session = topicDAO.getSessionFactory().openSession();
List objList = session.createSQLQuery(sql).list();
for (Object object : objList) {
Object[] objTopic = (Object[])object;
TTopic topic = new TTopic();
topic.setPid((Integer)objTopic[0]);
topic.setAuthor((String)objTopic[1]);
topic.setFname((String)objTopic[2]);
topic.setSubject((String)objTopic[3]);
topic.setView((Integer)objTopic[4]);
topic.setReplies((Integer)objTopic[5]);
topic.setAddtime((Date)objTopic[6]);
topicList.add(topic);
}
Map request=(Map)ServletActionContext.getContext().get("request");
request.put("topicList", topicList);
return SUCCESS;
}
public String topicDel(){
String delSql = "delete from TTopic where pid="+pid;
topicDAO.getHibernateTemplate().bulkUpdate(delSql);
delSql = "delete from TThreads where pid="+pid;
topicDAO.getHibernateTemplate().bulkUpdate(delSql);
this.setMessage("操作成功");
this.setPath("topicMana.action");
return "succeed";
}
public String topicview(){
List topicList = topicDAO.getHibernateTemplate().find("from TTopic where fid="+fid +"order by addtime desc");
for (Object object : topicList) {
TTopic topic = (TTopic)object;
topic.setStyle("folder_common");
String sql = "from TThreads where pid="+topic.getPid()+" order by addtime desc limit 1";
TThreads lastSub = (TThreads)threadsDAO.getHibernateTemplate().find(sql).get(0);
if(topic.getReplies()>0){
//判断是否是最新回复
if(newSub(lastSub.getAddtime())){
topic.setStyle("folder_new");
}
}
topic.setLastsub(lastSub);
}
TForums forums = forumsDAO.findById(fid);
Map request=(Map)ServletActionContext.getContext().get("request");
request.put("topicList", topicList);
request.put("forums", forums);
return ActionSupport.SUCCESS;
}
/**
* 判断是否有新主题
* @return
*/
private boolean newSub(Date addtime){
boolean result = false;
Calendar calendar = Calendar.getInstance();
calendar.setTime(addtime);
long timeadd = calendar.getTimeInMillis();
calendar.setTime(new Date());
long timethis = calendar.getTimeInMillis();
long theday = (timethis - timeadd) / (1000 * 60 * 60 * 24);
if(theday<7)
result = true;
return result;
}
public TForumsDAO getForumsDAO() {
return forumsDAO;
}
public void setForumsDAO(TForumsDAO forumsDAO) {
this.forumsDAO = forumsDAO;
}
public TTopicDAO getTopicDAO() {
return topicDAO;
}
public void setTopicDAO(TTopicDAO topicDAO) {
this.topicDAO = topicDAO;
}
public TThreadsDAO getThreadsDAO() {
return threadsDAO;
}
public void setThreadsDAO(TThreadsDAO threadsDAO) {
this.threadsDAO = threadsDAO;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public int getFid() {
return fid;
}
public void setFid(int fid) {
this.fid = fid;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
写在最后
由于代码太多了,贴不完整,需要完整可以v我:Code2Life2