一.实现效果图:
主页面:
登陆后跳转的页面:
添加新闻=====文件上传:
编辑新闻----修改 删除:
添加主题===分类:
编辑主题:
二.分层架构图:
web项目里后台的分层:
三.如下就是功能的实现代码:
登陆的主要代码:
package cn.news.dao.impl; import java.sql.SQLException; import org.junit.Test; import cn.news.dao.BaseDAO; import cn.news.dao.IUserInfoDAO; import cn.news.entity.UserInfo; public class UserInfoDAOImpl extends BaseDAO implements IUserInfoDAO { @Test public void loginTest() throws SQLException{ UserInfo info =new UserInfo(); info.setUname("admin"); info.setUpwd("admin"); boolean flag= isLogin(info); if (flag) { System.out.println("login success!"); } } public boolean isLogin(UserInfo info) throws SQLException { boolean flag=false; String sql="select count(1) as mycount from useerinfo where uname=? and upwd=?"; Object[] paras={info.getUname(),info.getUpwd()}; getConection(); rs=executeQuery(sql,paras); if (rs.next()) { int count = rs.getInt("mycount"); if (count>0) { //登录成功 flag=true; } } closeResources(); return flag; } }
获取新闻内容代码:
package cn.news.serlvet; import java.io.IOException; import java.io.PrintWriter; import java.sql.SQLException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.news.entity.NewsInfo; import cn.news.entity.UserInfo; import cn.news.service.INewsInfoService; import cn.news.service.IUserInfoService; import cn.news.service.impl.NewsInfoServiceImpl; import cn.news.service.impl.UserInfoServiceImpl; public class UserInfoServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost( request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //准备新闻数据 INewsInfoService newsservice=new NewsInfoServiceImpl(); try { List<NewsInfo> newsList = newsservice.getAllNews(); //放入request作用域 request.setAttribute("newsList", newsList); } catch (SQLException e1) { } String action=request.getParameter("action"); //logout if ("logout".equals(action)) { //执行注销操作 //1.清除session request.getSession().removeAttribute("uname"); //2.跳转到index /*response.sendRedirect("/NewsManagerSystem/index.jsp");*/ request.getRequestDispatcher("/index.jsp").forward(request, response); }else if("login".equals(action)) { //code write here //1.解决乱码 request.setCharacterEncoding("utf-8"); //2.解析前台传递的表单数据 String uname=request.getParameter("uname"); String upwd=request.getParameter("upwd"); //拼接成一个用户对象 UserInfo info=new UserInfo(); info.setUname(uname); info.setUpwd(upwd); IUserInfoService service=new UserInfoServiceImpl(); try { boolean flag = service.isLogin(info); if (flag) { //登录成功 //3.记录session request.getSession().setAttribute("uname", uname); //4.转发或者重定向 转发不需要加项目名称 request.getRequestDispatcher("/newspages/admin.jsp").forward(request, response); }else { //response.sendRedirect("/NewsManagerSystem/index.jsp"); request.getRequestDispatcher("/index.jsp").forward(request, response); } } catch (SQLException e) { e.printStackTrace(); }//code write here }else { //第一次访问首页 request.getRequestDispatcher("/index.jsp").forward(request, response); } }
页面调用:
<script type="text/javascript"> <%if("true".equals(request.getAttribute("longinfailure"))){%> alert("用户名或密码为空!"); <%}%> </script> </head> <body> <div id="header"> <div id="top_login"> <form action="<%=path%>/UserInfoNewServlet" method="post"> <label> 登录名 </label> <input type="text" id="uname" name="uname" value="" class="login_input" /> <label> 密  码 </label> <input type="password" id="upwd" name="upwd" value="" class="login_input" /> <input type="submit" class="login_sub" value="登录" />
分页:
package cn.news.util; import java.util.List; import cn.news.entity.NewsInfo; public class Page { // 当前页 private int pageIndex; // 页面总记录数 private int pageSize; // 本业显示真实数据 private List<NewsInfo> list; // 总页数 private int totalPages; // 总记录数 private int totalRecords; public int getPageIndex() { return pageIndex; } public void setPageIndex(int pageIndex) { this.pageIndex = pageIndex; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public List<NewsInfo> getList() { return list; } public void setList(List<NewsInfo> list) { this.list = list; } public int getTotalPages() { return totalPages; } public void setTotalPages(int totalPages) { this.totalPages = totalPages; } public int getTotalRecords() { return totalRecords; } public void setTotalRecords(int totalRecords) { this.totalRecords = totalRecords; } }
从数据库里拿到的分页代码如下:
@Override public List<NewsInfo> getPageInfos(int pageIndex, int pageSize) throws Exception { List<NewsInfo> list=new ArrayList<NewsInfo>(); String sql="SELECT * FROM newinfo LIMIT ?,?"; rs=executeQuery(sql,(pageIndex-1)*pageSize,pageSize); if (rs!=null) { while (rs.next()) { NewsInfo info=new NewsInfo(); int nid=rs.getInt("nid"); String ntitle=rs.getString("ntitle"); String nauthor=rs.getString("nauthor"); Date npublisherdate=rs.getDate("npublisherdate"); String ncontent=rs.getString("ncontent"); int tid=rs.getInt("tid"); info.setNauthor(nauthor); info.setNcontent(ncontent); info.setNid(nid); info.setNpublisherdate(npublisherdate); info.setNtitle(ntitle); info.setTid(tid); list.add(info); } } return list; }
servlet调用:
public void pagenews(HttpServletRequest request,HttpServletResponse response) { IUserInfoService daoInfoService = new UserInfoService(); try { Page oaPage = new Page(); // 默认三条数据 int pageSize = 3; oaPage.setPageSize(pageSize); // pageIndex(当前页) int myindex = 1; String pageIndex = request.getParameter("pageIndex"); if (null!=pageIndex && (!pageIndex.equals(""))) { myindex = Integer.parseInt(pageIndex); } // 总页数赋值=总记录数/pageSize int mytotalPages = 0; int ipages = daoInfoService.selectnewsall(); if (ipages % pageSize == 0) { mytotalPages = ipages / pageSize; } else { mytotalPages = ipages / pageSize + 1; } oaPage.setTotalPages(mytotalPages); if(myindex>oaPage.getTotalPages()){ myindex=oaPage.getTotalPages(); System.out.println(myindex+"1"); } System.out.println("================================="); if (myindex<1) { myindex=1; System.out.println(myindex+"2"); } oaPage.setPageIndex(myindex); // 泛型数据赋值 List<NewsInfo> list = daoInfoService.getPageInfos(oaPage.getPageIndex(), pageSize); oaPage.setList(list); // 将泛型集合,放入request作用域 request.setAttribute("list", oaPage); // 转发index.jsp 转发不用加名称 } catch (Exception e) { e.printStackTrace(); } } public void pageNews(HttpServletRequest request,HttpServletResponse response) { IUserInfoService daoInfoService = new UserInfoService(); try { Page oaPage = new Page(); // 默认三条数据 int pageSize = 3; oaPage.setPageSize(pageSize); // pageIndex(当前页) int myindex = 1; String pageIndex = request.getParameter("pageIndex"); if (null!=pageIndex && (!pageIndex.equals(""))) { myindex = Integer.parseInt(pageIndex); } // 给当前页赋值 // 总页数赋值=总记录数/pageSize int mytotalPages = 0; int ipages = daoInfoService.selectnewsall(); if (ipages % pageSize == 0) { mytotalPages = ipages / pageSize; } else { mytotalPages = ipages / pageSize + 1; } oaPage.setTotalPages(mytotalPages); if(myindex>oaPage.getTotalPages()){ myindex=oaPage.getTotalPages(); System.out.println(myindex+"1"); } if (myindex<1) { myindex=1; System.out.println(myindex+"2"); } oaPage.setPageIndex(myindex); // 泛型数据赋值 List<NewsInfo> list = daoInfoService.getPageInfos(oaPage.getPageIndex(), pageSize); oaPage.setList(list); // 将泛型集合,放入request作用域 request.setAttribute("list", oaPage); // 转发index.jsp 转发不用加名称 } catch (Exception e) { e.printStackTrace(); } }
显示的页数:
<p align="right"> 当前页数:[${list.pageIndex}/${list.totalPages}] <a href="${pageContext.request.contextPath}/NewsServlet?pageIndex=${list.pageIndex-1}">上一页</a> <a href="${pageContext.request.contextPath}/NewsServlet?pageIndex=${list.pageIndex+1}">下一页</a> <a href="${pageContext.request.contextPath}/NewsServlet?pageIndex=${list.totalPages}">末页</a> </p>
本期就到这里,等待下期继续——————————————————