설 연수
하하호홓
설 연수
전체 방문자
오늘
어제
  • 분류 전체보기 (231)
    • Back-End (2)
      • Java (20)
      • JSP (13)
      • Spring (18)
      • Kotlin (0)
      • node.js (0)
    • Front-End (68)
      • JavaScript (19)
      • jQuery (39)
      • Angular (4)
      • HTML (5)
    • Dev-Ops (12)
      • Linux, Cloud (5)
      • docker, k8s (5)
      • ElasticSeach (2)
    • Other (33)
      • OOP (3)
      • 알고리즘 (2)
      • DB (12)
      • Git (1)
      • Swift (4)
    • Backup (65)

블로그 메뉴

    공지사항

    인기 글

    태그

    • jquery invalid
    • Redis
    • page not found
    • RESTful
    • CORS
    • angular 콜백
    • INVALID
    • angular callback
    • angular2
    • 404 error
    • angular4
    • 패스트캠퍼스
    • MYSQL
    • Kafka
    • Angular
    • mongodb
    • docker
    • jOOQ
    • flex
    • 크로스도메인

    최근 댓글

    최근 글

    티스토리

    hELLO · Designed By 정상우.
    설 연수

    하하호홓

    Front-End/Angular

    [Tomcat]Angular App URL접근이나 새로고침 시 404에러 뜰때.

    2018. 2. 7. 16:44
    https://stackoverflow.com/questions/46299430/deploy-angular-application-on-apache-tomcat-404-deep-links-issue


    'ng build'한 후 톰켓에 빌드한 파일을 배포하였는데
    url로 angular에서 routing한 path를 접근하거나, 새로고침을 했을때 404에러가 발생하는 문제가 발생했다.

    때문에 404에러페이지를 생성했고, 
    요청한 url을 분기처리하여 angular에 접근하려는경우 angularApp에 해당접근path를 파라메터로 던지도록 했다.
    접근path를 넘겨받은 angularApp에서는 라우팅처리를 하도록 하여, 페이지에 정상 접근하도록 설정했다.


    web.xml
    1
    2
    3
    4
    5
    <!--JAVA WebApp lication Web.xml-->
    <error-page>
        <error-code>404</error-code>
        <location>/common/error.jsp</location>
    </error-page>
    cs



    /common/error.jsp

     - /mobile에 'ng build'된 파일을 배포하였음.(localhost:8080/mobile)

     - 넘어갈 path를 파라메터로 던짐.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <script src="<%=jsPath%>/jquery-1.7.2.js"></script>
    <script>
    $(document).ready(function(){
        var mobileUrl = "/mobile/index.html";
        var contextPath = "/mobile/";
        var currUrl = location.href;
        if(currUrl.indexOf( contextPath )>-1){
            location.href = mobileUrl + "?path=" + currUrl.substring(currUrl.indexOf( contextPath ) + contextPath.length);
        } else {
            $("body > table").show();
        }
    });
    </script>
    Colored by Color Scripter
    cs



    app.module.ts에 PageNotFoundComponent(404에러) 컴포넌트를 생성했다.

    1
    2
    3
    4
    5
    const appRoutes: Routes = [
      {path:'', component:ApprovalListComponent, canActivate : [AuthGuard] },
       .......
      {path:'**', component:PageNotFoundComponent},
    ];
    Colored by Color Scripter
    cs



    PageNotFoundComponent에서 URL Parameter에 path가 있는경우, 이동시키도록 설정

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    import { Component, OnInit } from '@angular/core';
    import { Router, ActivatedRoute } from '@angular/router';
     
    @Component({
      selector: 'app-page-not-found',
      templateUrl: './page-not-found.component.html',
      styleUrls: ['./page-not-found.component.css']
    })
    export class PageNotFoundComponent implements OnInit {
     
        constructor(
                private activatedRoute: ActivatedRoute,
                private router:Router
          ) { }
     
          ngOnInit() {
              const path = this.activatedRoute.snapshot.queryParams['path'];
              if(path){
                  this.router.navigate([path]);
              }
          }
     
    }
     
    Colored by Color Scripter
    cs




    처음 시도했던 방법(guard에다가 path값얻어서 navigate처리... 또는 app.component.ts에 처리?)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    import { Injectable } from '@angular/core';
    import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
    import { Observable } from 'rxjs/Observable';
    import { Router, ActivatedRoute } from '@angular/router';
    import { ApiService } from '../service/api.service';
    import $ from 'jquery/dist/jquery';
    declare var $: $;
     
    @Injectable()
    export class AuthGuard implements CanActivate {
      
      constructor(
        private activatedRoute: ActivatedRoute, 
        private apiService:ApiService,
        private router:Router
      ) { }
      
      canActivate(
        next: ActivatedRouteSnapshot,
    //    state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
        state: RouterStateSnapshot): boolean {
            
          this.apiService.sessionCheck().then((rtn_code)=>{
            const path = this.activatedRoute.snapshot.queryParams['path'];
            if(rtn_code!="SUCCESS"){
              location.href = "/login.do";
            } else if(path){
              this.router.navigate([path]);
            }
          });
          setTimeout(function(){
            $("footer").css("position", "absolute");
          },300);
        
          return true;
      }
    }
     
    Colored by Color Scripter
    cs


    저작자표시 (새창열림)

    'Front-End > Angular' 카테고리의 다른 글

    [Angular] 콜백function안에서 다른 function호출시 에러발생할때  (0) 2018.02.19
    angular 기초정리  (0) 2018.02.19
    [Angular] Error: Cannot find module '@angular-devkit/core'에러  (0) 2018.02.03
      'Front-End/Angular' 카테고리의 다른 글
      • [Angular] 콜백function안에서 다른 function호출시 에러발생할때
      • angular 기초정리
      • [Angular] Error: Cannot find module '@angular-devkit/core'에러
      설 연수
      설 연수

      티스토리툴바