Skip to content

Commit

Permalink
增加freemarker自定义标签,修复生成头像跟上传文件不存在的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
atjiu committed Jun 6, 2017
1 parent 8dad137 commit 5b2933c
Show file tree
Hide file tree
Showing 18 changed files with 349 additions and 202 deletions.
2 changes: 1 addition & 1 deletion pybbs.sql
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ VALUES (1, 7), (2, 7), (1, 8), (2, 8), (1, 9), (2, 9), (1, 10), (2, 10), (1, 11)
INSERT INTO `pybbs_section` VALUES (1, '分享'), (4, '博客'), (5, '招聘'), (3, '新闻'), (2, '问答');

INSERT INTO `pybbs_user` VALUES
(1, '5', null, 'http://localhost:8080/static/images/upload/avatar/default.png', '\0', '[email protected]', '2016-09-09 09:50:14',
(1, '0', null, 'http://localhost:8080/static/images/upload/avatar/default.png', '\0', '[email protected]', '2016-09-09 09:50:14',
'$2a$10$KkUG107R3ASTHfAHei.bweXWXgCa4cE1KhK.F0odzfE0r97aeeTXC', 'hello world',
'd20b9a5c-8693-41a6-8943-ddb2cb78eebd', 'https://tomoya92.github.io', 'tomoya');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import javax.annotation.PostConstruct;

import cn.tomoya.common.config.security.SpringSecurityTag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -25,6 +26,12 @@ public class FreemarkerConfig {
private SpringSecurityTag springSecurityTag;
@Autowired
private SectionService sectionService;
@Autowired
private UserTopicDirective userTopicDirective;
@Autowired
private UserReplyDirective userReplyDirective;
@Autowired
private UserCollectDirective userCollectDirective;

@PostConstruct
public void setSharedVariable() throws TemplateModelException {
Expand All @@ -33,6 +40,11 @@ public void setSharedVariable() throws TemplateModelException {
configuration.setSharedVariable("site", siteConfig);
// 将版块注入到全局freemarker变量里
configuration.setSharedVariable("sections", sectionService.findAll());

configuration.setSharedVariable("user_topics_tag", userTopicDirective);
configuration.setSharedVariable("user_replies_tag", userReplyDirective);
configuration.setSharedVariable("user_collects_tag", userCollectDirective);

log.info("init freemarker sharedVariables {site} success...");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cn.tomoya.common.config.freemarker;

import cn.tomoya.module.collect.entity.Collect;
import cn.tomoya.module.collect.service.CollectService;
import cn.tomoya.module.user.entity.User;
import cn.tomoya.module.user.service.UserService;
import freemarker.core.Environment;
import freemarker.template.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

/**
* Created by tomoya on 17-6-6.
*/
@Component
public class UserCollectDirective implements TemplateDirectiveModel {

@Autowired
private UserService userService;
@Autowired
private CollectService collectService;

@Override
public void execute(Environment environment, Map map, TemplateModel[] templateModels,
TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException {
Page<Collect> page = new PageImpl<>(new ArrayList<>());
if (map.containsKey("username") && map.get("username") != null) {
String username = map.get("username").toString();
if (map.containsKey("p")) {
int p = map.get("p") == null ? 1 : Integer.parseInt(map.get("p").toString());
int limit = Integer.parseInt(map.get("limit").toString());
User currentUser = userService.findByUsername(username);
if (currentUser != null) {
page = collectService.findByUser(p, limit, currentUser);
}
}
}
DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25);
environment.setVariable("page", builder.build().wrap(page));
templateDirectiveBody.render(environment.getOut());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cn.tomoya.common.config.freemarker;

import cn.tomoya.module.reply.entity.Reply;
import cn.tomoya.module.reply.service.ReplyService;
import cn.tomoya.module.user.entity.User;
import cn.tomoya.module.user.service.UserService;
import freemarker.core.Environment;
import freemarker.template.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

/**
* Created by tomoya on 17-6-6.
*/
@Component
public class UserReplyDirective implements TemplateDirectiveModel {

@Autowired
private UserService userService;
@Autowired
private ReplyService replyService;

@Override
public void execute(Environment environment, Map map, TemplateModel[] templateModels,
TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException {
Page<Reply> page = new PageImpl<>(new ArrayList<>());
if (map.containsKey("username") && map.get("username") != null) {
String username = map.get("username").toString();
if (map.containsKey("p")) {
int p = map.get("p") == null ? 1 : Integer.parseInt(map.get("p").toString());
int limit = Integer.parseInt(map.get("limit").toString());
User currentUser = userService.findByUsername(username);
if (currentUser != null) {
page = replyService.findByUser(p, limit, currentUser);
}
}
}
DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25);
environment.setVariable("page", builder.build().wrap(page));
templateDirectiveBody.render(environment.getOut());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cn.tomoya.common.config.freemarker;

import cn.tomoya.module.topic.entity.Topic;
import cn.tomoya.module.topic.service.TopicService;
import cn.tomoya.module.user.entity.User;
import cn.tomoya.module.user.service.UserService;
import freemarker.core.Environment;
import freemarker.template.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

/**
* Created by tomoya on 17-6-6.
*/
@Component
public class UserTopicDirective implements TemplateDirectiveModel {

@Autowired
private UserService userService;
@Autowired
private TopicService topicService;

@Override
public void execute(Environment environment, Map map, TemplateModel[] templateModels,
TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException {
Page<Topic> page = new PageImpl<>(new ArrayList<>());
if (map.containsKey("username") && map.get("username") != null) {
String username = map.get("username").toString();
if (map.containsKey("p")) {
int p = map.get("p") == null ? 1 : Integer.parseInt(map.get("p").toString());
int limit = Integer.parseInt(map.get("limit").toString());
User currentUser = userService.findByUsername(username);
if (currentUser != null) {
page = topicService.findByUser(p, limit, currentUser);
}
}
}
DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25);
environment.setVariable("page", builder.build().wrap(page));
templateDirectiveBody.render(environment.getOut());
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.tomoya.common.config.freemarker;
package cn.tomoya.common.config.security;

import java.util.Collection;
import java.util.HashSet;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,5 @@
package cn.tomoya.module.index.controller;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.tomoya.common.BaseController;
import cn.tomoya.common.config.SiteConfig;
import cn.tomoya.exception.Result;
Expand All @@ -39,6 +11,26 @@
import cn.tomoya.util.FileUtil;
import cn.tomoya.util.PageWrapper;
import cn.tomoya.util.identicon.Identicon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

/**
* Created by tomoya.
Expand Down Expand Up @@ -150,7 +142,7 @@ public String register(String username, String password, HttpServletResponse res
user.setBlock(false);
user.setToken(UUID.randomUUID().toString());
user.setAvatar(siteConfig.getStaticUrl() + "avatar/" + avatarName + ".png");
user.setAttempts(siteConfig.getAttempts());
user.setAttempts(0);
userService.save(user);
return redirect(response, "/login?s=reg");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
@Table(name = "pybbs_permission")
public class Permission extends BaseEntity implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue
private int id;
Expand Down
43 changes: 13 additions & 30 deletions src/main/java/cn/tomoya/module/user/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
package cn.tomoya.module.user.controller;

import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import cn.tomoya.common.BaseController;
import cn.tomoya.module.collect.service.CollectService;
import cn.tomoya.module.user.entity.User;
import cn.tomoya.module.user.service.UserService;
import cn.tomoya.util.FileUploadEnum;
import cn.tomoya.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import cn.tomoya.common.BaseController;
import cn.tomoya.common.config.SiteConfig;
import cn.tomoya.module.collect.service.CollectService;
import cn.tomoya.module.reply.service.ReplyService;
import cn.tomoya.module.topic.service.TopicService;
import cn.tomoya.module.user.entity.User;
import cn.tomoya.module.user.service.UserService;
import cn.tomoya.util.FileUploadEnum;
import cn.tomoya.util.FileUtil;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;

/**
* Created by tomoya.
Expand All @@ -36,12 +27,6 @@
@RequestMapping("/user")
public class UserController extends BaseController {

@Autowired
private SiteConfig siteConfig;
@Autowired
private TopicService topicService;
@Autowired
private ReplyService replyService;
@Autowired
private UserService userService;
@Autowired
Expand All @@ -63,8 +48,6 @@ public String profile(@PathVariable String username, Model model) {
model.addAttribute("user", getUser());
model.addAttribute("currentUser", currentUser);
model.addAttribute("collectCount", collectService.countByUser(currentUser));
model.addAttribute("topicPage", topicService.findByUser(1, 7, currentUser));
model.addAttribute("replyPage", replyService.findByUser(1, 7, currentUser));
model.addAttribute("pageTitle", currentUser.getUsername() + " 个人主页");
} else {
model.addAttribute("pageTitle", "用户未找到");
Expand All @@ -83,7 +66,7 @@ public String topics(@PathVariable String username, Integer p, HttpServletRespon
User currentUser = userService.findByUsername(username);
if (currentUser != null) {
model.addAttribute("currentUser", currentUser);
model.addAttribute("page", topicService.findByUser(p == null ? 1 : p, siteConfig.getPageSize(), currentUser));
model.addAttribute("p", p);
return render("/user/topics");
} else {
return renderText(response, "用户不存在");
Expand All @@ -100,7 +83,7 @@ public String replies(@PathVariable String username, Integer p, HttpServletRespo
User currentUser = userService.findByUsername(username);
if (currentUser != null) {
model.addAttribute("currentUser", currentUser);
model.addAttribute("page", replyService.findByUser(p == null ? 1 : p, siteConfig.getPageSize(), currentUser));
model.addAttribute("p", p);
return render("/user/replies");
} else {
return renderText(response, "用户不存在");
Expand All @@ -118,7 +101,7 @@ public String collects(@PathVariable String username, Integer p, HttpServletResp
User currentUser = userService.findByUsername(username);
if (currentUser != null) {
model.addAttribute("currentUser", currentUser);
model.addAttribute("page", collectService.findByUser(p == null ? 1 : p, siteConfig.getPageSize(), currentUser));
model.addAttribute("p", p);
return render("/user/collects");
} else {
return renderText(response, "用户不存在");
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/cn/tomoya/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ public String uploadFile(MultipartFile file, FileUploadEnum fileUploadEnum) thro
BufferedOutputStream stream = null;
String requestPath = null;
if (fileUploadEnum == FileUploadEnum.FILE) {
File file_dir = new File(siteConfig.getUploadPath());
if(!file_dir.exists()) file_dir.mkdirs();
stream = new BufferedOutputStream(new FileOutputStream(new File(siteConfig.getUploadPath() + fileName)));
requestPath = siteConfig.getStaticUrl();
} else if (fileUploadEnum == FileUploadEnum.AVATAR) {
File file_dir = new File(siteConfig.getUploadPath() + "avatar/");
if(!file_dir.exists()) file_dir.mkdirs();
stream = new BufferedOutputStream(
new FileOutputStream(new File(siteConfig.getUploadPath() + "avatar/" + fileName)));
requestPath = siteConfig.getStaticUrl() + "avatar/";
Expand Down
Loading

0 comments on commit 5b2933c

Please sign in to comment.