做毕设(十二)——JPA条件分页之翻页

来源:互联网 发布:java 代码混淆 编辑:程序博客网 时间:2024/06/05 16:21

一个网站几乎所有的数据都要分页,上一次做好了主页的点击浏览更多,而在编辑页面显然要用不同的方式。

<!DOCTYPE html><html lang="en" xmlns:th="http://www.w3.org/1999/xhtml"><head>    <meta charset="UTF-8">    <title>编辑专用</title>    <script src="http://how2j.cn/study/js/jquery/2.0.0/jquery.min.js"></script>    <link href="http://how2j.cn/study/css/bootstrap/3.3.6/bootstrap.min.css" rel="stylesheet">    <script src="http://how2j.cn/study/js/bootstrap/3.3.6/bootstrap.min.js"></script></head><body>    <form action="/edit/getnews">        <input class="btn btn-info" type="submit" value="爬取新闻并审核">    </form>    <br/>    <form action="/edit/switch">        <input class="btn btn-info" th:if="${session.switch==null}" type="submit" value="切换为已发布新闻">        <input class="btn btn-info" th:if="${session.switch!=null}" type="submit" value="切换为待审核新闻">    </form>    <hr/>    <p th:if="${getCount!=null}" th:text="'爬取了'+${getCount}+'条新新闻'"></p>    <p th:if="${newsCount!=null}" th:text="'有'+${newsCount}+'条新闻待编辑'"></p>    <nav>        <ul class="pagination">            <li><a th:href="@{/edit?pageNumber=0}">首页</a></li>            <li th:if="${isFirstPage}" class="disabled"><span>上一页</span></li>            <li th:if="${not isFirstPage}"><a th:href="@{'/edit?pageNumber='+${pageNum-1}}">上一页</a></li>            <li th:if="${isLastPage}" class="disabled"><span>下一页</span></li>            <li th:if="${not isLastPage}"><a th:href="@{'/edit?pageNumber='+${pageNum+1}}">下一页</a></li>            <li><a th:href="@{'/edit?pageNumber='+${totalPages}}">尾页</a></li>        </ul>    </nav>    <table style="table-layout:fixed" class="table table-condensed table-bordered table-hover table-striped">        <tr>            <th width="20%">标题</th>            <th width="6%">作者</th>            <th>内容</th>            <th width="14%">操作</th>        </tr>        <tr th:each="news:${editNews}">            <td th:text="${news.title}"></td>            <td th:text="${news.author}"></td>            <td style="overflow:hidden;white-space:nowrap;text-overflow:ellipsis;" th:text="${news.context}"></td>            <td valign="top">                <a th:href="@{'/edit/'+${news.id}+'?type=明星'}">明星</a>                <a th:href="@{'/edit/'+${news.id}+'?type=时尚'}">时尚</a>                <a th:href="@{'/edit/'+${news.id}+'?type=影视'}">影视</a>                <a th:href="@{'/edit/'+${news.id}+'?type=宠物'}">宠物</a>                <a th:href="@{'/edit/'+${news.id}+'?type=生活'}">生活</a>                <a th:href="@{'/edit/'+${news.id}+'?type=删除'}"><p class="text-danger">删除</p></a>            </td>        </tr>    </table></body></html>

因为编辑页面是单独的不展示的,而且没有需要自定义的样式,所有方便起见直接用bootstrap,真是方便。

@Controller@RequestMapping()public class EditController {    @Autowired    NewsService newsService;    @Autowired    CommentService commentService;    /**     * 管理员权限审核,所用方法都要加,以后换成AOP形式     * @param     * @return     */    public boolean isAdmin(HttpSession httpSession){        if(httpSession.getAttribute("user")==null){        }else{            User user = (User)httpSession.getAttribute("user");            if(user.getUsername().equals("admin"))                return true;        }        return true;//方便测试,应为false    }    /**     * 进入编辑页,显示数据     * @param     * @param     * @return     */    @RequestMapping(value = "/edit")    public String toEditPage(@RequestParam(value = "pageNumber",required = false)String pageNumber, HttpSession httpSession, ModelMap map){        if(isAdmin(httpSession)){            if(pageNumber==null ||"".equals(pageNumber)){                pageNumber="0";            }            int pageNum = Integer.parseInt(pageNumber);            List<News> editNews = null;            Page<News> newsPage = null;            if(httpSession.getAttribute("switch")==null) {                newsPage = newsService.getAllNewsTypeIsNull(pageNum,10);            }else {                newsPage = newsService.getAllNewsTypeIsNotNull(pageNum,10);            }            editNews = newsPage.getContent();            map.addAttribute("editNews", editNews);            map.addAttribute("newsCount",newsPage.getTotalElements());            map.addAttribute("totalPages",newsPage.getTotalPages()-1);            map.addAttribute("isFirstPage",newsPage.isFirst());            map.addAttribute("isLastPage",newsPage.isLast());            map.addAttribute("pageNum",pageNum);            return "edit";        }        return "error";    }    /**     * 切换审核还是编辑     * @param     * @return     */    @RequestMapping(value = "edit/switch",method = RequestMethod.GET)    public String switchPage(HttpSession httpSession){        if(isAdmin(httpSession)){            if(httpSession.getAttribute("switch")!=null) {                httpSession.setAttribute("switch",null);            }else {                httpSession.setAttribute("switch", "不为空");            }            return "redirect:/edit";        }        return "error";    }    /**     * 爬取新闻进入待审核状态     * @param     * @param     * @return     */    @RequestMapping(value = "edit/getnews",method = RequestMethod.GET)    public String getNews(HttpSession httpSession,ModelMap map){        if(isAdmin(httpSession)) {            // 定义即将访问的链接            String url = "http://www.huabian.com/";            // 访问链接并获取页面内容            String content = Spider.SendGet(url);            // 获取该页面的所有的新闻对象            ArrayList<News> newNews = Spider.GetNews(content);            // 打印结果            //System.out.println(newNews);            int count = 0;//记录抓取了多少条新数据            for(int i=0;i<newNews.size();i++){                count+=newsService.saveDifferentNews(newNews.get(i));            }            map.addAttribute("getCount",count);            List<News> editNews = newsService.getAllNewsByTypeIsNull();            map.addAttribute("editNews", editNews);            httpSession.setAttribute("switch","不为空");            return "edit";        }        return "error";    }    /**     * 对新闻设置类型(表示发布),或删除该id的新闻     * @param id 新闻id     * @param type 新闻类型     * @param     * @return     */    @RequestMapping(value = "edit/{id}",method = RequestMethod.GET)    public String checkNews(@PathVariable("id") Integer id,String type, HttpSession httpSession){        if(isAdmin(httpSession)){            News news = newsService.getNewsById(id);            if(type.equals("删除")){                newsService.delNews(news);                commentService.delAllbyNid(id.toString());            }else {                news.setType(type);                newsService.updateNews(news);            }            return "redirect:/edit/";        }        return "error";    }}

这里写图片描述