MVC2项目实践

1.下载UEditor

MVC2项目实践

 

 

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

效果展示:

MVC2项目实践

 

 

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

 

 

 MVC2项目实践

 

可以看到上传成功

 

 MVC2项目实践

 

 

 

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

效果展示:

MVC2项目实践

 

 可以看到删除成功:

MVC2项目实践

 

 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;
    }
}

效果展示:

MVC2项目实践

 

将每一项进行简单修改:

MVC2项目实践

 

 

 可以看到修改成功:

MVC2项目实践

 

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

效果展示:

MVC2项目实践

 

 查看新闻:MVC2项目实践

 

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

MVC2项目实践

上一篇:[HTML+CSS] 好看的登录界面


下一篇:ios8 UITableView设置 setSeparatorInset:UIEdgeInsetsZero不起作用的解决办法(去掉15px空白间距)