说明:
1.用到了Struts2和hibernate没有用到spring综合去管理项目
2.tomcat中的端口为8080没有修改过的,你那边也默认为8080,然后不用改了,部署好了之后直接浏览器中输入http://localhost:8080/Struts2Demo/login
3.所有的要自己新建的配置文件(比如:struts.xml,hibernate.cfg.xml,log4j.properties)都只需要在选中src文件夹 ,然后新建相应的文件就行了
4.关于log4j--其实这个你想要的效果可以去配置这个log4j.properties文件(就是日志的格式,或者日志存储的目的地等),我随便配的,效果应该不是你想要的
5.没有分页,注销没有写(注销的话把session删了,然后跳回登录页面就行了)
6.前台没有css,前段框架之类的,如果你会前段框架可以把一些按钮,表单之类的美化下(可以用bootstraps,这个只要引入js,css文件就可以用了)
7.还有就是回复应该是写了,但是没有测试(就是说方法有了,还没测试)
项目结构
具体的代码:
package com.hikvision.login --这个包下面的
action
LoginAciton.java
package com.hikvision.login.action; import java.util.ArrayList; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.log4j.Logger; import org.apache.struts2.ServletActionContext; import org.hibernate.Session; import tutorial.TestLog; import com.hikvision.login.dao.UserDao; import com.hikvision.login.model.User; import com.hikvision.login.service.UserService; import com.hikvision.login.service.UserServiceImpl; import com.hikvision.poster.model.Poster; import com.hikvision.poster.service.PosterService; import com.hikvision.poster.service.PosterServiceImpl; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport{ private String code; private User user; private Poster poster; private String userid;//用于传递登录人的id private String flaginfo;//对用户操作的标识:新建还是修改 private String postidinfo;//选中帖子的id private List list = new ArrayList();//后台查到所有的User的对象 private List listP;//后台查到所有的Poster的对象 private UserService userService = new UserServiceImpl(); private PosterService posterService = new PosterServiceImpl(); //HttpServletRequest request = ServletActionContext.getRequest(); private static Logger log = Logger.getLogger(LoginAction.class); //ActionContext cxt = ActionContext.getContext(); //HttpServletRequest request = (HttpServletRequest)cxt.get(ServletActionContext.HTTP_REQUEST); HttpServletRequest request=ServletActionContext.getRequest(); public String login(){ return SUCCESS; } public String logincheck(){ User user1 = new User(); String result = ""; user1 = userService.getUser(user.getUsername()); if(user1!=null&&"admin123456+".equals(user1.getUserpassword())){//管理员登录 list = userService.getAllObject();//返回所有的用户 result= SUCCESS; }else if(user1!=null&&user.getUserpassword().equals(user1.getUserpassword())){//普通用户登录--用户名密码对 userid = user1.getId().toString(); request.setAttribute("userid", userid); user.setId(user1.getId());//这个地方不设置的话,下面的sessin中的id是为空的 //设置session request.getSession().setAttribute("user",user); request.getSession().setAttribute("userid",userid); listP = posterService.getAllPosters();//显示所有的帖子 return SUCCESS; }else{//普通用户登录--用户名密码错 code="1001"; return "error";//用户名或密码错误 } return result; } public String loginback(){ if("1001".equals(code)){ return "success"; } return "false"; } //测试与hibernate连接 public String testHibernate(){ userService.getObject(); return SUCCESS; } //跳转到adduser.jsp public String toHome(){ if("NOADMINedit".equals(flaginfo)){ user=userService.findUserById(userid); request.setAttribute("user", user); } return SUCCESS; } //添加用户addUser public String addUser(){ if("admin".equals(user.getUsername())){//管理员 }else if(!"admin".equals(user.getUsername())){//普通用户 if("NOADMINedit".equals(flaginfo)){ User user2= new User(); user2.setId(user.getId()); user2.setUsername(user.getUsername()); user2.setUserpassword(user.getUserpassword()); userService.editUser(user2); userid = user2.getId().toString(); request.setAttribute("userid", userid); }else{ userService.addUser(user); } } return SUCCESS; } //setter , getter public User getUser() { return user; } public void setUser(User user) { this.user = user; } public List getList() { return list; } public void setList(List list) { this.list = list; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getUserid() { return userid; } public void setUserid(String userid) { this.userid = userid; } public String getFlaginfo() { return flaginfo; } public void setFlaginfo(String flaginfo) { this.flaginfo = flaginfo; } public List getListP() { return listP; } public void setListP(List listP) { this.listP = listP; } public Poster getPoster() { return poster; } public void setPoster(Poster poster) { this.poster = poster; } public String getPostidinfo() { return postidinfo; } public void setPostidinfo(String postidinfo) { this.postidinfo = postidinfo; } }
dao
BaseDao.java
package com.hikvision.login.dao;
import java.util.List;
public interface BaseDao {
public List getAllObjects(Class entityClass);
}
UserDao.java
package com.hikvision.login.dao;
import java.util.List;
import com.hikvision.login.model.User;
public interface UserDao extends BaseDao {
public User login(String name,String password);
public User findById(Integer id);
public List findAdminPage(int nowPage,int pageSize);
public List getAll();
//添加用户
public void saveUser(User user2);
//获得所有用户
public List getAllObject();
public User getUser(String name);
public User getUserById(String userid);
public void editUser(User user);
}
UserDaoImpl.java
package com.hikvision.login.dao; import java.util.Iterator; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaExport; import com.hikvision.login.model.User; //实现接口A,不仅要实现接口A中的方法,而且也要实现接口A所集成的接口B的方法 public class UserDaoImpl implements UserDao{ //默认读取hibernate.cfg.xml文件 Configuration cfr = new Configuration().configure(); //SchemaExport export = new SchemaExport(cfr); //export.create(true, true); SessionFactory factory = cfr.buildSessionFactory(); Session session = null; public List findAdminPage(int nowPage, int pageSize) { // TODO Auto-generated method stub return null; } //org.hibernate.cfg.Configuration; public User findById(Integer id) { // TODO Auto-generated method stub return null; } public User login(String name, String password) { // TODO Auto-generated method stub return null; } public List getAllObjects(Class entityClass) { // TODO Auto-generated method stub return null; } public List getAll() { try{ session = factory.openSession(); //开启事务 session.beginTransaction(); User user = new User(); user.setUsername("tt"); user.setUserpassword("111111"); session.save(user); //提交事务 session.getTransaction().commit(); }catch(Exception e){ e.printStackTrace(); //回滚事务 session.getTransaction().rollback(); } finally{ if(session != null){ if(session.isOpen()){ //关闭session session.close(); } } } return null; } //添加用户 public void saveUser(User user2) { try{ session = factory.openSession(); //开启事务 session.beginTransaction(); User user = new User(); user.setUsername(user2.getUsername()); user.setUserpassword(user2.getUserpassword()); session.save(user); //提交事务 session.getTransaction().commit(); }catch(Exception e){ e.printStackTrace(); //回滚事务 session.getTransaction().rollback(); } finally{ if(session != null){ if(session.isOpen()){ //关闭session session.close(); } } } } //修改用户 public void editUser(User user) { try{ session = factory.openSession(); //开启事务 session.beginTransaction(); session.saveOrUpdate(user); //提交事务 session.getTransaction().commit(); }catch(Exception e){ e.printStackTrace(); //回滚事务 session.getTransaction().rollback(); } finally{ if(session != null){ if(session.isOpen()){ //关闭session session.close(); } } } } public List getAllObject() { List userList = null; try{ session = factory.openSession(); //开启事务 session.beginTransaction(); Query query = session.createQuery("from User"); userList = query.list(); for(Iterator it = userList.iterator();it.hasNext();){ User user = it.next(); System.out.println(user.getId()+":"+user.getUsername()+":"+user.getUserpassword()); } }catch(Exception e){ e.printStackTrace(); //回滚事务 session.getTransaction().rollback(); } finally{ if(session != null){ if(session.isOpen()){ //关闭session session.close(); } } } return userList; } //根据用户名去查找用户 public User getUser(String name) { List userList = null; User user = null; try{ session = factory.openSession(); //开启事务 session.beginTransaction(); Query query = session.createQuery(" from User t where t.username = ? "); query.setString(0, name); userList = query.list(); if(userList!=null){ user = (User) userList.get(0); } }catch(Exception e){ e.printStackTrace(); //回滚事务 session.getTransaction().rollback(); } finally{ if(session != null){ if(session.isOpen()){ //关闭session session.close(); } } } return user; } //根据id查询用户 public User getUserById(String userid) { List userList = null; User user = null; try{ session = factory.openSession(); //开启事务 session.beginTransaction(); Query query = session.createQuery(" from User t where t.id = ? "); query.setString(0, userid); userList = query.list(); if(userList!=null){ user = (User) userList.get(0); } }catch(Exception e){ e.printStackTrace(); //回滚事务 session.getTransaction().rollback(); } finally{ if(session != null){ if(session.isOpen()){ //关闭session session.close(); } } } return user; } }
model
User.java
package com.hikvision.login.model; public class User implements java.io.Serializable{ private Integer id; private String username; private String userpassword; private static final long serialVersionUID = 1L; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getUserpassword() { return userpassword; } public void setUserpassword(String userpassword) { this.userpassword = userpassword; } }
User.hbm.xml
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
Service
UserService.java
package com.hikvision.login.service; import java.util.List; import com.hikvision.login.model.User; public interface UserService { //public static final User user = null; //这个用于hibernate的测试 public List getObject(); //获得数据库中所有的user public List getAllObject(); public void addUser(User user2); public User getUser(String name); public User findUserById(String userid); public void editUser(User user); }
UserServiceImpl.java
package com.hikvision.login.service; import java.util.List; import com.hikvision.login.dao.UserDao; import com.hikvision.login.dao.UserDaoImpl; import com.hikvision.login.model.User; public class UserServiceImpl implements UserService{ private UserDao userDao = new UserDaoImpl(); List list = null; private User user; //这个用于hibernate的测试 public List getObject() { list = userDao.getAll(); return list; } //添加用户 public void addUser(User user2) { // TODO Auto-generated method stub userDao.saveUser(user2); } //修改用户 public void editUser(User user) { // TODO Auto-generated method stub userDao.editUser(user); } //获得数据库中所有的user public List getAllObject() { return userDao.getAllObject(); } public User getUser(String name) { // TODO Auto-generated method stub return userDao.getUser(name); } //根据id查询用户 public User findUserById(String userid) { // TODO Auto-generated method stub return userDao.getUserById(userid); } //get set public UserDao getUserDao() { return userDao; } public void setUserDao(UserDao userDao) { this.userDao = userDao; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }
package com.hikvision.poster;
action
PosterAction.java
package com.hikvision.poster.action; import java.util.Date; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import com.hikvision.login.model.User; import com.hikvision.poster.model.Contents; import com.hikvision.poster.model.Poster; import com.hikvision.poster.service.PosterService; import com.hikvision.poster.service.PosterServiceImpl; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class PosterAction extends ActionSupport{ private Poster poster; private User user; private Contents content; private List listP; private List listContent; private String postidinfo;//选中帖子的id private PosterService posterService = new PosterServiceImpl(); private String userid;//用于传递登录人的id HttpServletRequest request=ServletActionContext.getRequest(); //跳转到新帖页面 public String newposter(){ //这个是从帖子的详细页面 --点击’编辑‘按钮时 过来的 if(postidinfo!=null){ poster = posterService.findPosterById(postidinfo); } return SUCCESS; } //新帖子的form表单的提交 public String addposter(){ String falginfo=""; if(postidinfo!=null&&poster.getId()!=null){//编辑帖子 Poster oldPoster = posterService.findPosterById(postidinfo); oldPoster.setPostertext(poster.getPostertext()); oldPoster.setPostertitle(poster.getPostertitle()); posterService.editPoster(oldPoster); falginfo="EDITINFO"; }else{//新建帖子 posterService.savePoster(poster); falginfo="SUCCESS"; } user= (User)request.getSession().getAttribute("user"); userid = (String)request.getSession().getAttribute("userid"); listP = posterService.getAllPosters(); return falginfo; } //编辑帖子 public String editposter(){ return SUCCESS; } //选择后查看具体的帖子 public String posterDetails(){ poster = posterService.findPosterById(postidinfo);//具体的帖子 //listContent = posterService.getAllContent();//得到所有的评论 listContent = posterService.getContentById(postidinfo); return SUCCESS; } //添加评论 public String newcontent(){ user= (User)request.getSession().getAttribute("user"); userid = (String)request.getSession().getAttribute("userid"); content.setPosterid(Integer.valueOf(postidinfo)); content.setUserid(Integer.valueOf(userid));//当前用户id content.setContenterid(Integer.valueOf(userid));//评论者的id content.setContenttime(new Date()); posterService.saveContent(content); poster = posterService.findPosterById(postidinfo); //listContent = posterService.getAllContent(); listContent = posterService.getContentById(postidinfo); return SUCCESS; } //添加replycontent public String replycontent(){ //保存回复 posterService.saveReplay(content); poster = posterService.findPosterById(postidinfo); //listContent = posterService.getAllContent(); listContent = posterService.getContentById(postidinfo); return SUCCESS; } //set get public Poster getPoster() { return poster; } public void setPoster(Poster poster) { this.poster = poster; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String getUserid() { return userid; } public void setUserid(String userid) { this.userid = userid; } public List getListP() { return listP; } public void setListP(List listP) { this.listP = listP; } public String getPostidinfo() { return postidinfo; } public void setPostidinfo(String postidinfo) { this.postidinfo = postidinfo; } public Contents getContent() { return content; } public void setContent(Contents content) { this.content = content; } public List getListContent() { return listContent; } public void setListContent(List listContent) { this.listContent = listContent; } }
dao
PosterDao.java
package com.hikvision.poster.dao; import java.util.List; import com.hikvision.poster.model.Contents; import com.hikvision.poster.model.Poster; public interface PosterDao { //添加 新帖 void savePoster(Poster poster); List getAllPoster(); Poster findPosterById(String postidinfo); void editPoster(Poster poster); void saveContent(Contents content); List getAllContent(); List getContentById(String postidinfo); void saveReplsy(Contents content); }
PosterDaoImpl.java
package com.hikvision.poster.dao; import java.util.Date; import java.util.Iterator; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import com.hikvision.login.model.User; import com.hikvision.poster.model.Contents; import com.hikvision.poster.model.Poster; public class PosterDaoImpl implements PosterDao { //默认读取hibernate.cfg.xml文件 Configuration cfr = new Configuration().configure(); //SchemaExport export = new SchemaExport(cfr); //export.create(true, true); SessionFactory factory = cfr.buildSessionFactory(); Session session = null; //保存新帖 public void savePoster(Poster poster) { try{ session = factory.openSession(); //开启事务 session.beginTransaction(); Poster poster1 = new Poster(); //String title = new String(poster.getPostertitle().getBytes("ISO-8859-1"),"UTF-8"); //String test = new String(poster.getPostertext().getBytes("ISO-8859-1"),"UTF-8"); poster1.setPostertitle(poster.getPostertitle()); poster1.setPostertext(poster.getPostertext()); poster1.setPostercreatetime(new Date()); poster1.setSetvisiable("1"); session.save(poster1); //提交事务 session.getTransaction().commit(); }catch(Exception e){ e.printStackTrace(); //回滚事务 session.getTransaction().rollback(); } finally{ if(session != null){ if(session.isOpen()){ //关闭session session.close(); } } } } //返回所有的帖子--并以发帖日期为倒序排列 public List getAllPoster() { List userList = null; try{ session = factory.openSession(); //开启事务 session.beginTransaction(); Query query = session.createQuery("from Poster ORDER BY postercreatetime DESC"); userList = query.list(); for(Iterator it = userList.iterator();it.hasNext();){ Poster poster = it.next(); System.out.println(poster.getId()+":"+poster.getPostertitle()+":"+poster.getPostertitle()); } }catch(Exception e){ e.printStackTrace(); //回滚事务 session.getTransaction().rollback(); } finally{ if(session != null){ if(session.isOpen()){ //关闭session session.close(); } } } return userList; } public Poster findPosterById(String postidinfo) { List postList = null; Poster poster = null; try{ session = factory.openSession(); //开启事务 session.beginTransaction(); Query query = session.createQuery(" from Poster t where t.id = ? "); query.setString(0, postidinfo); postList = query.list(); if(postList!=null){ poster = (Poster) postList.get(0); } }catch(Exception e){ e.printStackTrace(); //回滚事务 session.getTransaction().rollback(); } finally{ if(session != null){ if(session.isOpen()){ //关闭session session.close(); } } } return poster; } //编辑帖子 public void editPoster(Poster poster) { try{ session = factory.openSession(); //开启事务 session.beginTransaction(); session.saveOrUpdate(poster); System.out.println(poster.getPostertitle()); System.out.println(poster.getPostertext()); //提交事务 session.getTransaction().commit(); }catch(Exception e){ e.printStackTrace(); //回滚事务 session.getTransaction().rollback(); } finally{ if(session != null){ if(session.isOpen()){ //关闭session session.close(); } } } } //////////////////////////////////////////////评论///////////////////////////////////////////////////////// //添加评论 public void saveContent(Contents content) { try{ session = factory.openSession(); //开启事务 session.beginTransaction(); session.save(content); System.out.println(content.getContenttext()+content.getPosterid()); //提交事务 session.getTransaction().commit(); }catch(Exception e){ e.printStackTrace(); //回滚事务 session.getTransaction().rollback(); } finally{ if(session != null){ if(session.isOpen()){ //关闭session session.close(); } } } } //返回所有评论 public List getAllContent() { List contentList = null; try{ session = factory.openSession(); //开启事务 session.beginTransaction(); // Query query = session.createQuery("from Contents ORDER BY contenttime DESC");//from contents con,users u WHERE con.contenterid = u.id ORDER BY con.contenttime DESC contentList = query.list(); for(Iterator it = contentList.iterator();it.hasNext();){ Contents content = it.next(); System.out.println(content.getContenterid()+content.getContenttext()); } }catch(Exception e){ e.printStackTrace(); //回滚事务 session.getTransaction().rollback(); } finally{ if(session != null){ if(session.isOpen()){ //关闭session session.close(); } } } return contentList; } //返回一个帖子下面的评论 public List getContentById(String postidinfo) { List contentList = null; try{ session = factory.openSession(); //开启事务 session.beginTransaction(); //from Poster t where t.id = ? Query query = session.createQuery("from Contents where posterid = ? ORDER BY contenttime DESC");//from contents con,users u WHERE con.contenterid = u.id ORDER BY con.contenttime DESC query.setInteger(0, Integer.valueOf(postidinfo)); //.setString(0, postidinfo); contentList = query.list(); for(Iterator it = contentList.iterator();it.hasNext();){ Contents content = it.next(); System.out.println(content.getContenterid()+content.getContenttext()); } }catch(Exception e){ e.printStackTrace(); //回滚事务 session.getTransaction().rollback(); } finally{ if(session != null){ if(session.isOpen()){ //关闭session session.close(); } } } return contentList; } //添加回复 public void saveReplsy(Contents content) { try{ session = factory.openSession(); //开启事务 session.beginTransaction(); session.save(content); System.out.println(content.getContenttext()+content.getPosterid()); //提交事务 session.getTransaction().commit(); }catch(Exception e){ e.printStackTrace(); //回滚事务 session.getTransaction().rollback(); } finally{ if(session != null){ if(session.isOpen()){ //关闭session session.close(); } } } } }
model
Contents.java
package com.hikvision.poster.model; import java.util.Date; public class Contents implements java.io.Serializable{ private Integer id; private String contenttext; private Date contenttime; private Integer contenterid; private Integer posterid; private Integer userid; private Integer contentid;//评论的id private static final long serialVersionUID = 1L; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getContenttext() { return contenttext; } public void setContenttext(String contenttext) { this.contenttext = contenttext; } public Date getContenttime() { return contenttime; } public void setContenttime(Date contenttime) { this.contenttime = contenttime; } public Integer getContenterid() { return contenterid; } public void setContenterid(Integer contenterid) { this.contenterid = contenterid; } public Integer getPosterid() { return posterid; } public void setPosterid(Integer posterid) { this.posterid = posterid; } public Integer getUserid() { return userid; } public void setUserid(Integer userid) { this.userid = userid; } public Integer getContentid() { return contentid; } public void setContentid(Integer contentid) { this.contentid = contentid; } }
Contents.hbm.xml
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
Poster.java
package com.hikvision.poster.model; import java.sql.Timestamp; import java.util.Date; public class Poster implements java.io.Serializable{ private Integer id; private String postertitle; private String postertext; private Date postercreatetime; private Timestamp posterupdatetime; private String image; private Integer userid; private String setvisiable; private static final long serialVersionUID = 1L; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getPostertitle() { return postertitle; } public void setPostertitle(String postertitle) { this.postertitle = postertitle; } public String getPostertext() { return postertext; } public void setPostertext(String postertext) { this.postertext = postertext; } public Date getPostercreatetime() { return postercreatetime; } public void setPostercreatetime(Date postercreatetime) { this.postercreatetime = postercreatetime; } public Timestamp getPosterupdatetime() { return posterupdatetime; } public void setPosterupdatetime(Timestamp posterupdatetime) { this.posterupdatetime = posterupdatetime; } public String getImage() { return image; } public void setImage(String image) { this.image = image; } public Integer getUserid() { return userid; } public void setUserid(Integer userid) { this.userid = userid; } public String getSetvisiable() { return setvisiable; } public void setSetvisiable(String setvisiable) { this.setvisiable = setvisiable; } }
Poster.hbm.xml
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
service
PosterService.java
package com.hikvision.poster.service; import java.util.List; import com.hikvision.poster.model.Contents; import com.hikvision.poster.model.Poster; public interface PosterService { //添加 帖子 void savePoster(Poster poster); List getAllPosters(); Poster findPosterById(String postidinfo); void editPoster(Poster poster); void saveContent(Contents content); List getAllContent(); List getContentById(String postidinfo); void saveReplay(Contents content); }
PosterServiceImpl.java
package com.hikvision.poster.service; import java.util.List; import com.hikvision.poster.dao.PosterDao; import com.hikvision.poster.dao.PosterDaoImpl; import com.hikvision.poster.model.Contents; import com.hikvision.poster.model.Poster; public class PosterServiceImpl implements PosterService{ private PosterDao posterDao = new PosterDaoImpl(); private Poster poster; //添加 新帖 public void savePoster(Poster poster) { posterDao.savePoster(poster); } //获得所有的帖子 public List getAllPosters() { return posterDao.getAllPoster(); } //根据id去查帖子 public Poster findPosterById(String postidinfo) { return posterDao.findPosterById(postidinfo); } //编辑帖子 public void editPoster(Poster poster) { // TODO Auto-generated method stub posterDao.editPoster(poster); } //////////////////////////////////////////////////评论//////////////////////////////////////////////// //添加评论 public void saveContent(Contents content) { // TODO Auto-generated method stub posterDao.saveContent(content); } //返回所有评论 public List getAllContent() { return posterDao.getAllContent(); } //返回一个帖子下面的评论 public List getContentById(String postidinfo) { return posterDao.getContentById(postidinfo); } //保存回复 public void saveReplay(Contents content) { posterDao.saveReplsy(content); } //get set public Poster getPoster() { return poster; } public void setPoster(Poster poster) { this.poster = poster; } }
hibernate配置文件
hibernate.cfg.xml
"-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> root 888 com.mysql.jdbc.Driver jdbc:mysql:///stu org.hibernate.dialect.MySQLDialect true true update true utf8 log4j.properties 配置文件 log4j.rootCategory=debug, stdout , R log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=[QC] %p %d{yyyy-MM-dd HH:mm:ss} [%t] %C.%M(%L) | %m%n log4j.appender.R=org.apache.log4j.DailyRollingFileAppender log4j.appender.R.File=D\:\\Tomcat 5.5\\logs\\qc.log log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=%d-[TS] %p %t %c - %m%n log4j.logger.com.neusoft=DEBUG log4j.logger.com.opensymphony.oscache=ERROR log4j.logger.net.sf.navigator=ERROR log4j.logger.org.apache.commons=ERROR log4j.logger.org.apache.struts=WARN log4j.logger.org.displaytag=ERROR log4j.logger.org.springframework=DEBUG log4j.logger.com.ibatis.db=WARN log4j.logger.org.apache.velocity=FATAL log4j.logger.com.canoo.webtest=WARN log4j.logger.org.hibernate.ps.PreparedStatementCache=WARN log4j.logger.org.hibernate=DEBUG log4j.logger.org.logicalcobwebs=WARN struts配置文件
struts.xml
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> struts-login.xml
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> /WEB-INF/hi.jsp /WEB-INF/login.jsp /WEB-INF/home.jsp loginback ${code} /WEB-INF/login.jsp /WEB-INF/home.jsp /WEB-INF/adduser.jsp /WEB-INF/home.jsp struts-poster.xml
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> /WEB-INF/poster/newposter.jsp /WEB-INF/home.jsp /WEB-INF/poster/details.jsp /WEB-INF/poster/details.jsp /WEB-INF/poster/details.jsp /WEB-INF/poster/details.jsp JSP页面
poster
details.jsp
newposter.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <%! Random rnd = new Random(); %> <% int n = rnd.nextInt(200);pageContext.setAttribute("n",n); %> BBS
"> "> 文章标题: "/> 文章内容: adduser.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@page import="com.hikvision.login.model.User"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% String flaginfo = "NOADMINadd"; String userid = null; String buttonname="添加"; User user = (User)request.getAttribute("user"); String flaginfos = request.getParameter("flaginfo"); if(flaginfo!=null){ flaginfo = flaginfos; userid = request.getParameter("userid"); buttonname="修改"; } %> 注册用户
name: password:
home.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@page import="com.hikvision.login.model.User"%> <%@page import="com.hikvision.poster.model.Poster"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% String userid = (String)request.getAttribute("userid");//登录用户的id String hrefinfo = "/Struts2Demo/toHome?flaginfo=NOADMINedit&userid="+userid; //Poster p = (Poster)request.getAttribute("p"); //String posterinfo = "/Struts2Demo/poster/posterDetails?postid="+""; %> Home 欢迎您:${user.username } 注销 发布新帖
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <%! Random rnd = new Random(); %> <% int n = rnd.nextInt(200);pageContext.setAttribute("n",n); %> 登录BBS web.xml
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> index.jsp struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2 /* 数据库
数据库只有3个 表,分别是users,posters,contents
users--用户表
posters--帖子表,所有的帖子都放在这个里面
contents--评论表(但对评论的回复也放在这个表中,查的时间只要关联查出来就行,但我觉得这个设计有问题,不过有没有好的方案,所以有点纠结,你自己看下。。或者重新设计个表。。。。)
新建数据库和表 新建数据库
安装mysql后新建一个数据库 stu,格式如下:
新建完数据库后就可以在库中新建表了,具体执行下面的语句。(语句中包含一些测试数据,你可以进去看到效果)
users
/* Navicat MySQL Data Transfer Source Server : 练习 Source Server Version : 50622 Source Host : localhost:3306 Source Database : stu Target Server Type : MYSQL Target Server Version : 50622 File Encoding : 65001 Date: 2016-01-19 15:43:54 */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for users -- ---------------------------- DROP TABLE IF EXISTS `users`; CREATE TABLE `users` ( `id` int(10) NOT NULL AUTO_INCREMENT, `username` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '用户名', `userpassword` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '用户密码', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; -- ---------------------------- -- Records of users -- ---------------------------- INSERT INTO `users` VALUES ('4', 'tom', '123456'); INSERT INTO `users` VALUES ('9', 'admin', 'admin12345+'); INSERT INTO `users` VALUES ('10', null, null); INSERT INTO `users` VALUES ('11', null, null); INSERT INTO `users` VALUES ('12', null, null);
contents
/* Navicat MySQL Data Transfer Source Server : 练习 Source Server Version : 50622 Source Host : localhost:3306 Source Database : stu Target Server Type : MYSQL Target Server Version : 50622 File Encoding : 65001 Date: 2016-01-19 15:40:14 */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for contents -- ---------------------------- DROP TABLE IF EXISTS `contents`; CREATE TABLE `contents` ( `id` int(11) NOT NULL AUTO_INCREMENT, `contenttext` varchar(4000) COLLATE utf8_bin DEFAULT NULL COMMENT '评论内容', `contenttime` datetime DEFAULT NULL COMMENT '评论时间', `contenterid` int(11) DEFAULT NULL COMMENT '评论者的id', `posterid` int(11) DEFAULT NULL COMMENT '帖子id', `userid` int(11) DEFAULT NULL COMMENT '发帖人的id', `contentid` int(11) DEFAULT NULL COMMENT '评论的id 这个主要用于和回复关联', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; -- ---------------------------- -- Records of contents -- ---------------------------- INSERT INTO `contents` VALUES ('1', 'hahah', '2016-01-18 16:14:41', '4', '10', '4', null); INSERT INTO `contents` VALUES ('2', 'hahah', '2016-01-18 16:16:10', '4', '10', '4', null); INSERT INTO `contents` VALUES ('3', 'hahah', '2016-01-18 16:18:22', '4', '10', '4', null); INSERT INTO `contents` VALUES ('4', 'hahah', '2016-01-18 16:18:55', '4', '10', '4', null); INSERT INTO `contents` VALUES ('5', 'dfdfsd ', '2016-01-18 16:32:37', '4', '10', '4', null); INSERT INTO `contents` VALUES ('6', '在评论一次\r\n', '2016-01-18 16:32:48', '4', '10', '4', null); INSERT INTO `contents` VALUES ('7', '在评论一次\r\n', '2016-01-18 16:35:15', '4', '10', '4', null); INSERT INTO `contents` VALUES ('8', '在评论一次\r\n', '2016-01-18 16:36:13', '4', '10', '4', null); INSERT INTO `contents` VALUES ('9', '在评论一次\r\n', '2016-01-18 16:37:32', '4', '10', '4', null); INSERT INTO `contents` VALUES ('10', 'ddddd', '2016-01-18 16:59:43', '4', '10', '4', null); INSERT INTO `contents` VALUES ('11', 'ddddd', '2016-01-18 17:04:50', '4', '10', '4', null); INSERT INTO `contents` VALUES ('12', 'ddddd', '2016-01-18 17:06:21', '4', '10', '4', null); INSERT INTO `contents` VALUES ('13', 'kkkk', '2016-01-18 19:40:22', '4', '11', '4', null); INSERT INTO `contents` VALUES ('14', 'kkkk', '2016-01-18 19:41:29', '4', '11', '4', null); INSERT INTO `contents` VALUES ('15', 'fff', '2016-01-18 19:41:38', '4', '11', '4', null); INSERT INTO `contents` VALUES ('16', 'hahah', '2016-01-18 19:53:18', '4', '11', '4', null); INSERT INTO `contents` VALUES ('17', 'gggg', '2016-01-18 19:58:24', '4', '11', '4', null); INSERT INTO `contents` VALUES ('18', 'gggg', '2016-01-18 20:06:25', '4', '11', '4', null); INSERT INTO `contents` VALUES ('19', 'gggg', '2016-01-18 20:08:12', '4', '11', '4', null); INSERT INTO `contents` VALUES ('20', 'gggg', '2016-01-18 20:08:18', '4', '11', '4', null); INSERT INTO `contents` VALUES ('21', 'gggg', '2016-01-18 20:08:45', '4', '11', '4', null); INSERT INTO `contents` VALUES ('22', 'aaa', '2016-01-18 20:10:32', '4', '12', '4', null);
posters
/ * Navicat MySQL Data Transfer Source Server : 练习 Source Server Version : 50622 Source Host : localhost:3306 Source Database : stu Target Server Type : MYSQL Target Server Version : 50622 File Encoding : 65001 Date: 2016-01-19 15:43:47 */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for posters -- ---------------------------- DROP TABLE IF EXISTS `posters`; CREATE TABLE `posters` ( `id` int(11) NOT NULL, `postertitle` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '帖子标题', `postertext` varchar(4000) COLLATE utf8_bin NOT NULL COMMENT '帖子内容', `postercreatetime` datetime DEFAULT NULL COMMENT '发帖时间', `posterupdatetime` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间', `image` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '上传图片', `userid` int(11) DEFAULT NULL COMMENT '发帖人的id', `setvisiable` varchar(1) COLLATE utf8_bin NOT NULL COMMENT '帖子是否显示', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; -- ---------------------------- -- Records of posters -- ---------------------------- INSERT INTO `posters` VALUES ('1', 'testccc', 'testsssscc', '2016-01-14 15:11:33', null, null, null, '1'); INSERT INTO `posters` VALUES ('2', 'rr', 'rr', '2016-01-14 16:33:49', null, null, null, '1'); INSERT INTO `posters` VALUES ('3', 'oo', 'oo', '2016-01-14 16:46:17', null, null, null, '1'); INSERT INTO `posters` VALUES ('5', '8', '8', '2016-01-14 16:56:12', null, null, null, '1'); INSERT INTO `posters` VALUES ('6', '1', '1', '2016-01-14 17:00:23', null, null, null, '1'); INSERT INTO `posters` VALUES ('7', '2', '2', '2016-01-14 17:00:37', null, null, null, '1'); INSERT INTO `posters` VALUES ('8', '2', '2', '2016-01-14 17:00:42', null, null, null, '1'); INSERT INTO `posters` VALUES ('9', '各个kk', '各个kk', '2016-01-18 15:16:07', null, null, null, '1'); INSERT INTO `posters` VALUES ('10', '再测试一遍 改变cc', '啦啦啦cc', '2016-01-18 15:17:07', null, null, null, '1'); INSERT INTO `posters` VALUES ('11', '这个可以用吗', '得到', '2016-01-18 17:08:55', null, null, null, '1'); INSERT INTO `posters` VALUES ('12', 'aa', 'aa', '2016-01-18 20:10:27', null, null, null, '1'); 关于jar包
由于不能上传jar包,所有你自己下载,主要是hibernate和struts的包,log4j等的包,如果运行不起来会有 提示错去,如果是关于jar包的,下载后导入就像了