0

Refreshing angular2 dart page using HashLocationStrategy works fine as it opens the exact same view.

Refreshing the page using PathLocationStrategy - with tomcat server configured to serve index.html - works for the url without parameter but does not work for the url with parameter.

localhost:8090/menu1 // refresh works
localhost:8090/menu2/paramVal // does not refresh

tomcat web.xml has

  <error-page>
    <error-code>404</error-code>
    <location>/index.html</location>
  </error-page>

main.dart

main() {  
      bootstrap(AppComponent, [  
      ROUTER_PROVIDERS,  
      provide(APP_BASE_HREF, useValue: '/')]);  
}  

app_component.dart

import 'package:angular2/core.dart';
import 'package:angular2/router.dart';
import 'package:angular2/angular2.dart';
import 'package:mboxes/menu1.dart';
import 'package:mboxes/menu2.dart';

@Component(
    selector: 'my-app',
    templateUrl: 'app_component.html',
    directives: const [ROUTER_DIRECTIVES],
    providers: const[ROUTER_PROVIDERS, ])

@RouteConfig(const [
  const Route(
      path: '/menu1',
      name: 'Menu1',
      component: Menu1Component,
      useAsDefault: true),
  const Route(
      path: '/menu2/:param', name: 'Menu2', component: Menu2Component)
])
class AppComponent {}

app_component.html

<div class="container">
    <nav>
        <ul>
            <li>
                <a [routerLink]="['Menu1']">Menu1</a>
            </li>
            <li> <a [routerLink]="['Menu2', {'param':'paramVal'}]">Menu2</a> </li>
        </ul>
    </nav>

    <div style="padding-left: 200px; padding-top: 200px; padding-bottom: 50px">
    <router-outlet></router-outlet>
    </div>
</div>

menu1.dart

import 'package:angular2/core.dart';
import 'package:angular2/router.dart';
@Component(
    selector: 'menu1',
    template: ''' menu 1 was clicked '''
)
class Menu1Component {}

menu2.dart

import 'package:angular2/core.dart';
import 'package:angular2/router.dart';
@Component(
    selector: 'menu2',
    template: ''' menu 2 was clicked'''
)
class Menu2Component implements OnInit {
  final RouteParams _routeParams;
  Menu2Component(this._routeParams);
  ngOnInit()  {
    var val  = _routeParams.get('param');
    print  ("passed param is " +  val);
  }
}
marcg
  • 552
  • 1
  • 8
  • 20
  • Is the application served at the root folder or is `heros` a detail folder. Normally there is nothing else to do than configuring `PathLocationStrategy` (default) and rewrite requests to non-existing URLs to `index.html`. – Günter Zöchbauer Feb 10 '17 at 15:44
  • I have only two folders - web folder contains index.html and lib folder contains all other components – marcg Feb 10 '17 at 19:45
  • Application is being served from the root folder. URLs with parameter do not refresh. URLs without parameter refresh fine. – marcg Feb 10 '17 at 23:19
  • Can you update the question to make it more clear what error you see? What does it mean when it "doesn't work"? – Nate Bosch Mar 07 '17 at 00:44

1 Answers1

1

Instead of using 404 to serve index.html I think you want to set up a servlet-mapping with something like <url-pattern>*</url-pattern>

See also Tomcat servlet, redirect all urls to a single webpage

Community
  • 1
  • 1
Nate Bosch
  • 10,145
  • 2
  • 26
  • 22