gpt4 book ai didi

java - Angular 4 在带有 Valve 的 Tomcat 9 上使用路由时出现错误 404

转载 作者:行者123 更新时间:2023-11-28 23:21:13 30 4
gpt4 key购买 nike

我将通过以下步骤告诉您会发生什么:

1) 我的应用程序项目基于 Angular 4.0.2。我正在使用路由。这些是我在 app.module.ts 中的路线:

const routes: Routes =
[
{ path: '', component: LoginComponent },
{ path: 'dashboard', component: DashboardComponent },
{ path: 'login', component: LoginComponent }
];

所以 Web 应用程序的第一个屏幕是登录。

2) 我执行了命令 ng build -prod

3) 我将文件夹 dist 移动到 Tomcat webapps 文件夹中

4) 我修改了文件 index.html 并更改了 base href。现在是

5) 在/Tomcat9/conf/context.xml 中,我添加了以下指令:

<Valve className="org.apache.catalina.valves.rewrite.RewriteValve" />

6) 我创建了WEB-INF文件夹,然后我创建了文件rewrite.config,这个文件包含: # 如果请求现有 Assets 或目录,请按原样转到它

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]

# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html

7) 我启动了 Tomcat 服务器

8) 接下来我打开了一个 Mozilla 浏览器并输入了地址栏 本地主机:8080/分区/出现带有 2 个文本框(用户和密码)和一个用于登录的按钮的登录屏幕。

9) 当我按下用户按钮并通过有效时。它转到仪表板屏幕,我看到地址栏更改为:localhost:8080/dist/dashboard.

10) 之后,我转到地址栏并手动写入:localhost:8080/dist/dashboard。

然后我按 Enter 并出现错误 404。

它应该继续显示仪表板屏幕而不是错误 404。

此应用程序在带有 .htaccess 的 Apache 服务器中运行良好,但在 Tomcat 9 中运行不佳

如果我配置了 rewrite.config,为什么在这种情况下不显示仪表板屏幕?为了避免错误 404,缺少什么?

最佳答案

重写规则似乎不起作用。试试这些规则:

#Check if the uri ends with an file extension
RewriteCond %{REQUEST_URI} .*\.(.*)$ [OR]

#Check if the uri contains /api/
RewriteCond %{REQUEST_URI} ^(.*)(/api/).*$ [OR]
RewriteRule ^(.*)$ - [L]

#If the requested resource doesn't exist, use index.html
RewriteRule ^(.*)$ /index.html

关于java - Angular 4 在带有 Valve 的 Tomcat 9 上使用路由时出现错误 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43509519/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com