現在、Spring Boot を使用して React アプリを提供する必要があります。ルート URL - では動作しますlocalhost:8080/
が、もちろん、サブルートはどれも Spring コントローラーによって認識されません。
マップ可能なすべてのルートをハードコーディングせずに、React ルーティングと Spring リクエスト マッピングを整合させる方法がわかりません。index.html
また、ルートの一部には可変のサブルートがあります。
これが私HomeController
のindex.html
@Controller
public class HomeController {
@RequestMapping(value = "/")
public String index() {
return "index.html";
}
}
これは私のReactアプリのルーティングレンダリングです
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>
)
どのような助けでも大歓迎です!
編集: ワイルドカード機能をいくつか追加してみましたが、奇妙な動作が見られます。 の更新されたコードは次のとおりですHomeController
。
@Controller
public class HomeController {
@RequestMapping(value = {"/", "/users/**", "/org/**"})
public String index() {
return "index.html";
}
}
と にはアクセスできます/
が、/users
または にはアクセスできませ/users/
ん/users/2545
。
後者にアクセスしようとしたときのエラーは次のとおりです。
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]
ベストアンサー1
私は、この答えを試してみましたリンクコントローラーを持っていませんでした。
基本的には、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:/");
}
}