config StringToEnumConverterFactory

pull/137/head
ruibaby 2019-03-14 17:49:55 +08:00
parent c9c68eef54
commit 2a71ba67b2
3 changed files with 49 additions and 8 deletions

View File

@ -1,15 +1,16 @@
package cc.ryanc.halo.config; package cc.ryanc.halo.config;
import cc.ryanc.halo.config.properties.HaloProperties; import cc.ryanc.halo.config.properties.HaloProperties;
import cc.ryanc.halo.factory.StringToEnumConverterFactory;
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.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource; import org.springframework.context.annotation.PropertySource;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.LocaleResolver; import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; 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.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; 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") @PropertySource(value = "classpath:application.yaml", ignoreResourceNotFound = true, encoding = "UTF-8")
public class WebMvcAutoConfiguration implements WebMvcConfigurer { public class WebMvcAutoConfiguration implements WebMvcConfigurer {
// @Autowired // @Autowired
// private LoginInterceptor loginInterceptor; // private LoginInterceptor loginInterceptor;
// //
// @Autowired // @Autowired
@ -130,4 +131,9 @@ public class WebMvcAutoConfiguration implements WebMvcConfigurer {
lci.setParamName("lang"); lci.setParamName("lang");
return lci; return lci;
} }
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverterFactory(new StringToEnumConverterFactory());
}
} }

View File

@ -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<String, Enum> {
@SuppressWarnings("unchecked")
@Override
public <T extends Enum> Converter<String, T> getConverter(Class<T> targetType) {
return new StringToEnumConverter(targetType);
}
private static class StringToEnumConverter<T extends Enum>
implements Converter<String, T> {
private Class<T> enumType;
private StringToEnumConverter(Class<T> enumType) {
this.enumType = enumType;
}
@SuppressWarnings("unchecked")
@Override
public T convert(String source) {
return (T) Enum.valueOf(this.enumType, source.toUpperCase());
}
}
}

View File

@ -27,7 +27,7 @@ import static org.springframework.data.domain.Sort.Direction.DESC;
@RequestMapping(value = "/admin/posts") @RequestMapping(value = "/admin/posts")
public class PostController { public class PostController {
private PostService postService; private final PostService postService;
public PostController(PostService postService) { public PostController(PostService postService) {
this.postService = postService; this.postService = postService;
@ -45,18 +45,18 @@ public class PostController {
*/ */
@GetMapping @GetMapping
public String posts(Model model, 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, @RequestParam(value = "page", defaultValue = "0") Integer page,
@SortDefault.SortDefaults({ @SortDefault.SortDefaults({
@SortDefault(sort = "postPriority", direction = DESC), @SortDefault(sort = "topPriority", direction = DESC),
@SortDefault(sort = "postDate", direction = DESC) @SortDefault(sort = "createTime", direction = DESC)
}) Sort sort) { }) Sort sort) {
final Pageable pageable = PageRequest.of(page, 10, sort); final Pageable pageable = PageRequest.of(page, 10, sort);
final Page<PostSimpleOutputDTO> posts = postService.listByStatus(status, PostType.POST, pageable); final Page<PostSimpleOutputDTO> posts = postService.listByStatus(status, PostType.POST, pageable);
model.addAttribute("posts", posts); 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("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); model.addAttribute("status", status);
return "admin/admin_post"; return "admin/admin_post";
} }