做毕设(十三)——改首页,设置类型
来源:互联网 发布:芭比娃娃淘宝 编辑:程序博客网 时间:2024/06/07 19:47
刚开始版面是照着CSDN极客头条做的,后来发现是做娱乐新闻网站,明显不适合了,就改一下。
效果图
在新闻实体类中加入img(预览图),preview(简介)
写个工具类
/** * 预览图文工具 */public class PreviewDeal { public List<News> Deal(List<News> newsList){ for(int i=0;i<newsList.size();i++){ Pattern imgPattern = Pattern.compile("(<img.*?\"(>| />))"); Matcher imgMatcher = imgPattern.matcher(newsList.get(i).getContext()); if(imgMatcher.find()){ newsList.get(i).setImg(imgMatcher.group(1)); }else { newsList.get(i).setImg("<img src=\"/image/logo.png\"/>"); } Pattern prePattern = Pattern.compile("(?<=<p>)(.*?)(?=</p>)"); Matcher preMatcher = prePattern.matcher(newsList.get(i).getContext()); if(preMatcher.find()){ newsList.get(i).setPreview(preMatcher.group()); }else { newsList.get(i).setPreview("点击查看详情。"); } } return newsList; }}
/** * 初始获取20条新闻 * @param * @return */ @RequestMapping(value = "",method = RequestMethod.GET) public String showNews(ModelMap map) { Page<News> newsPage = newsService.getAllNewsTypeIsNotNull(0,20);// List<News> newsList = newsPage.getContent(); PreviewDeal previewDeal = new PreviewDeal();//预览图文工具 List<News> newsList = previewDeal.Deal(newsPage.getContent());//设置预览图文 map.addAttribute("News",newsList); int totalPages = newsPage.getTotalPages()-1; map.addAttribute("totalPages",totalPages); return "index"; }
这里要提一下,我抓的网站是花边新闻,他的新闻内容是这样的
<p ><img src="//p0.ifengimg.com/a/2017_39/c2065a8ffc2705a_size80_w565_h531.jpg" alt="" /></p><p ><span>刘涛与老公王珂</span></p><p>凤凰网娱乐讯 近日,刘涛与老公王珂在云南丽江参加综艺节目录制,刘涛多次被拍到素颜逛街、前往菜市场买菜,十分随性。</p><p ><img src="//p2.ifengimg.com/a/2017_39/c6adead6cc63488_size71_w569_h555.jpg" alt="" /></p><p ><span>刘涛与老公王珂</span></p><p>9月25日,又有网友在云南丽江泸沽湖景区偶遇刘涛和王珂,并拍照记录下偶遇瞬间。照片中,刘涛头戴黑色帽子,上身穿牛仔外套,下身穿条纹裙子;刘涛老公王珂则头戴灰色鸭舌帽,上身穿灰色T恤,下身穿黑色休闲裤。两人穿着随性走在大街上,走路动作神同步,十分默契。夫妻二人同框很是低调,也很悠闲甜蜜。</p>
注意到正文的段落标签是<p>
的,而其他的是<p >
的,多了个空格,所以(?<=<p>)(.*?)(?=</p>)
可以直接抓到正文而不会抓到图片,学到了一招。
我的新闻推荐是根据关键字来的,所以要先提取新闻的关键字。找个工具包。
HanLP是由一系列模型与算法组成的Java工具包,目标是普及自然语言处理 在生产环境中的应用。HanLP具备功能完善、性能高效、架构清晰、语料时 新、可自定义的特点。
https://github.com/hankcs/HanLP
newsPage = newsService.getAllNewsTypeIsNull(pageNum,10);editNews = newsPage.getContent();//抓关键字for(int i=0;i<editNews.size();i++){ String content = "";//储存标题和无图正文 Pattern txtPattern = Pattern.compile("[\\u4e00-\\u9fa5]");//中文字符 Matcher txtMatcher = txtPattern.matcher(editNews.get(i).getContext()); content += editNews.get(i).getTitle(); while(txtMatcher.find()){ content += txtMatcher.group(0); } //System.out.println(content); List<String> keywordList = HanLP.extractKeyword(content,5);//HandLP工具包,找关键词 String keyword = StringUtils.collectionToDelimitedString(keywordList,",");//List转化为以逗号分隔的字符串 editNews.get(i).setType(keyword);}
<script type="text/javascript"> /*<![CDATA[*/ $(document).ready(function(){ $(".tag").bind("input propertychange","textarea",function(){ var tag = $(this).val(); var alist = $(this).parent().next().children(); $.each(alist,function () { $(this).attr("href",$(this).next().val() +tag); }) }); }); /*]]>*/ </script> <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="20%">TAG(用英文“,”间隔多类型)</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><textarea class="tag" style="resize: none;width:100%;" th:text="${news.type}"></textarea></td> <td th:if="${session.switch==null}" valign="top"> <a th:href="@{'/edit/'+${news.id}+'?type=明星,'+${news.type}}">明星</a> <input type="hidden" th:attr="value='/edit/'+${news.id}+'?type=明星,'"> <a th:href="@{'/edit/'+${news.id}+'?type=时尚,'+${news.type}}">时尚</a> <input type="hidden" th:attr="value='/edit/'+${news.id}+'?type=时尚,'"> <a th:href="@{'/edit/'+${news.id}+'?type=影视,'+${news.type}}">影视</a> <input type="hidden" th:attr="value='/edit/'+${news.id}+'?type=影视,'"> <a th:href="@{'/edit/'+${news.id}+'?type=宠物,'+${news.type}}">宠物</a> <input type="hidden" th:attr="value='/edit/'+${news.id}+'?type=宠物,'"> <a th:href="@{'/edit/'+${news.id}+'?type=生活,'+${news.type}}">生活</a> <input type="hidden" th:attr="value='/edit/'+${news.id}+'?type=生活,'"> <a th:href="@{'/edit/'+${news.id}+'?type=删除'}"><p class="text-danger">删除</p></a> <input type="hidden" th:attr="value='/edit/'+${news.id}+'?type=删除,'"> </td> <td th:if="${session.switch!=null}" valign="top"> <a th:href="@{'/edit/'+${news.id}+'?type='+${news.type}}">更改</a> <input type="hidden" th:attr="value='/edit/'+${news.id}+'?type='"> <a th:href="@{'/edit/'+${news.id}+'?type=删除'}"><p class="text-danger">删除</p></a> <input type="hidden" th:attr="value='/edit/'+${news.id}+'?type=删除,'"> </td> </tr> </table>
这里是修改文本域中的值,会直接影响超链接的传参。input用来放id和基本类型,不然a一改就变了,input可以保持前面不变只改后面加的类型。如果用点击是在获取文本域中的值组合超链接可能不会这么麻烦,不过写都写了。
之后做完推荐就完成了,大概。
阅读全文
0 0
- 做毕设(十三)——改首页,设置类型
- Redis源码分析(十三)——集合类型t_set
- JavaScript程序开发(十三)—RegExp类型
- 类型不同的站点,首页这么设置才能吸引人!
- word2007页面设置——首页不编码
- js脚本——添加收藏、设置首页
- 设置首页
- 首页设置
- 首页设置
- 菜鸟教程之工具使用(十三)——设置Eclipse文件默认打开方式
- php学习笔记(十三)类型转换
- IIS7 设置网站默认主页(首页)
- 首页——个人简介
- jmeter(二十三)-关联设置
- Scrapy爬虫入门教程十三 Settings(设置)
- Oracle12C--设置数据层次(十三)
- 网站开发(十三)首页文章循环显示和显示文章内容
- JavaScript FAQ(十三)——图片
- LeetCode-91. Decode Ways
- 使用Spring Session和Redis解决分布式Session跨域共享问题
- python2.6对json字符串有序的解析成有序的字典
- ASP.net本质论之用控制台应用程序创建Asp.net服务器
- 基于MYCAT的MYSQL主从与读写分离配置详解与示例
- 做毕设(十三)——改首页,设置类型
- android-studio报错Error:Execution failed for task
- type命令
- js Array.prototype.reduce()
- matlab 图像灰度级转[0,1], Normalize处理, 提升/降低所有元素若干倍, .^的运用(点乘与乘)
- vs2013 编译问题c++
- iptables之NAT端口转发设置
- 简单BP神经网络分类手写数字识别0-9
- JAVA进程诡异消失,oom killer干的