繁体   English   中英

白标错误页面 404 spring boot angular 5

[英]whitelabel error page 404 spring boot angular 5

我正在开发一个 Web 应用程序,其中我的后端是 Spring Boot,前端是在 4200 端口上运行的 angular 5。 我在 angular 5 登录、家庭应用程序、搜索中有四个组件。 当我开始 spring boot 项目和 angular 项目时,我可以通过提供http://localhost:4200来导航登录页面。 所以它导航到http://localhost:4200/login

但是当我刷新该页面时,出现以下错误。 在此处输入图片说明

这是我的代码:

代理配置文件

    {
    "/": {
        "target": "http://localhost:8081",
        "secure": false
    }
}

包.json:

 {  "name": "cyber-security-vw",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve --proxy-config proxy.conf.json",
    "build": "ng build --prod",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^5.2.0",
    "@angular/common": "^5.2.0",
    "@angular/compiler": "^5.2.0",
    "@angular/core": "^5.2.0",
    "@angular/forms": "^5.2.0",
    "@angular/http": "^5.2.0",
    "@angular/platform-browser": "^5.2.0",
    "@angular/platform-browser-dynamic": "^5.2.0",
    "@angular/router": "^5.2.0",
    "core-js": "^2.4.1",
    "rxjs": "^5.5.6",
    "zone.js": "^0.8.19"
  },
  "devDependencies": {
    "@angular/cli": "1.6.5",
    "@angular/compiler-cli": "^5.2.0",
    "@angular/language-service": "^5.2.0",
    "@types/jasmine": "~2.8.3",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "codelyzer": "^4.0.1",
    "jasmine-core": "~2.8.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~2.0.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~4.1.0",
    "tslint": "~5.9.1",
    "typescript": "~2.5.3"
  }
}

索引.html

    <!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>CyberSecurityVw</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
  <app-root></app-root>
</body>
</html>

应用程序组件.html

<div align="center">
  <router-outlet></router-outlet>
</div>

app-routing.module.ts

import { ApplicationComponent } from './application/application.component';
import { NavigationComponent } from './navigation/navigation.component';
import { HomepageComponent } from './homepage/homepage.component';
import { AppComponent } from './app.component';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { CommonModule } from '@angular/common';
import { LoginComponent } from './login/login.component';



const routes: Routes = [
  { path: '', redirectTo: '/login', pathMatch: 'full' },
  { path: 'login', component: LoginComponent },
  { path: 'home', component: HomepageComponent },
  { path: 'application', component: ApplicationComponent },
  { path: 'navigation', component: NavigationComponent },
];

@NgModule({
  imports: [CommonModule, RouterModule.forRoot(routes)],
  exports: [RouterModule],
  declarations: []
})

export class AppRoutingModule { }

Spring Boot 主类

package com.vl.cybersecurity;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
@SpringBootApplication
public class CyberSecurityApplication {

    public static void main(String[] args) {
        SpringApplication.run(CyberSecurityApplication.class, args);
    }
}

其他包:

com.vl.cybersecurity.controller     
com.vl.cybersecurity.entity
com.vl.cybersecurity.service
com.vl.cybersecurity.dao

我也面临同样的问题。 这是对我有用的解决方案。

我在我的应用程序路由模块中启用了 hash(#)。

如何启用?

Step 1:打开app-routing.module.ts Step 2:在RouterModule.forRoot中添加路由时,传入可选参数useHash为真值。

 @NgModule({
  imports: [RouterModule.forRoot(routes, { useHash: true })],
  exports: [RouterModule],
})

第 3 步:保存文件。 它应该可以解决这个问题。 您会在 url 中注意到的唯一不同之处是端口号后的 hash(#) 没有http://localhost:4800/#/dashboard

还有一种方法可以做同样的事情,检查这个

您需要将所有“notFound”请求重定向到“index.html”。 查看此主题以解决您的问题 - 链接

对不起,伙计们,我可能来不及回答这个问题了。 在我当前使用(Angular 7 + Spring boot)开发 Web 应用程序时,我在刷新页面时也遇到了同样的问题(白标错误),并且在另一种情况下,例如复制 URL 并粘贴到浏览器新选​​项卡中。

原因:我们必须了解为什么会发生这种情况,因为当您刷新或复制并粘贴 URL 时,实际发生的情况浏览器不知道在哪里导航请求的路由/路径,因为我们都知道我们的 Angular 应用程序已部署,并且它是在像 tomcat 等服务器上运行...所以服务器不知道从浏览器到哪里导航特定的路由请求。所以我们必须以某种方式告诉服务器。

解决方案:所以我得到了一个解决方案,在阅读了很多之后,我在我的控制器类中添加了以下代码。( *PIC 1 )实际上将来自浏览器的路由请求重定向到index.html从这里开始Angular 本身会处理路由。所以不会再出现白标错误。

我的控制器类 -(PIC 1)

另一件事你必须在构建 Angular 应用程序时正确定义 index.html 的重定向路径,我们可能已经给出了输出目录文件夹路径,其中所有 TS 文件都被转换为简单的 JS 文件。默认情况下,spring boot 将查看静态文件夹。在我的情况下,我做了同样的事情,你可以参考我在我的 angular 应用程序中的angular.json ( *PIC 2 ) 文件中给出的第 16 行(输出目录路径)。 在构建 angular 应用程序时,静态文件夹将在资源文件夹下自动创建。在静态文件夹内,将自动生成 index.html 文件。

我的 angular.json 文件-(PIC 2)

希望对你/某人有帮助。 谢谢各位。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM