🎨 代码优化

pull/33/merge
ruibaby 2018-07-26 10:54:22 +08:00
parent da6ffd600d
commit 2cc45c353c
7 changed files with 33 additions and 18 deletions

View File

@ -282,11 +282,6 @@
<fork>true</fork> <fork>true</fork>
</configuration> </configuration>
</plugin> </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins> </plugins>
</build> </build>
</profile> </profile>

View File

@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data; import lombok.Data;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -33,11 +34,13 @@ public class Category implements Serializable {
/** /**
* *
*/ */
@NotBlank(message = "分类名称不能为空")
private String cateName; private String cateName;
/** /**
* *
*/ */
@NotBlank(message = "分类路径不能为空")
private String cateUrl; private String cateUrl;
/** /**

View File

@ -4,6 +4,8 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data; import lombok.Data;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import java.io.Serializable; import java.io.Serializable;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -41,11 +43,13 @@ public class Comment implements Serializable {
/** /**
* *
*/ */
@NotBlank(message = "评论用户名不能为空")
private String commentAuthor; private String commentAuthor;
/** /**
* *
*/ */
@Email(message = "邮箱格式不正确")
@JsonIgnore @JsonIgnore
private String commentAuthorEmail; private String commentAuthorEmail;
@ -73,6 +77,7 @@ public class Comment implements Serializable {
/** /**
* *
*/ */
@NotBlank(message = "评论内容不能为空")
@Lob @Lob
private String commentContent; private String commentContent;

View File

@ -7,6 +7,8 @@ import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.Table; import javax.persistence.Table;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import java.io.Serializable; import java.io.Serializable;
import java.util.Date; import java.util.Date;
@ -35,6 +37,7 @@ public class User implements Serializable {
/** /**
* *
*/ */
@NotBlank(message = "用户名不能为空")
@JsonIgnore @JsonIgnore
private String userName; private String userName;
@ -52,6 +55,7 @@ public class User implements Serializable {
/** /**
* *
*/ */
@Email(message = "邮箱格式不正确")
private String userEmail; private String userEmail;
/** /**

View File

@ -41,6 +41,7 @@ public class CategoryController {
*/ */
@PostMapping(value = "/save") @PostMapping(value = "/save")
public String saveCategory(@ModelAttribute Category category) { public String saveCategory(@ModelAttribute Category category) {
try { try {
categoryService.saveByCategory(category); categoryService.saveByCategory(category);
} catch (Exception e) { } catch (Exception e) {

View File

@ -9,9 +9,12 @@ import freemarker.template.Configuration;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import javax.validation.Valid;
/** /**
* @author : RYAN0UP * @author : RYAN0UP
@ -47,15 +50,16 @@ public class UserController {
*/ */
@PostMapping(value = "save") @PostMapping(value = "save")
@ResponseBody @ResponseBody
public JsonResult saveProfile(@ModelAttribute User user, HttpSession session) { public JsonResult saveProfile(@Valid @ModelAttribute User user, BindingResult result, HttpSession session) {
try { try {
if (null != user) { if (result.hasErrors()) {
userService.saveByUser(user); for (ObjectError error : result.getAllErrors()) {
configuration.setSharedVariable("user", userService.findUser()); return new JsonResult(ResultCode.FAIL.getCode(), error.getDefaultMessage());
session.invalidate(); }
} else {
return new JsonResult(ResultCode.FAIL.getCode(), "修改失败!");
} }
userService.saveByUser(user);
configuration.setSharedVariable("user", userService.findUser());
session.invalidate();
} catch (Exception e) { } catch (Exception e) {
log.error("修改用户资料失败:{}", e.getMessage()); log.error("修改用户资料失败:{}", e.getMessage());
return new JsonResult(ResultCode.FAIL.getCode(), "修改失败!"); return new JsonResult(ResultCode.FAIL.getCode(), "修改失败!");

View File

@ -24,9 +24,12 @@ import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -95,14 +98,14 @@ public class FrontCommentController {
*/ */
@PostMapping(value = "/newComment") @PostMapping(value = "/newComment")
@ResponseBody @ResponseBody
public JsonResult newComment(@ModelAttribute("comment") Comment comment, public JsonResult newComment(@Valid @ModelAttribute("comment") Comment comment,
BindingResult result,
@ModelAttribute("post") Post post, @ModelAttribute("post") Post post,
HttpServletRequest request) { HttpServletRequest request) {
if (StringUtils.equals(StringUtils.trim(comment.getCommentAuthor()), "")) { if (result.hasErrors()) {
return new JsonResult(ResultCode.FAIL.getCode(), "请正确输入昵称!"); for (ObjectError error : result.getAllErrors()) {
} return new JsonResult(ResultCode.FAIL.getCode(), error.getDefaultMessage());
if (StringUtils.equals(StringUtils.trim(comment.getCommentContent()), "")) { }
return new JsonResult(ResultCode.FAIL.getCode(), "请正确输入评论内容!");
} }
try { try {
Comment lastComment = null; Comment lastComment = null;