perf:globalConfig中增加redirect方法

This commit is contained in:
yz
2023-01-09 23:28:24 +08:00
parent dd87b00612
commit 58ab427110
4 changed files with 113 additions and 4 deletions

View File

@@ -2,8 +2,15 @@ package com.tiesheng.util.config;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.log.LogFactory;
import com.tiesheng.util.exception.ApiException;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author hao
@@ -45,6 +52,27 @@ public class GlobalConfig {
return path;
}
/**
* 重定向
*
* @param htmlDir 资源目录
* @param route
*/
public void redirect(String htmlDir, String route, HttpServletResponse response) {
PathMatchingResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
try {
Resource[] resources = patternResolver.getResources(String.format("classpath*:static/%s/*/index.html", htmlDir));
if (resources.length == 0) {
throw new ApiException("无法重定向,请检查资源");
}
String filename = resources[resources.length - 1].getFile().getPath();
String path = buildPath(String.format("/%s%s#%s", htmlDir, StrUtil.subAfter(filename, htmlDir, true), route));
response.sendRedirect(path);
} catch (IOException ignored) {
}
}
///////////////////////////////////////////////////////////////////////////
// setter\getter
///////////////////////////////////////////////////////////////////////////