diff --git a/src/main/java/cc/ryanc/halo/config/WebMvcAutoConfiguration.java b/src/main/java/cc/ryanc/halo/config/WebMvcAutoConfiguration.java index f5961688a..7ef520eab 100644 --- a/src/main/java/cc/ryanc/halo/config/WebMvcAutoConfiguration.java +++ b/src/main/java/cc/ryanc/halo/config/WebMvcAutoConfiguration.java @@ -1,15 +1,16 @@ package cc.ryanc.halo.config; import cc.ryanc.halo.config.properties.HaloProperties; +import cc.ryanc.halo.factory.StringToEnumConverterFactory; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; +import org.springframework.format.FormatterRegistry; import org.springframework.web.servlet.LocaleResolver; import org.springframework.web.servlet.config.annotation.EnableWebMvc; -import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; @@ -32,7 +33,7 @@ import java.util.Locale; @PropertySource(value = "classpath:application.yaml", ignoreResourceNotFound = true, encoding = "UTF-8") public class WebMvcAutoConfiguration implements WebMvcConfigurer { -// @Autowired + // @Autowired // private LoginInterceptor loginInterceptor; // // @Autowired @@ -130,4 +131,9 @@ public class WebMvcAutoConfiguration implements WebMvcConfigurer { lci.setParamName("lang"); return lci; } + + @Override + public void addFormatters(FormatterRegistry registry) { + registry.addConverterFactory(new StringToEnumConverterFactory()); + } } diff --git a/src/main/java/cc/ryanc/halo/factory/StringToEnumConverterFactory.java b/src/main/java/cc/ryanc/halo/factory/StringToEnumConverterFactory.java new file mode 100644 index 000000000..7aa807a8b --- /dev/null +++ b/src/main/java/cc/ryanc/halo/factory/StringToEnumConverterFactory.java @@ -0,0 +1,35 @@ +package cc.ryanc.halo.factory; + +import org.springframework.core.convert.converter.Converter; +import org.springframework.core.convert.converter.ConverterFactory; +import org.springframework.stereotype.Component; + +/** + * @author : RYAN0UP + * @date : 2019/3/14 + */ +@Component +public class StringToEnumConverterFactory implements ConverterFactory { + + @SuppressWarnings("unchecked") + @Override + public Converter getConverter(Class targetType) { + return new StringToEnumConverter(targetType); + } + + private static class StringToEnumConverter + implements Converter { + + private Class enumType; + + private StringToEnumConverter(Class enumType) { + this.enumType = enumType; + } + + @SuppressWarnings("unchecked") + @Override + public T convert(String source) { + return (T) Enum.valueOf(this.enumType, source.toUpperCase()); + } + } +} diff --git a/src/main/java/cc/ryanc/halo/web/controller/admin/PostController.java b/src/main/java/cc/ryanc/halo/web/controller/admin/PostController.java index f7423e989..306ad5c24 100644 --- a/src/main/java/cc/ryanc/halo/web/controller/admin/PostController.java +++ b/src/main/java/cc/ryanc/halo/web/controller/admin/PostController.java @@ -27,7 +27,7 @@ import static org.springframework.data.domain.Sort.Direction.DESC; @RequestMapping(value = "/admin/posts") public class PostController { - private PostService postService; + private final PostService postService; public PostController(PostService postService) { this.postService = postService; @@ -45,18 +45,18 @@ public class PostController { */ @GetMapping public String posts(Model model, - @RequestParam(value = "status", defaultValue = "0") PostStatus status, + @RequestParam(value = "status", defaultValue = "published") PostStatus status, @RequestParam(value = "page", defaultValue = "0") Integer page, @SortDefault.SortDefaults({ - @SortDefault(sort = "postPriority", direction = DESC), - @SortDefault(sort = "postDate", direction = DESC) + @SortDefault(sort = "topPriority", direction = DESC), + @SortDefault(sort = "createTime", direction = DESC) }) Sort sort) { final Pageable pageable = PageRequest.of(page, 10, sort); final Page posts = postService.listByStatus(status, PostType.POST, pageable); model.addAttribute("posts", posts); - model.addAttribute("publishCount", postService.countByStatus(PostStatus.PUBLISHED, PostType.POST)); + model.addAttribute("publishedCount", postService.countByStatus(PostStatus.PUBLISHED, PostType.POST)); model.addAttribute("draftCount", postService.countByStatus(PostStatus.DRAFT, PostType.POST)); - model.addAttribute("trashCount", postService.countByStatus(PostStatus.RECYCLE, PostType.POST)); + model.addAttribute("recycleCount", postService.countByStatus(PostStatus.RECYCLE, PostType.POST)); model.addAttribute("status", status); return "admin/admin_post"; }