加载出发布文章页面核心代码为:
/** * 加载出新增文章页面 * * @param model * @param request * @param session * @return */ @RequestMapping(value = "/user/article/add", method = RequestMethod.GET) public String add(Model model, HttpServletRequest request, HttpSession session) { // 获取登录信息 User tempUser = (User) session.getAttribute(Const.SESSION_USER); String userId = tempUser.getUserId(); // 获取用户信息 User user = userService.getById(userId); // 构建专栏的查询条件 Map<String, Object> params = new HashMap<String, Object>(); params.put("creator", user.getUserId()); params.put("status", Group.STATUS_SUCCESS); List<Group> groups = groupService.list(new QueryWrapper<Group>().allEq(params).orderByDesc("createTime")); model.addAttribute("user", user); model.addAttribute("groups", groups); return Const.BASE_INDEX_PAGE + "blog/article/add"; }处理发布文章的核心代码为:
/** * 新增文章 * * @param request * @param session * @return */ @RequestMapping(value = "/user/article/add", method = RequestMethod.POST) @ResponseBody public Result add(HttpServletRequest request, HttpSession session) { Result result = new Result(); try { // 接收参数 String groupId = request.getParameter("groupId"); String title = request.getParameter("title"); String content = request.getParameter("content"); String tag = request.getParameter("tag"); String description = request.getParameter("description"); String typeStr = request.getParameter("type"); String canTopStr = request.getParameter("canTop"); String canCommentStr = request.getParameter("canComment"); // 校验参数 if (StringUtils.isEmpty(title) || StringUtils.isEmpty(content) || StringUtils.isEmpty(description)) { throw new TipException("缺少必要参数"); } int type = 0; int canTop = 0; int canComment = 1; try { type = Integer.parseInt(typeStr); canTop = Integer.parseInt(canTopStr); canComment = Integer.parseInt(canCommentStr); } catch (Exception e) { throw new TipException("参数类型错误"); } // 去html相关标签 description = StringUtil.replaceHtmlTags(description); // 客户端ip String ip = HttpUtil.getIpAddr(request); // 获取session中的用户信息 User tempUser = (User) session.getAttribute(Const.SESSION_USER); String userId = tempUser.getUserId(); // 封装文章信息 Article article = new Article(); article.setArticleId(IdGenarator.guid()); article.setGroupId(groupId); article.setTitle(title); article.setContent(content); article.setDescription(description); article.setType(type); article.setCanTop(canTop); article.setCanComment(canComment); article.setViewCount(0L); article.setGoodNum(0L); article.setBadNum(0L); article.setCreateTime(new Date()); article.setCreateIp(ip); article.setUserId(userId); // 保存文章 articleService.create(article, tag); result.setCode(Result.CODE_SUCCESS); result.setMsg("发布文章成功"); } catch (TipException e) { result.setCode(Result.CODE_EXCEPTION); result.setMsg(e.getMessage()); } catch (Exception e) { log.error("新增文章失败", e); result.setCode(Result.CODE_EXCEPTION); result.setMsg("新增文章失败"); } return result; }完整的发布文章页面如下:
以你最方便的方式关注我:
微信公众号: