gpt4 book ai didi

java - Spring Boot Controller 映射到 React 组件

转载 作者:行者123 更新时间:2023-12-01 16:24:03 26 4
gpt4 key购买 nike

我正在使用 - 在我的 React 应用程序中切换路线

<Route path="/myBootDomain" render={props => {
//check route path
const onpathA = window.location.href.indexOf('/pathA') !== -1;
const onpathB = window.location.href.indexOf('/pathB') !== -1;

if (onpathA){ return <PathAComponent />;}
else if(onpathB){ return <onpathBComponent />;}
}}/>

当我在本地主机上运行应用程序时,它按预期工作,因此我希望我的 Controller 将子域映射到正确的路由,例如 Controller -

@CrossOrigin(maxAge = 3600)
@RestController
@RequestMapping("/pathA")
public class WebContentController {


@RequestMapping(method = RequestMethod.GET)
public void method(HttpServletRequest request, HttpServletResponse httpServletResponse) {

httpServletResponse.setHeader("Location", "/myBootDomain/pathA");
httpServletResponse.setStatus(302);

}
}

尝试访问http://localhost:8080/myBootDomain/pathA时我没有重定向到索引文件,而是重定向到同一个 Controller (无限循环)。

我的index.html位于/resources/static下。

感谢您的帮助。

最佳答案

您想要做的是提供包含脚本(React 应用程序)但来自不同路径的相同 index.html 文件,即: /myBootDomain/pathA/myBootDomain/pathA/foo/bar。它将确保当用户重新加载 URL 时,他会获得相同的 React 应用程序,然后执行路由逻辑。请参阅@tashkhisi 答案以获取更多背景信息 https://stackoverflow.com/a/62193394/906265

现在这取决于服务器应用程序的设置方式以及您想要“包罗万象”的路径,但已经有一个类似的问题 Spring catch all route for index.html

关于java - Spring Boot Controller 映射到 React 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62192856/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com