码迷,mamicode.com
首页 > Web开发 > 详细

MVC2项目实践

时间:2020-06-24 23:58:49      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:java   turn   char   etc   forward   oid   protect   pre   lis   

1.下载UEditor

技术图片

 

 

 2.新闻上传

package Controller;

import Entity.News;
import Service.NewsService;
import Util.Category;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/*
新闻主页
 */
@WebServlet(name = "MainNewsServlet")
public class MainNewsServlet 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");
        NewsService newsService=new NewsService();
        try {
            List<News> lsNews=newsService.QueryNews();
            List<List<News>> lsNews_by_category= new ArrayList<>();
            for (int i = 0; i <Category.values().length ; i++) {
                List<News> temp=new ArrayList<>();
                lsNews_by_category.add(temp);
            }
            for (News news:lsNews) {
                if (news.getCategory().equals("图片新闻") && lsNews_by_category.get(0).size()<6)
                    lsNews_by_category.get(0).add(news);
                else if (news.getCategory().equals("学术交流") && lsNews_by_category.get(1).size()<6)
                    lsNews_by_category.get(1).add(news);
                else if (news.getCategory().equals("新闻速递") && lsNews_by_category.get(2).size()<6)
                    lsNews_by_category.get(2).add(news);
                else if (news.getCategory().equals("党建动态") && lsNews_by_category.get(3).size()<6)
                    lsNews_by_category.get(3).add(news);
                else if (news.getCategory().equals("通知公告") && lsNews_by_category.get(4).size()<6)
                    lsNews_by_category.get(4).add(news);
                else if (news.getCategory().equals("专题列表") && lsNews_by_category.get(5).size()<6)
                    lsNews_by_category.get(5).add(news);
            }
            request.setAttribute("lsNews_by_0",lsNews_by_category.get(0));
            request.setAttribute("lsNews_by_1",lsNews_by_category.get(1));
            request.setAttribute("lsNews_by_2",lsNews_by_category.get(2));
            request.setAttribute("lsNews_by_3",lsNews_by_category.get(3));
            request.setAttribute("lsNews_by_4",lsNews_by_category.get(4));
            request.setAttribute("lsNews_by_5",lsNews_by_category.get(5));
            request.getRequestDispatcher("SWPUnews.jsp").forward(request,response);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

效果展示:

技术图片

 

 

 简单填写一些数据,然后点击添加

 

 

 技术图片

 

可以看到上传成功

 

 技术图片

 

 

 

 3.新闻删除:

package Controller;

import Service.NewsService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/*
删除新闻
 */
@WebServlet(name = "DeleteServlet")
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);
    }
}

效果展示:

技术图片

 

 可以看到删除成功:

技术图片

 

 4.修改新闻:

package Controller;

import Entity.News;
import Service.NewsService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
/*
编辑新闻
 */
@WebServlet(name = "EditServlet")
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("AddNews.jsp").forward(request,response);
            request.getRequestDispatcher("EditNews.jsp").forward(request,response);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static String htmlToString(String str)
    {
        if(str==null) return "";
        if(str.equals("")) return "";
        str = str.replaceAll("&", "&amp;");
        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;
    }
}

效果展示:

技术图片

 

将每一项进行简单修改:

技术图片

 

 

 可以看到修改成功:

技术图片

 

 5.查看新闻:

package Controller;

import Entity.News;
import Service.NewsService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
/*
新闻列表
 */
@WebServlet(name = "Controller.ShowNewsListServlet")
public class ShowNewsListServlet 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");
        NewsService newsService=new NewsService();
        try {
            List<News> lsNews=newsService.QueryNews();
            request.setAttribute("lsNews",lsNews);
            request.getRequestDispatcher("ShowNewsList.jsp").forward(request,response);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

效果展示:

技术图片

 

 查看新闻:技术图片

 

 7.码云地址:https://gitee.com/chenxingxin123/WEB.git

MVC2项目实践

标签:java   turn   char   etc   forward   oid   protect   pre   lis   

原文地址:https://www.cnblogs.com/565118008a/p/13190403.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!