zoukankan      html  css  js  c++  java
  • 新闻发布系统

    ·一.登录模块开发
        1.创建了一个用户信息实体类(数据表的字段)
        2.开发了Dao层:创建了一个接口,接口中有一个方法
           //登录的方法
           public User login(String username) throws Exception;
        3.开发了Dao层的实现层,创建了一个类,实现了Dao层接口,重写了login方法
           @Override
           public User login(String username) throws Exception {
            User user=null;
            //步骤一:创建一条SQL
            String sql="select * from news_users where uname=?";
            //步骤二:执行SQL并且接受返回值
            ResultSet rs = executeQuery(sql, username);
            //步骤三:将返回值装配到实体对象当中
            if(rs!=null) {
             while(rs.next()) {
              user=new User();
              user.setUid(rs.getInt("uid"));
              user.setUname(rs.getString("uname"));
              user.setUpwd(rs.getString("upwd"));
             }
            }
            return user;
           }
        4.开发Service层,创建一个接口,接口中有一个login方法,
           public interface IUserInfoService {
            //登录的方法
            public User login(String username,String password) throws Exception;
           }
        5.开发Service的实现类,创建了一个类,这个类实现了Service接口,重写了login方法,方法中主要将dao层拿到的数据跟用户输入的数据做比较
           public class IUserInfoServiceImpl implements IUserInfoService{
            //植入Dao层对象
            private IUserInfoDao userinfoDao=new IUserInfoDaoImpl();
            @Override
            public User login(String username, String password) throws Exception {
             //执行login方法,从数据库中拿出当前查询到的user数据
             User user = userinfoDao.login(username);
             //先判断从dao层接收过来的user对象是否为空,在不是空的情况下,用实体中存放的数据跟用户输入的数据做对比
             if(user!=null&&user.getUpwd().equals(password)) {
              return user;
             }
             return null;
            }
           }
        6.开发Servlet层,创建了一个类,这个类继承了HttpServlet抽象类,重写了doGet()和doPost()方法,在doGet()中调用doPost(),内部实现逻辑如下:
           6.1从前台能够接受到用户提交的用户名和密码
           6.2调用service层的login方法执行登录操作
           6.3接收方法的返回值,判断返回值是否为空,如果为空则代表登录失败,跳转到Login.jsp,如果不为空,将用户信息保存到session对象当中,并且跳转到首页
     二.非法路径拦截(没有登录的用户无法进行后台操作)
          在需要拦截的页面中加入如下代码(判断session对象当中是否有用户登录的信息,如果没有则跳转到登录页面)
           <%
            //获取Session的存储的登录信息
            User user=(User)session.getAttribute("user");
            if(user==null){
             response.sendRedirect("page/login/login.jsp");
            }
           %>
     三.退出系统
          在Servlet当中加上对应模块的代码,用了一个action地址区分不同的功能
          if(action.equals("logOut")) {
           //清空Session
           req.getSession().removeAttribute("user");
           resp.sendRedirect("/NewsManager");
          }
          <a href="<%=basePath %>IUserInfoServlet?action=logOut" class="signOut"><i class="seraph icon-tuichu"></i><cite>退出</cite></a>
      
     四.新闻列表
        1.开发Dao层,创建一个Dao层接口,在接口中添加一个查询所有新闻的方法
           public interface INewsDao {
            //查询所有新闻的方法
            public List<News> getAllNews() throws Exception;
           }
        2.创建Dao层实现类,创建一个类,实现Dao层接口,然后执行数据库查询操作
           @Override
           public List<News> getAllNews() throws Exception {
            List<News> newsList=new ArrayList<News>();
            //步骤一:创建Sql
            String sql="select * from news,topic where news.ntid=topic.tid";
            //步骤二:执行
            ResultSet rs = executeQuery(sql);
            //步骤三:装载数据
            if(rs!=null) {
             while(rs.next()) {
              News news=new News();
              news.setNid(rs.getInt("nid"));
              news.setNtitle(rs.getString("ntitle"));
              news.setNtid(rs.getInt("ntid"));
              news.setNauthor(rs.getString("nauthor"));
              news.setNcontent(rs.getString("ncontent"));
              news.setNpicPath(rs.getString("npicpath"));
              news.setNcreateDate(rs.getDate("ncreateDate"));
              news.setNmodifyDate(rs.getDate("nmodifyDate"));
              news.setNsummary(rs.getString("nsummary"));
              Topic topic=new Topic();
              topic.setTid(rs.getInt("tid"));
              topic.setTname(rs.getString("tname"));
              //怎么让当前拿到的News对象和Topic对象关联起来
              news.setTopic(topic);
              newsList.add(news);
             }
            }
            return newsList;
           }
        3.创建Service层接口,跟Dao层接口的方法相同
           public interface INewsService {
            //查询所有新闻的方法
            public List<News> getAllNews() throws Exception;
           }
        4.创建Service层的实现类,实现Service层接口
           public class INewsServiceImpl implements INewsService{
            //植入Dao层接口对象
            private INewsDao newsDao=new INewsDaoImpl();
            @Override
            public List<News> getAllNews() throws Exception {
             return newsDao.getAllNews();
            }
           }
        5.Servlet处理用户请求,调用service层拿到从数据库查询出来的数据,将数据保存到request作用域当中,跳转到新闻列表页面
           @Override
           protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String action=req.getParameter("action");
            if(action.equals("newsList")) {
             //执行查询新闻列表操作
             try {
              List<News> allNews = newService.getAllNews();
              for (News news : allNews) {
               System.out.println(news.getNid());
              }
              //把集合数据保存下来
              req.setAttribute("newsList", allNews);
              //跳转页面
              req.getRequestDispatcher("page/news/newsList.jsp").forward(req, resp);
             } catch (Exception e) {
              e.printStackTrace();
             }
            }
           }
        6.渲染数据
           <table class="layui-table">
            <thead>
             <tr>
              <th>新闻编号</th>
              <th>新闻标题</th>
              <th>新闻类别</th>
              <th>发布者</th>
              <th>发布时间</th>
              <th>操作</th>
             </tr>
             <%
              List<News> newsList = (List<News>) request.getAttribute("newsList");
              for (News news : newsList) {
             %>
             <tr>
              <td><%=news.getNid()%></td>
              <td><%=news.getNtitle()%></td>
              <td><%=news.getTopic().getTname()%></td>
              <td><%=news.getNauthor()%></td>
              <td><%=news.getNcreateDate()%></td>
              <td><a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
               <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
               <a class="layui-btn layui-btn-xs layui-btn-primary"  lay-event="look">预览</a></td>
             </tr>
             <%
              }
             %>
            </thead>
           </table>
    新闻删除:
        1.在INewsDao接口中创建删除方法
           //删除新闻
           public int deleteNews(int nid) throws Exception;
        2.在INewsDaoImpl中重写该方法,执行数据库操作
           @Override
           public int deleteNews(int nid) throws Exception {
            String sql="delete from news where nid=?";
            int count = executeUpdate(sql, nid);
            return count;
           }
        3.在INewsService接口中创建删除方法,供Servlet调用
           //删除新闻
           public int deleteNews(int nid) throws Exception;
        4.在INewsServiceImpl实现类中重写该方法,调用Dao层该方法
           @Override
           public int deleteNews(int nid) throws Exception {
            return newsDao.deleteNews(nid);
           }
        5.在INewsServlet中通过action控制功能,调用Service层该方法进行删除,删除成功调用新闻列表地址
           else if(action.equals("deleteNews")) {
            //获取页面上选择的新闻id
            String nid=req.getParameter("nid");
            try {
             //执行删除信息的方法
             int count = newService.deleteNews(Integer.parseInt(nid));
             if(count>0) {
              req.getRequestDispatcher("INewsServlet?action=newsList").forward(req, resp);
             }
            } catch (NumberFormatException e) {
             e.printStackTrace();
            } catch (Exception e) {
             e.printStackTrace();
            }
           }
        6.页面中找到删除按钮,添加Servlet地址,进行删除
           <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del" href="INewsServlet?action=deleteNews&nid=<%=news.getNid() %>">删除</a>
    六.新闻添加
        步骤:需要将新闻分类的数据绑定到newsAdd.jsp页面中的下拉列表
         1.创建Dao层接口:ITopicDao,在里面写了一个方法用于查询所有的新闻分类
            //查询新闻分类
            public List<Topic> getAllTopic() throws Exception;
         2.创建Dao层接口实现类:ITopicDaoImpl,重写Dao层方法,查询数据
            @Override
            public List<Topic> getAllTopic() throws Exception {
             List<Topic> topicList=new ArrayList<>();
             //SQL语句
             String sql="select * from topic";
             ResultSet rs = executeQuery(sql);
             if(rs!=null) {
              while(rs.next()) {
               Topic topic=new Topic();
               topic.setTid(rs.getInt("tid"));
               topic.setTname(rs.getString("tname"));
               topicList.add(topic);
              }
             }
             closeResource();
             return topicList;
            }
         3.创建Service层接口,跟Dao层接口的方法相同
            //查询新闻分类
            public List<Topic> getAllTopic() throws Exception;
         4.创建Service层的实现类,实现Service层接口
            public class ITopicServiceImpl implements ITopicService{
             //创建Dao层接口对象
             private ITopicDao topicDao=new ITopicDaoImpl();

             @Override
             public List<Topic> getAllTopic() throws Exception {
              return topicDao.getAllTopic();
             }
            }
         5.创建Servlet实现用户访问
            String action=request.getParameter("action");
            if(action.equals("topicList")) {
             //查询所有新闻分类的操作
             try {
              List<Topic> allTopic = topicService.getAllTopic();
              request.setAttribute("topicList", allTopic);
              request.getRequestDispatcher("page/news/newsAdd.jsp").forward(request, response);
             } catch (Exception e) {
              e.printStackTrace();
             }
            }
         6.在newsList.jsp页面中,找到添加文章按钮,将href属性值改为访问Servlet的地址
            <a class="layui-btn layui-btn-normal" href="<%=basePath %>ITopicServlet?action=topicList">添加文章</a>
         7.newsAdd.jsp页面的数据渲染
            <select name="topic" lay-verify="required">
             <%
              List<Topic> allTopic=(List<Topic>)request.getAttribute("topicList");
              for(Topic topic:allTopic){ 
             %>
              <option value="<%=topic.getTid()%>"><%=topic.getTname()%></option>
             <%
              }
             %>
            </select>
     
  • 相关阅读:
    Android游戏开发22:Android动画的实现J2me游戏类库用于Android开发
    android sqlite SQLiteDatabase 操作大全 不看后悔!必收藏!看后精通SQLITE (第三部分,完整代码)
    使用OGR创建dxf格式矢量数据
    mysql 数据库引擎 MyISAM InnoDB 大比拼 区别
    android sqlite SQLiteDatabase 操作大全 不看后悔!必收藏!看后精通SQLITE (第二部分)
    mysql 更改数据库引擎
    android sqlite SQLiteDatabase 操作大全 不看后悔!必收藏!看后精通SQLITE (第一部分)
    android 数字键盘使用
    MySQL Innodb数据库性能实践
    eclipse : Error while performing database login with the driver null
  • 原文地址:https://www.cnblogs.com/wnwn/p/11155153.html
Copyright © 2011-2022 走看看