Spring MVC/Rest中嵌入的React app:浏览器GET请求路由
React app embedded in Spring MVC/Rest: browser GET request routing
所以我有一个 Java/Spring MVC 网络应用程序,它有一个 @Controller
带有注释的方法 @RequestMapping(value = "/", method = RequestMethod.GET)
返回主 index.html(当在浏览器 url 中输入 localhost:8080
时),包含 React.js 应用程序。这是 text/html
的唯一控制器,所有其他控制器都 @RestController
映射到路径 /api...
- React SPA 的 restfull api。 React 应用程序已实现 react-router-dom 并且应用程序中的编程路由工作正常,f.e。 <Link to="/dashboard">dashboard</Link>
到达 http://localhost:8080/dashboard
并且没有向服务器发送 GET。
问题是当我在浏览器 url 中手动键入 http://localhost:8080/dashboard
时,它会向 Spring 服务器发送 GET 请求,而当 React 应用程序由 节点运行时提供服务时、react-router-dom "catches" 并处理这些浏览器请求。这些 text/html 浏览器请求是否可以强制由 React 路由器处理?
我假设您使用的是 React 的 BrowserRouter。
当您在浏览器中键入 url 并按 ENTER 键时,您正在向服务器发出 GET http 请求。当使用 React 的 Link 元素导航时,您的浏览器会解析 url 更改并根据 url 值呈现 React 组件,而无需发出请求。
希望对您有所帮助
所以我有一个 Java/Spring MVC 网络应用程序,它有一个 @Controller
带有注释的方法 @RequestMapping(value = "/", method = RequestMethod.GET)
返回主 index.html(当在浏览器 url 中输入 localhost:8080
时),包含 React.js 应用程序。这是 text/html
的唯一控制器,所有其他控制器都 @RestController
映射到路径 /api...
- React SPA 的 restfull api。 React 应用程序已实现 react-router-dom 并且应用程序中的编程路由工作正常,f.e。 <Link to="/dashboard">dashboard</Link>
到达 http://localhost:8080/dashboard
并且没有向服务器发送 GET。
问题是当我在浏览器 url 中手动键入 http://localhost:8080/dashboard
时,它会向 Spring 服务器发送 GET 请求,而当 React 应用程序由 节点运行时提供服务时、react-router-dom "catches" 并处理这些浏览器请求。这些 text/html 浏览器请求是否可以强制由 React 路由器处理?
我假设您使用的是 React 的 BrowserRouter。
当您在浏览器中键入 url 并按 ENTER 键时,您正在向服务器发出 GET http 请求。当使用 React 的 Link 元素导航时,您的浏览器会解析 url 更改并根据 url 值呈现 React 组件,而无需发出请求。
希望对您有所帮助