所以我有一个 Java/Spring MVC Web 应用程序,其中有 @Controller
方法注释为 @RequestMapping(value = "/", method = RequestMethod.GET)
返回主 index.html (当在浏览器 URL 中输入 localhost:8080
时),其中包含 React.js 应用程序。这是 text/html
的唯一 Controller ,其他都是@RestController
s 映射到路径 /api...
- React SPA 的restfull api。 React 应用程序已经实现了react-router-dom,并且应用程序中的编程路由工作正常,例如一个<Link to="/dashboard">dashboard</Link>
到达http://localhost:8080/dashboard
并且不向服务器发送 GET。
问题是当我手动输入 http://localhost:8080/dashboard
时在浏览器 URL 中,它向 Spring 服务器发送 GET 请求,而当 React 应用程序由 Node 运行时提供服务时,react-router-dom 会“捕获”并处理这些浏览器请求。这些text/html浏览器请求可以强制由react路由器处理吗?
最佳答案
我假设你正在使用react的BrowserRouter。
当您在浏览器中输入 URL 并按 ENTER 键时,您正在向服务器发出 GET http 请求。当使用 React 的 Link 元素进行导航时,浏览器会解析 url 更改并根据 url 值渲染 React 组件,而无需发出请求。
希望这有帮助
关于java - Spring MVC/Rest 中嵌入的 React 应用程序 : browser GET request routing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51483903/