MVC2 项目实践

项目效果:

登录:

MVC2 项目实践

 

 MVC2 项目实践

 

 登录后的新闻列表页面:

MVC2 项目实践

 

 添加新闻:

MVC2 项目实践

 

 MVC2 项目实践

 

编辑新闻: 

MVC2 项目实践

 

 查看新闻详情:

MVC2 项目实践

 

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

MVC2 项目实践

 

思路:

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

MVC2 项目实践

 

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

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

MVC2 项目实践

 

 添加新闻:

@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"); //进入新闻添加界面
    }
}

MVC2 项目实践

 

@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();
        }
    }
}

MVC2 项目实践

 

 码云地址:

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

MVC2 项目实践

上一篇:Servlet & HTTP & Request


下一篇:Spark Streaming揭秘 Day6 关于SparkStreaming Job的一些思考