MVC2 项目实践

MVC框架模式

MVC: Model View Controller 模型-视图-控制器 
视图:用户看到并与之交互的界面。

模型:表示企业数据和业务规则。在MVC的3个部件中,模型拥有最多的处理任务。被模型返回的数据是独立的,与数据格式无关。这样的一个模式能够为多个视图提供数据,这样一来模型的代码只需要写一次便能够被多个视图重用,减少了代码的重复性。

控制器:接受用户的输入并调用相应的模型和视图来完成用户的需求,所以当用户点击web页面中的超链接和发送表单时,控制器本身不输出任何东西和做任何处理。它只是接收请求并根据请求来调用哪个模型去处理请求,然后在决定用哪个视图来显示返回的数据。

项目效果:

登录:

 

 登录后的新闻列表页面:

 

 添加新闻:

 

 

 

编辑新闻: 

 

 查看新闻详情:

 

 删除标题为题目3的新闻:

 

思路:

将news表中的idnews设置成自动加1

 

 下载富文本编辑器:http://ueditor.baidu.com/website/download.html

 将下载得到的文件解压放入web文件夹下的yinglang文件夹中

 

 添加新闻:

@WebServlet(name = "AddNewServlet")
public class AddNewServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.sendRedirect("AddNew.jsp"); //进入新闻添加界面
    }
}

 

@WebServlet(name = "SaveAddNewServlet")
public class SaveAddNewServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        News news=new News();

        news.setAuthor(request.getParameter("author"));
        news.setTitle(request.getParameter("title"));
        news.setContent(request.getParameter("content"));
        news.setCategory(request.getParameter("category"));
        news.setNewsdate(Date.valueOf(request.getParameter("newsdate")));
        NewsService newsService=new NewsService();
        System.out.println(newsService.AddNews(news));
        request.getRequestDispatcher("ShowNewsListServlet").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

删除新闻:

@WebServlet(name = "DeleteNewServlet")
public class DeleteNewServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        int idnews=Integer.valueOf(request.getParameter("idnews"));
        System.out.println(request.getParameter("idnews"));
        NewsService newsService=new NewsService();
        newsService.DeleteNews(idnews);
        request.getRequestDispatcher("ShowNewsListServlet").forward(request,response);
    }
}
public boolean DeleteNews(int idnews){
        String sql ="delete from news where idnews="+Integer.toString(idnews);
        System.out.println(sql);
        DatabaseOperation operation=new DatabaseOperation();
        boolean rs=operation.executeUpdate(sql);
        return  rs;
    }

修改新闻:

@WebServlet(name = "EditNewServlet")
public class EditNewServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        int idnews=Integer.valueOf(request.getParameter("idnews"));
        NewsService newsService=new NewsService();
        try {
            News news=newsService.GetNews(idnews);
            String temp=htmlToString(news.getContent());
            news.setContent(temp);
            request.setAttribute("news",news);

            request.getRequestDispatcher("EditNews.jsp").forward(request,response); //将实例转发给EditNews.jsp
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static String htmlToString(String str)
    {
        if(str==null) return "";
        if(str.equals("")) return "";
        str = str.replaceAll("&", "&");
        str = str.replaceAll("<", "&lt;");
        str = str.replaceAll(">", "&gt;");
        str = str.replaceAll("&amp;amp;", "&amp;");
        str = str.replaceAll("&amp;quot;", "&quot;");
        str = str.replaceAll("\"", "&quot;");
        str = str.replaceAll("&amp;lt;", "&lt;");
        str = str.replaceAll("&amp;gt;", "&gt;");
        str = str.replaceAll("&amp;nbsp;", "&nbsp;");
        return str;
    }
}

查看新闻:

@WebServlet(name = "ViewNewServlet")
public class ViewNewServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        int idnews=Integer.valueOf(request.getParameter("idnews"));
        NewsService newsService=new NewsService();
        try {
            News news=newsService.GetNews(idnews);
            request.setAttribute("news",news);
            request.getRequestDispatcher("NewsSpecific.jsp").forward(request,response);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

 

 码云地址:

 https://gitee.com/xmr123/JAVAEE/tree/feature-MVC2/

posted @ 2020-06-22 21:01  ♬♬♬♪♪♪  阅读(198)  评论(0编辑  收藏  举报