簡體   English   中英

Spring Boot 和 Angular 應用程序,在 tomcat 容器中部署 war 文件時出現問題

[英]Spring boot and angular app, issue in deploying war file in tomcat container

我在 Spring boot 2.6.6 和 Angular 6.1.0 中有一個應用程序。 我試圖在一場戰爭中部署這兩個應用程序。 如果在 Eclipse 和 Angular 應用程序中分別作為 Spring Boot 應用程序運行,則應用程序在上下文路徑中運行良好。 但是如果我在 tomcat (9.0.30) 中運行,應用程序不會加載。 對於 URL localhost:8080/Demo/#/login ,我在瀏覽器屏幕上獲得了一些 JSON 內容。

{
    "_links": {
        "users": {
            "href": "http://localhost:8080/Demo/index.html/users{?page,size,sort}",
            "templated": true
        },
        "country": {
            "href": "http://localhost:8080/Demo/index.html/country{?page,size,sort}",
            "templated": true
        }
        ...
        ...
        ...
    }
}

我也嘗試過 URL - localhost:8080/Demo/index.html,然后我可以看到應用程序 UI。 但是在刷新頁面 URL 時,會更改為 localhost:8080/Demo/#/login 並在 UI 上顯示相同的 JSON。

你必須在你的 spring 應用程序中添加一個控制器

@Controller
public class RouteToAngular implements ErrorController {

@RequestMapping("/error")
public String handleError() {
    return "/";
}


public String getErrorPath() {
    return "/error";
}
}

您的 Angular 中的 index.html 在頭部添加<base href="/">

<!doctype html>
 <html lang="en">
  <head>
      <meta charset="utf-8">
       <title>App title</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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM