使用 Spring Boot 提供 React App 时的 React-Router 问题 [英] React-Router issues when serving React App with Spring Boot
问题描述
我目前需要使用 Spring Boot 为我的 React 应用程序提供服务.它适用于根 url - localhost:8080/
,但当然 Spring 控制器无法识别任何子路由.
I'm currently needing to serve my React app with Spring Boot. It is working for the root url - localhost:8080/
, but of course none of the sub-routes are recognized by the Spring Controller.
我不知道如何让 React Routing 和 Spring 请求映射在没有硬编码的情况下在每个可能的路由中对齐以映射到 index.html
- 然后一些路由有可变子路由.
I'm not sure how to get the React Routing and Spring request mapping to line up without hard-coding in every possible route to map to index.html
- and then some of the routes have variable sub-routes.
这是我的 HomeController
服务于 index.html
@Controller
public class HomeController {
@RequestMapping(value = "/")
public String index() {
return "index.html";
}
}
<小时>
这是我的 React 应用程序中的路由渲染
Here is the Routing render in my React App
const Root = ({ store }) => (
<Provider store={store}>
<Router history={browserHistory}>
<Route path="/" component={App}>
<IndexRedirect to="users"/>
<Route path="/org" component={OrgSearch}>
{<Route path="/org/:orgId" component={Org}/>}
</Route>
<Route path="/users" component={UserSearch}>
{<Route path="/users/:userId" component={User} />}
</Route>
</Route>
</Router>
</Provider>
)
<小时>
非常感谢任何帮助!
Any help is greatly appreciated!
我尝试添加一些通配符功能,但它呈现出一些奇怪的行为.这是 HomeController
的更新代码.
I tried adding some wildcard functionality, and it is presenting some strange behavior. Here is the updated code for the HomeController
.
@Controller
public class HomeController {
@RequestMapping(value = {"/", "/users/**", "/org/**"})
public String index() {
return "index.html";
}
}
我可以访问 /
和 /users
但不能访问 /users/
或 /users/2545
.
I can access /
and /users
but not /users/
or /users/2545
.
这是我尝试访问后者时的错误.
Here is the error when I try to access the latter.
2017-04-14 09:21:59.896 ERROR 58840 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Circular view path [index.html]: would dispatch back to the current handler URL [/users/index.html] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)] with root cause
javax.servlet.ServletException: Circular view path [index.html]: would dispatch back to the current handler URL [/users/index.html] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
at org.springframework.web.servlet.view.InternalResourceView.prepareForRendering(InternalResourceView.java:205) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:145) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1282) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1037) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:980) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
推荐答案
对我来说,我在这个 link.我没有控制器.
For me, I tried the answer in this link. I did not have a controller.
基本上是关于定义 addViewController...
Basically it is about defining the addViewController...
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/{spring:\\w+}")
.setViewName("forward:/");
registry.addViewController("/**/{spring:\\w+}")
.setViewName("forward:/");
registry.addViewController("/{spring:\\w+}/**{spring:?!(\\.js|\\.css)$}")
.setViewName("forward:/");
}
}
这篇关于使用 Spring Boot 提供 React App 时的 React-Router 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!