mirror of https://github.com/halo-dev/halo
config StringToEnumConverterFactory
parent
c9c68eef54
commit
2a71ba67b2
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<PostSimpleOutputDTO> 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";
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue