Java项目:新闻管理系统(java+javaweb+jdbc)

news/2024/5/18 21:43:06 标签: java, javaweb, jdbc

源码获取:博客首页 "资源" 里下载!

功能介绍:

登录、注册、新闻发布、新闻管理、公告发布、用户管理、退出登录

 

 

品类控制层:

@Controller
@RequestMapping("CategoryServlet")
public class CategoryController {
    @Autowired
    private HttpServletRequest request;
    private CategoryDao categoryDao=new CategoryDao();

    @RequestMapping("/listforadmin")
    public String listforadmin(){
        List<Category> categoryList = categoryDao.queryAll();
        request.setAttribute("list", categoryList);
        return "listcategory";
    }
    @RequestMapping("/show")
    public String show(){
        List<Category> categoryList = categoryDao.queryAll();
        categoryList=categoryList.stream().filter(x->x.getState().equals("1")).collect(Collectors.toList());

        if(categoryList.size()>=5)
        {

            request.setAttribute("list", categoryDao.queryAll());
            request.setAttribute("msg","设置栏目显示失败,前台栏目最多显示5个");
            return "listcategory";
        }else {
            categoryDao.show(Integer.parseInt(request.getParameter("id")));
            return "redirect:listforadmin";
        }
    }
    @RequestMapping("/hidden")
    public String hidden(){

        categoryDao.hidden(Integer.parseInt(request.getParameter("id")));
        return "redirect:listforadmin";
    }
    @RequestMapping("/add")
    public String add(){
        String name=request.getParameter("name");
        String state=request.getParameter("state");
        Category category=new Category();
        category.setName(name);
        category.setState(state);
        categoryDao.save(category);
        return "redirect:listforadmin";
    }



}

 

新闻控制层:

@Controller
@RequestMapping("NewsServlet")
public class NewsController {
    @Autowired
    private HttpServletRequest request;
    private NewsDao newsDao = new NewsDao();
    private CategoryDao categoryDao = new CategoryDao();
    private CommentsDao commentsDao = new CommentsDao();
    @RequestMapping("/publish")
    public String publish(){
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        String categoryid = request.getParameter("categoryid");

        Users users = (Users) request.getSession().getAttribute("loginUsers");
        Integer usersId = users.getId();

        Integer deptid = users.getDeptid();

        News news = new News();
        news.setCategoryid(Integer.parseInt(categoryid));
        news.setClicks(0);
        news.setContent(content);
        news.setPbdate(new Date());
        news.setPbdeptid(deptid);
        news.setPublisher(usersId);
        news.setCategoryid(Integer.parseInt(categoryid));
        news.setTitle(title);
        newsDao.save(news);
        return "redirect:listforadmin";
    }


    @RequestMapping("/listforadmin")
    public String listforadmin(){
        List<News> newsList = newsDao.queryAll();
        request.setAttribute("list", newsList);
        return "listnews";
    }





    @RequestMapping("/toaddnews")
    public String toaddnews(){
        List<News> newsList = newsDao.queryAll();
        request.setAttribute("list", newsList);
        List<Category> categoryList = categoryDao.queryAll();
        request.setAttribute("categoryList", categoryList);
        return "addnews";
    }


    @RequestMapping("/edit")
    public String edit(){
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        String categoryid = request.getParameter("categoryid");

        News news = new News();
        news.setCategoryid(Integer.parseInt(categoryid));
        news.setContent(content);
        news.setPbdate(new Date());
        news.setId(Integer.parseInt(request.getParameter("id")));

        news.setTitle(title);
        newsDao.edit(news);
        return "redirect:listforadmin";
    }



    @RequestMapping("/del")
    public String del(){
        newsDao.deleteById(Integer.parseInt(request.getParameter("id")));
        List<News> newsList = newsDao.queryAll();
        request.setAttribute("list", newsList);
        return "listnews";

    }


    @RequestMapping("/toeditnews")
    public String toeditnews(){
        News news = newsDao.queryByid(Integer.parseInt(request.getParameter("id")));
        request.setAttribute("v", news);
        List<Category> categoryList = categoryDao.queryAll();
        request.setAttribute("categoryList", categoryList);
        return "editnews";
    }


    @RequestMapping("/queryByType")
    public String queryByType(){
        String categoryid = request.getParameter("categoryid");

        List<News> newsList = newsDao.queryAll();
        request.setAttribute("list", newsList.stream()
                .filter(x -> x.getCategoryid().equals(Integer.parseInt(categoryid))).collect(Collectors.toList()));
        List<Category> categoryList = categoryDao.queryAll();
        request.setAttribute("categoryList",
                categoryList.stream().filter(x -> x.getState().equals("1")).collect(Collectors.toList()));

        return "typenews";
    }


    @RequestMapping("/detail")
    public String detail(){
        //测试
        String id =  request.getParameter("id");
        if(id.contains(".jsp"))
        {
           id= request.getParameter("id").replaceAll(".jsp","");

        }

        News news = newsDao.queryByid(Integer.parseInt(id));

        news.setClicks(news.getClicks() + 1);

        newsDao.setClicksIncrement(news);

        NewsDetail detail = new NewsDetail();
        detail.setNews(news);

        Category category = categoryDao.queryById(news.getCategoryid());

        detail.setCategory(category);

        String content = news.getContent();
        int length = content.length();

        length = length / 60;

        length = length * 27 + 10;

        request.setAttribute("length", length);

        request.setAttribute("v", detail);

        List<Category> categoryList = categoryDao.queryAll();

        List<Comments> commentsList = commentsDao.getByNewsId(news.getId());
        request.setAttribute("commentsList", commentsList);

        request.setAttribute("contentstr", content);

        request.setAttribute("categoryList",
                categoryList.stream().filter(x -> x.getState().equals("1")).collect(Collectors.toList()));

        return "detailnews";
    }

    @RequestMapping("/search")
    public String search(){
        String search = request.getParameter("search");

        request.setAttribute("searchStr", search);

        List<News> newsList = newsDao.queryAll();
        newsList=newsList.stream().filter(x -> x.getTitle().contains(search)).collect(Collectors.toList());

        newsList=	newsList.stream().map(x->{

            String title = x.getTitle();

            /*			String[] split = title.split(search);
             */
			/*if(split.length>=2){
				for(int i=0;i<split.length-1;i++){
			title=split[i]+"<font color='red'>"+search+"</font>"+split[i+1];

				}
			}else{
				title=split[0]+"<font color='red'>"+search+"</font>";
			}*/
            title=title.replace(search, "<font color='red'>"+search+"</font>");


            System.out.println(title);

            x.setTitle(title);

            return x;
        }).collect(Collectors.toList());

        request.setAttribute("list",newsList
        );
        List<Category> categoryList = categoryDao.queryAll();
        request.setAttribute("categoryList",
                categoryList.stream().filter(x -> x.getState().equals("1")).collect(Collectors.toList()));

        return "searchnews";
    }

}

源码获取:博客首页 "资源" 里下载!


http://www.niftyadmin.cn/n/1554396.html

相关文章

Java项目:学生选课系统(java+javaweb+jdbc)

源码获取&#xff1a;博客首页 "资源" 里下载&#xff01; 功能介绍&#xff1a; 用户菜单、学生管理、教师管理、课程管理、成绩排名查询 学生管理控制层&#xff1a; Controller RequestMapping("/student") public class StudentController {private …

mac android命令行工具,在Mac上安装CMake命令行工具(Installing CMake command line tools on a Mac)...

在Mac上安装CMake命令行工具(Installing CMake command line tools on a Mac)我两天前通过GUI安装了CMake&#xff0c;没有任何问题&#xff0c;并选择“安装命令行工具”。 我刚买了一台全新的Macbook&#xff0c;并试图再次安装CMake&#xff0c;但该选项已更改为“如何安装命…

Java项目:人事管理系统(java+javaweb+jdbc)

源码获取&#xff1a;博客首页 "资源" 里下载&#xff01; 功能介绍&#xff1a; 登录、新增、修改、离职 员工管理控制层&#xff1a; Controller RequestMapping("/employee") public class EmployeeController {Autowiredprivate IEmployeeService em…

android动态设置attr值,Android,如何在代码中获取attr属性的值

获取arrt的值有时候我们需要把颜色&#xff0c;数值写成attr属性&#xff0c;这样做是为了屏蔽开发者对应具体数值&#xff0c;比如我们需要设置不同主题下的主色&#xff0c;副色&#xff0c;或者是不同版本的ActionBar大小&#xff0c;亦或者是不同Dpi下的DrawerLayout的宽度…

Java项目:植物大战僵尸(java+swing)

源码获取&#xff1a;博客首页 "资源" 里下载&#xff01; 功能简介&#xff1a; 植物大战僵尸、冒险模式、生存模式、解谜模式 小车服务类&#xff1a; public class CarThread extends Thread{private boolean flagtrue;private int x;private int y;private JL…

android studio 测试工具,Android:Studio性能监测工具

Android Studio 内置了四种性能监测工具Memory Monitor、Network Monitor、CPU Monitor、GPU Monitor&#xff0c;我们可以使用这些工具监测APP的状态&#xff0c;该文简单介绍下这些工具的使用imageA&#xff1a;手动触发GC操作B&#xff1a;获取当前的堆栈信息&#xff0c;生…

Java项目:贪吃蛇游戏(java+swing)

源码获取&#xff1a;博客首页 "资源" 里下载&#xff01; 功能简介&#xff1a; 贪吃蛇游戏 大嘴鱼洁面类。完成大嘴鱼的界面的绘制: /*** 大嘴鱼洁面类。完成大嘴鱼的界面的绘制。*/ public class BigMouthFishFrame extends JFrame{private FishPool pool null;…

android file只能点击一次,Android必备知识点- Android文件(File)操作

Android 使用与其他平台上基于磁盘的文件系统类似的文件系统。本文讲述如何使用 Android 文件系统通过 File API 读取和写入文件。File 对象适合按照从开始到结束的顺序不跳过地读取或写入大量数据。 例如&#xff0c;它适合于图片文件或通过网络交换的任何内容。本文展示如何在…