zoukankan      html  css  js  c++  java
  • springboot mvc beetl模板 自定义错误的后缀问题

    @Component
    public class BeetlErrorViewResolver implements ErrorViewResolver {
    
    	private static final Map<Series, String> SERIES_VIEWS;
    	@Value("${beetl.templatesPath:templates}")
    	String templatesPath;
    	@Value("${beetl.suffix:btl}")
    	String suffix;
    
    	private static ArrayList<String> errorFiles = new ArrayList<String>();
    
    	public void getErrorFiles() {
    		if (errorFiles.isEmpty()) {
    			String path = BeetlErrorViewResolver.class.getClassLoader().getResource("").getFile();
    			File templates = new File(path + templatesPath + "/error");
    			File[] files = templates.listFiles();
    			for (File file : files) {
    				if (file.isFile())
    					errorFiles.add(file.getName());
    			}
    		}
    	}
    
    	static {
    		Map<Series, String> views = new EnumMap<>(Series.class);
    		views.put(Series.CLIENT_ERROR, "4xx");
    		views.put(Series.SERVER_ERROR, "5xx");
    		SERIES_VIEWS = Collections.unmodifiableMap(views);
    
    	}
    
    	@Override
    	public ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus status, Map<String, Object> model) {
    		ModelAndView modelAndView = resolve(String.valueOf(status), model);
    		if (modelAndView == null && SERIES_VIEWS.containsKey(status.series())) {
    			modelAndView = resolve(SERIES_VIEWS.get(status.series()), model);
    		}
    		return modelAndView;
    	}
    
    	private ModelAndView resolve(String viewName, Map<String, Object> model) {
    		viewName = viewName + "." + suffix;
    		getErrorFiles();
    		try {
    			for (String error : errorFiles) {
    				if (error.equals(viewName))
    					return new ModelAndView("/error/" + viewName, model);
    			}
    		} catch (Exception ex) {
    		}
    		return null;
    	}
    
    }
    

      

  • 相关阅读:
    maven打包不执行测试用例
    maven不打包子模块资源文件
    mvn打包时添加version和profile
    eclipse控制台中文乱码解决方法
    eclipse常用插件
    Spring Boot系列之-logging
    Spring Boot系列之-profile
    Spring Boot系列之-helloword入门
    sqlite入门
    解决eclipse spring配置报错:cvc-elt.1: Cannot find the declaration of element
  • 原文地址:https://www.cnblogs.com/startnow/p/8818931.html
Copyright © 2011-2022 走看看