本文介紹了每條url路徑和子路徑的Spring Single頁(yè)面/a/**=>;/a/index.html/a/靜態(tài)/**&qot;除外的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
我正在構(gòu)建Spring網(wǎng)站,該網(wǎng)站在子路徑下有反應(yīng)單頁(yè)應(yīng)用程序,我當(dāng)前的URL結(jié)構(gòu)應(yīng)該如下所示
localhost/admin/** => react app
localhost/** => spring thymeleaf/rest/websocket app for everything else
react app mapping:
localhost/admin/static/** => static react files
localhost/admin/** => react index.html for everything else
Example of project resources structure:
resources/
admin/ <= my admin react files is here
index.html
static/ <= react css, js, statics
templates/ <= thymeleaf templates
static/ <= theamleaf static
...
因此,我需要為每個(gè)URL路由及其子路由轉(zhuǎn)發(fā)REACTIONindex.html
文件。除靜態(tài)文件外,基本上只有一個(gè)頁(yè)面的應(yīng)用程序
看起來(lái)像是一項(xiàng)常見(jiàn)的任務(wù),下面是我已經(jīng)嘗試過(guò)的一些方法:
項(xiàng)目的完整工作演示Spring+Reaction+Gradle如何構(gòu)建項(xiàng)目以及為什么我不能將Reaction文件放在不同的目錄(例如/Resources/Static):https://github.com/varren/spring-react-example
無(wú)法使用forward:/admin/index.html
for/admin/**
,因?yàn)檫@將創(chuàng)建遞歸,因?yàn)?code>admin/index.html也在admin/**
下,必須以某種方式截取admin/static/**
。
無(wú)法在WebMvcConfigurerAdapter
中使用addResourceHandlers
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/admin/**")
.addResourceLocations("classpath:/admin/");
}
index.html
僅映射到/admin/index.html
url,此選項(xiàng)幾乎有效,但僅當(dāng)您從localhost/admin/index.html
訪問(wèn)Reaction應(yīng)用程序時(shí)
看到this和this和this和許多其他鏈接,我也有一些解決方案,但可能有一些通用選項(xiàng)我就是看不到
推薦答案
現(xiàn)在我正在使用自定義ResourceResolver
來(lái)解決此問(wèn)題
演示:https://github.com/varren/spring-react-example
@Configuration
public class BaseWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
ResourceResolver resolver = new AdminResourceResolver();
registry.addResourceHandler("/admin/**")
.resourceChain(false)
.addResolver(resolver);
registry.addResourceHandler("/admin/")
.resourceChain(false)
.addResolver(resolver);
}
private class AdminResourceResolver implements ResourceResolver {
private Resource index = new ClassPathResource("/admin/index.html");
@Override
public Resource resolveResource(HttpServletRequest request, String requestPath, List<? extends Resource> locations, ResourceResolverChain chain) {
return resolve(requestPath, locations);
}
@Override
public String resolveUrlPath(String resourcePath, List<? extends Resource> locations, ResourceResolverChain chain) {
Resource resolvedResource = resolve(resourcePath, locations);
if (resolvedResource == null) {
return null;
}
try {
return resolvedResource.getURL().toString();
} catch (IOException e) {
return resolvedResource.getFilename();
}
}
private Resource resolve(String requestPath, List<? extends Resource> locations) {
if(requestPath == null) return null;
if (!requestPath.startsWith("static")) {
return index;
}else{
return new ClassPathResource("/admin/" + requestPath);
}
}
}
}
這篇關(guān)于每條url路徑和子路徑的Spring Single頁(yè)面/a/**=>;/a/index.html/a/靜態(tài)/**&qot;除外的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,