/** * 设置关键字高亮 * * @param query 查询对象 * @param list 设置高亮的内容列表 * @param subLength 截取长度 * @param fields 字段名 */ public List<T> keywordsHighlight( BooleanQuery query, List<T> list, int subLength, String... fields) { Analyzer analyzer = new IKAnalyzer(); Formatter formatter = new SimpleHTMLFormatter("<span class=\"highlight\">", "</span>"); Highlighter highlighter = new Highlighter(formatter, new QueryScorer(query)); highlighter.setTextFragmenter(new SimpleFragmenter(subLength)); for (T entity : list) { try { for (String field : fields) { String text = StringUtils.replaceHtml((String) Reflections.invokeGetter(entity, field)); // 设置高亮字段 String description = highlighter.getBestFragment(analyzer, field, text); if (description != null) { Reflections.invokeSetter(entity, fields[0], description); break; } Reflections.invokeSetter(entity, fields[0], StringUtils.abbr(text, subLength * 2)); } } catch (IOException e) { e.printStackTrace(); } catch (InvalidTokenOffsetsException e) { e.printStackTrace(); } } return list; }
/** 通过编号获取内容标题 */ public List<Object[]> findByIds(String ids) { List<Object[]> list = Lists.newArrayList(); Long[] idss = (Long[]) ConvertUtils.convert(StringUtils.split(ids, ","), Long.class); if (idss.length > 0) { List<Link> l = linkDao.findByIdIn(idss); for (Link e : l) { list.add(new Object[] {e.getId(), StringUtils.abbr(e.getTitle(), 50)}); } } return list; }
@RequiresPermissions("cms:article:edit") @RequestMapping(value = "save") public String save(Article article, Model model, RedirectAttributes redirectAttributes) { if (!beanValidator(model, article)) { return form(article, model); } articleService.save(article); addMessage(redirectAttributes, "保存文章'" + StringUtils.abbr(article.getTitle(), 50) + "'成功"); String categoryId = article.getCategory() != null ? article.getCategory().getId() : null; return "redirect:" + adminPath + "/cms/article/?repage&category.id=" + (categoryId != null ? categoryId : ""); }