reactjs - Spring Boot 上下文路径不适用于 React 浏览器路由器
问题描述
我有打包在 Spring Boot jar 中的 React 应用程序。build后jar里面的结构如下
/static/bundle.js
/static/index.html
在 Spring Boot 配置中渲染 UI 我添加了
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/index.html");
}
React 应用程序内部
<BrowserRouter basename="/">
<Switch>
<Route path="/" component={ShoppingCart} />
<Route path="/recap" component={Recap} />
<Route path="/confirm" component={ConfirmPayment} />
</Switch>
</BrowserRouter>
这工作正常,我可以访问根路径 http://localhost:8088/ 上的应用程序。所有进一步的导航也像 /recap 一样工作。
现在我在 Spring 应用程序属性 server.servlet.contextPath=/demo中添加了上下文名称,代码更改如下
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/demo").setViewName("forward:/index.html");
}
<BrowserRouter basename="/demo">
<Switch>
<Route path="/" component={ShoppingCart} />
<Route path="/recap" component={Recap} />
<Route path="/confirm" component={ConfirmPayment} />
</Switch>
</BrowserRouter>
这会在 http://localhost:8088/demo 成功呈现根页面但是如果我尝试导航到回顾屏幕 [history.push({pathname: "/recap" ....] 它无法找到路线. url 更改为 http://localhost:8088/demo/recap ,但页面未加载。
我已经搜索并尝试了许多不同的方法,但无法成功。
解决方案
推荐阅读
- firebase - 找不到正确的提供者
在此注册小部件上方 - python - 从python中的json文件中查找字符串匹配
- python - Subprocess unrecognized arguments but command works in terminal
- android - 无法使用 Firebase UI Recycler Adapter 在 Recycler 视图中加载内容
- excel - 具有多个 OR/AND 标准的 SUMIFS(使用单元格引用)
- javascript - 全局数组保持为空
- java - AWS @LambdaFunction 无法反序列化为 POJO
- sql - SQL Server 中 group by 子句中的前 1 名?
- google-apps-script - 如何从侧边栏填充特定列?
- lua - lua 脚本如何调用它没有导入的函数?