简体   繁体   English

刷新后的白标错误页面

[英]Whitelabel Error Page after refresh

I have Spring Boot Application (backend) and for the Frontend I am using the Angular 2 Single Page Application.我有 Spring Boot 应用程序(后端),而前端我使用的是 Angular 2 单页应用程序。

Whenever I navigate to a route for example: localhost:8080/getAccounts and do after the navigation a refresh I get the Whitelabel Error Page.每当我导航到一条路线时,例如:localhost:8080/getAccounts 并在导航后刷新我得到白标签错误页面。 If I am at the root localhost:8080 I works fine.如果我在根 localhost:8080 我工作正常。 The problem only occurs in the sub links.问题仅发生在子链接中。

Returning (use the return/back button) to the previous page also works fine.返回(使用返回/返回按钮)到上一页也可以正常工作。 Just the refresh.只是刷新。

I also can not call direct the link: localhost:8080/getAccounts.我也不能直接调用链接:localhost:8080/getAccounts。 First I have to go to Home (localhost:8080) an call the page throug sub navigation bar.首先,我必须转到主页(本地主机:8080)并通过子导航栏调用页面。

Does anybody had the same problem?有人有同样的问题吗? What excalty I do have to change.我必须改变什么。 My Code:我的代码:

Main.ts主文件

import {bootstrap} from '@angular/platform-browser-dynamic';
import {AppComponent} from './components/app.component';
import {HTTP_PROVIDERS};
import {enableProdMode} from '@angular/core';

enableProdMode();
bootstrap(AppComponent, [HTTP_PROVIDERS]);

app.comonent:应用程序组件:

import { Component, OnInit } from '@angular/core';

import { Http } from '@angular/http';
import { RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS } from '@angular/router-deprecated';

import { HomeComponent } from './home.component';
import { UserSearchComponent} from './webUserProfiles.component';
import { UserDetailViewComponent} from './webUserProfileView.component'; 

import { HTTPService } from '../service/http.service';


@Component({
  selector: 'app-content',
  templateUrl: './app/templates/app.component.html',
  directives: [ROUTER_DIRECTIVES, AccessErrorComponent],
  providers: [
    ROUTER_PROVIDERS,
    HTTPService
  ]
})

@RouteConfig([
  {
    path: '/',
    name: 'HomeComponent,
    useAsDefault: true
  },
  {
    path: '/user',
    name: 'UserSearch',
    component: UserSearchComponent,
  },
  {
    path: '/user/:id',
    name: 'UserDetailView',
    component: UserDetailViewComponent,
  }
])

export class AppComponent implements OnInit {
    constructor (
    ) { } 
}
}

Thanks in advance提前致谢

After some researches, i found this pretty good answer from Thierry Templier经过一些研究,我从Thierry Templier找到了这个很好的答案

With the default strategy (HTML5 history API) of routing, you need a server configuration to redirect all your paths to your HTML entry point file.使用默认的路由策略(HTML5 历史 API),您需要一个服务器配置来将您的所有路径重定向到您的 HTML 入口点文件。 With the hashbang approach it's not necessary... If you want to switch to this approach, simply use the following code:使用 hashbang 方法没有必要......如果你想切换到这种方法,只需使用以下代码:

import { bootstrap } from "angular2/platform/browser";
import { provide } from "angular2/core";
import {
  ROUTER_PROVIDERS, LocationStrategy, HashLocationStrategy
} from "angular2/router";

bootstrap(MainApp, [
  ROUTER_PROVIDERS,
  provide(LocationStrategy, {useClass:HashLocationStrategy});
]);

You could have a look at these questions about this issue:您可以查看有关此问题的这些问题:

I had a similar issue WhiteLabel Error message on my Angular SPA whenever I did a refresh.每当我刷新时,我的 Angular SPA 上都会出现类似的问题 WhiteLabel 错误消息。

If you don't want to change the app URL (which will happen if you use HashLocation Strategy), you could add a new controller to handle the White label Error mapping in your Spring Boot app.如果您不想更改应用程序 URL(如果您使用 HashLocation 策略会发生这种情况),您可以添加一个新控制器来处理 Spring Boot 应用程序中的白标签错误映射。

The fix was to create a controller that implements ErrorController and return a ModelAndView object that forwards to /修复方法是创建一个实现 ErrorController 的控制器并返回一个转发到/的 ModelAndView 对象

@CrossOrigin
@RestController
public class IndexController implements ErrorController {
    
    private static final String PATH = "/error";
    
    @RequestMapping(value = PATH)
    public ModelAndView saveLeadQuery() {           
        return new ModelAndView("forward:/");
    }

    @Override
    public String getErrorPath() {
        return PATH;
    }
}

If you don't want to use the HashLocationStrategy , you can add the following controller in your project :如果您不想使用HashLocationStrategy ,您可以在您的项目中添加以下控制器:

@Controller
public class UiController {

    @GetMapping("/")
    public String welcome() {
        return "index.html";
    }

    // Match everything without a suffix (so not a static resource)
    @GetMapping(value = {
            "/{path:[^.]*}",
            "/{path:[^.]*}/{path:[^.]*}",
            "/{path:[^.]*}/{path:[^.]*}/{path:[^.]*}",
            "/{path:[^.]*}/{path:[^.]*}/{path:[^.]*}/{path:[^.]*}",
            "/{path:[^.]*}/{path:[^.]*}/{path:[^.]*}/{path:[^.]*}/{path:[^.]*}"
            // add more if required ...
    })
    public String redirect() {
        // Forward to home page so that route is preserved.
        return "forward:/";
    }

}

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

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