简体   繁体   English

在TomCat上部署时,我的Angular + Spring应用程序路由不起作用

[英]My Angular + Spring Application routing does not work when deployed on TomCat

I am very much new to angular and spring. 我对Angle和Spring非常陌生。 I might have made some silly mistakes. 我可能犯了一些愚蠢的错误。 I have Angular + Spring app which displays login screen and after that it shows menu. 我有Angular + Spring应用程序,它显示登录屏幕,然后显示菜单。 This works perfectly when not deployed (ie when Angular & Spring are running parallel) But when I deploy the app on tomcat, it does not show login form. 当未部署时(即当Angular和Spring并行运行时),这将完美工作。但是当我在tomcat上部署应用程序时,它不会显示登录表单。 I followed below steps for deployment - 我按照以下步骤进行了部署-

  1. ng build --base-href=./ ng build --base-href =。/
  2. copy all files from angular dist folder to spring static folder 将所有文件从angular dist文件夹复制到spring静态文件夹
  3. create .war file in spring and deploy it in tomcat 在春季创建.war文件并将其部署在tomcat中

my pom.xml 我的pom.xml

<groupId>com.techence</groupId>
        <artifactId>new</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>war</packaging>

        <name>LoginDemoBackend</name>
        <description>Demo project for Spring Boot</description>

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.4.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>

        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>

        <dependencies>

        <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>

            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>


            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>

                <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <version>1.5.7.RELEASE</version>
                <scope>provided</scope>
                </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>

            <dependency>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.1.0</version>
                <type>maven-plugin</type>
            </dependency>
        </dependencies>

        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>


        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>javax.servlet</groupId>
                    <artifactId>servlet-api</artifactId>
                    <version>2.4</version>
                </dependency>
            </dependencies>
        </dependencyManagement>
    </project>

My app.module.ts 我的app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { LoginFormComponent } from './login-form/login-form.component';
import { HttpModule } from '@angular/http';
import { FormsModule} from '@angular/forms';
import { LoginServiceService } from './login-service.service';
import { HttpClientModule } from '@angular/common/http';
import { MenubarComponent } from './menubar/menubar.component';
import { BranchCreationComponent } from './menubar/branch-creation/branch-
creation.component';
import { RouterModule,Routes } from '@angular/router';
import { LedgerCreationComponent } from './menubar/ledger-creation/ledger-creation.component';

import { LocationStrategy } from '@angular/common';

import { HashLocationStrategy } from '@angular/common';

import { ForgotPasswordComponent } from './forgot-password/forgot-
password.component';

const routes: Routes = [    
    { path: '', component : LoginFormComponent}, 

    { path: 'menubar', component: MenubarComponent }, 

    { path: 'branchCreation', component: BranchCreationComponent },

    { path: 'ledgerCreation', component: LedgerCreationComponent } 
];

@NgModule({
declarations: [

    AppComponent,

    LoginFormComponent,

    MenubarComponent,

    BranchCreationComponent,

    LedgerCreationComponent,

    ForgotPasswordComponent

  ],
  imports: [ 
    BrowserModule,

FormsModule,

HttpModule,

HttpClientModule,

RouterModule.forRoot(routes),

  ],


  providers: [
              LoginServiceService,
             { provide: LocationStrategy, useClass: HashLocationStrategy },
            ],
  bootstrap: [AppComponent]
})
export class AppModule { }

Try to add this in your Spring Boot code: 尝试将其添加到您的Spring Boot代码中:

@Bean
public ErrorViewResolver customErrorViewResolver() {
    final ModelAndView redirectToIndexHtml = new ModelAndView("forward:/index.html", Collections.emptyMap(), HttpStatus.OK);
    return (request, status, model) -> status == HttpStatus.NOT_FOUND ? redirectToIndexHtml : null;
}

It works for me and even Angular routing is working. 它对我有用,甚至Angular路由也在工作。 I found it here . 我在这里找到的。

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

相关问题 部署在Tomcat上的Spring MVC应用程序无法在IntelliJ上运行 - Spring MVC application deployed on Tomcat does not run on IntelliJ 部署在Tomcat 7.0.57上的Spring Boot 1.2.2应用程序无法启动 - Spring Boot 1.2.2 application deployed on Tomcat 7.0.57 does not start 在tomcat中部署了Spring Boot Application - Deployed Spring boot Application in tomcat Spring应用程序在Eclipse中工作,但在tomcat 8上部署时无法正常工作 - Spring application working in eclipse but same not working when deployed on tomcat 8 Spring Boot应用程序可以独立运行,但是在Tomcat中部署时找不到类 - Spring Boot application works as standalone but class not found when deployed in Tomcat 部署到 Tomcat 时无法访问基本 REST Spring MVC 应用程序 - Basic REST Spring MVC application cannot be reached when deployed to Tomcat 在Tomcat中部署spring时是否需要JTA TX管理器 - Does spring requires JTA TX manager when deployed in Tomcat 部署到 Tomcat 时 msal-java-webapp-sample 不起作用 - msal-java-webapp-sample does not work when deployed to Tomcat 雄猫 在Eclipse中工作,但在部署时不工作 - Tomcat. Works in Eclipse, but does not work when deployed 在Tomcat中部署具有战争功能的Hotswap Spring应用程序 - Hotswap Spring Application with war deployed in Tomcat
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM