手动访问使用React Router和Spring Boot后端的路由。

huangapple go评论73阅读模式
英文:

Manually access a route with react router and a spring boot backend

问题

我有一个React用户界面,可以在新标签页中打开另一个用户界面的子路由。

例如:在localhost/app1中的一个按钮可以打开一个新标签页到localhost/app2/usersettings - 这在开发环境中可以正常工作,但一旦我将应用程序打包部署,它就会失败。问题是它只是重定向我回到localhost/app2

这是我的设置:

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addRedirectViewController("/", "index.html");
    registry.addRedirectViewController("/usersettings", "index.html?path=usersettings");
    registry.addRedirectViewController("/{x:[\\w\\-]+}", "index.html");
    registry.addRedirectViewController("/{x:^(?!api$).*$}/**/{y:[\\w\\-]+}", "index.html");
}
const MainPage = () => {
    return (
        <Box>
            <CssBaseline/>
            <Routes>
                <Route path="/" element={<HomePage/>}/>
                <Route path="index.html" element={<Navigate to="/"/>}/>
                <Route path="/usersettings" element={<UserSettings/>}/>
            </Routes>
        </Box>
    );
};

如果我在app2中导航到/usersettings,如果我点击路由,它可以完美工作,但一旦我手动输入URL或者通过app1的按钮重定向,它只会加载主页路由。

我应该如何设置路由,以便路由器接收到正确的路径变量?

我完全迷失了。

谢谢。

英文:

I have a react UI that opens a new tab to another UI's sub route.

Example: a button in localhost/app1 opens a new tab to localhost/app2/usersettings - This works in a development scenario but once I roll out the applications as package, it fails. The issue is, that it just redirects me back to localhost/apps2

Here's my setup

@Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController(&quot;/&quot;,&quot;index.html&quot;);
        registry.addRedirectViewController(&quot;/usersettings&quot;,&quot;index.html?path=usersettings&quot;);
        registry.addRedirectViewController(&quot;/{x:[\\w\\-]+}&quot;,&quot;index.html&quot;);
        registry.addRedirectViewController(&quot;/{x:^(?!api$).*$}/**/{y:[\\w\\-]+}&quot;, &quot;index.html&quot;);
    }
const MainPage = () =&gt; {
    return (
        &lt;Box&gt;
            &lt;CssBaseline/&gt;
            &lt;Routes&gt;
                &lt;Route path=&quot;/&quot; element={&lt;HomePage/&gt;}/&gt;
                &lt;Route path=&quot;index.html&quot; element={&lt;Navigate to=&quot;/&quot;/&gt;}/&gt;
                &lt;Route path=&quot;/usersettings&quot; element={&lt;UserSettings/&gt;}/&gt;
            &lt;/Routes&gt;
        &lt;/Box&gt;
    );
};

Navigating to /usersettings in app2 works perfectly if I click the route, but once I manually enter the url or redirect by a button from app1 it just loads the home route.

How am I supposed to setup the routes so router receives the correct path variable?

I am totally lost.

Thanks

答案1

得分: 1

首先,您需要配置您的服务器,以便为您的React应用程序应处理的所有路由提供正确的index.html文件,正如我所说的。为此,您可以通过设置一个捕获所有路由的路由来实现,以为任何未识别的路由提供index.html文件。

让我用一个示例来解释给您:

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addRedirectViewController("/", "index.html");
    registry.addRedirectViewController("/{x:^(?!api$).*$}/**", "index.html");
}

这应该允许您的React应用程序正确处理所有路由,包括在新标签页中打开的路由或从app1重定向的路由。

在您的React应用程序中,您可以像这样设置您的路由:

const MainPage = () => {
    return (
        <Box>
            <CssBaseline/>
            <Routes>
                <Route path="/" element={<HomePage/>}/>
                <Route path="/usersettings" element={<UserSettings/>}/>
                <Route path="*" element={<Navigate to="/"/>}/>
            </Routes>
        </Box>
    );
};

正如您所见,配置设置了一个捕获所有路由并将其重定向到主页的路由,以确保您的React应用程序正确处理所有路由。

祝你好运,朋友!

英文:

Alrighty, first of all you need to configure your server to serve the correct index.html file for all routes that should be handled by your React application, as I told. for that purpose, you maybe should do this by setting up a catch-all route that serves the index.html file for any unrecognized routes.

let me explain it for you with an example :

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addRedirectViewController(&quot;/&quot;, &quot;index.html&quot;);
    registry.addRedirectViewController(&quot;/{x:^(?!api$).*$}/**&quot;, &quot;index.html&quot;);
}

it should allow your React application to handle all routes correctly, including routes that are opened in a new tab or redirected from app1.

and in your React application,you can then set up your routes like this:

const MainPage = () =&gt; {
    return (
        &lt;Box&gt;
            &lt;CssBaseline/&gt;
            &lt;Routes&gt;
                &lt;Route path=&quot;/&quot; element={&lt;HomePage/&gt;}/&gt;
                &lt;Route path=&quot;/usersettings&quot; element={&lt;UserSettings/&gt;}/&gt;
                &lt;Route path=&quot;*&quot; element={&lt;Navigate to=&quot;/&quot;/&gt;}/&gt;
            &lt;/Routes&gt;
        &lt;/Box&gt;
    );
};

as you see,configuration sets up a catch-all route that redirects to the home page for any unrecognized routes and it ensures that all routes are handled correctly by your React application

good luck bro !

huangapple
  • 本文由 发表于 2023年6月19日 22:26:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76507573.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定