Jak můžeme přesměrovat do požadované úhlové stránka (směrování page) od jara boot controller - úhlové integrované uvnitř spring boot

0

Otázka

Mám integrovanou úhlovou aplikace uvnitř spring boot aplikaci.. tj. úhlové stavět soubory jsou umístěny uvnitř statické složky spring boot aplikaci jako následující obrázek. enter image description here

I have defined two angular routing urls like

 1. http://localhost:8080/pageone
 2. http://localhost:8080/pagetwo

když jsem přístup k výše uvedené urls z prohlížeče, který are handled by server (spring boot aplikaci) a přímo not by the angular. tak I end up in page not found.

We can redirect to index page od jara boot, jako je tento

@GetMapping("/")
public RedirectView welcome(RedirectAttributes attributes) {
    //attributes.addFlashAttribute("flashAttribute", "redirectWithRedirectView");
    //attributes.addAttribute("pageToRequest", "paymentoverview");
    return new RedirectView("index.html");
}

but můžeme only redirect to index.html stránka not to routing urls "/pageone" or "/pagetwo".

Nechci skončit v indexu stránku.

Somehow I wan't to end up in respective page that I accessed from browser automatically, 
even after redirect to index page also fine.

Snažil jsem se poslat atributy spolu s index.html,, ale to nefunguje. Jak můžeme tento problém vyřešit.

1

Nejlepší odpověď

0

Našel jsem řešení..

Přístup pod url z prohlížeče

1. http://localhost:8080/pageone
2. http://localhost:8080/pagetwo

Žádost dosáhne jaře regulátor přímo a bude zpracována níže controller

@Controller // should not be @RestController
public class BaseController implements ErrorController { // implement ErrorController to handle 404 (error pages)

    // Always redirect to index.html page (only option)

    // Can handle all request (which end up in 404)
    @GetMapping("/error")
    public String error() {
        return "forward:/index.html";
    }

    // Specific request 
    @GetMapping("/pageone")
    public String pageone() {
        return "forward:/index.html";
    }

    // Specific request with pathvariable and requestparam
    @GetMapping("/pagetwo" + "/{id}")
    public String pagetwo(@PathVariable("id") String id,
                                  @RequestParam("param1") String param1,
                                  @RequestParam("param2") String param2 ) {
        // both pathvariable and requestparam will be sent to front end
        return "forward:/index.html";
    }
}

Index stránka bude odeslána po, že úhlové automaticky načíst požadovanou stránku, například : '/pageone "nebo " pagetwo'

aplikace-směrování.moduly.ts

const routes: Routes = [
  {
    path: 'pageone',
    component: PageOneComponent
  },
  {
    path: 'pagetwo/:id', // to handle pathvariables
    component: PageTwoComponent
  },
  {
    path: '**',
    component: HomeComponent
  }
  ]

Můžete přistupovat obdržel pathvariable a requestparam, jak je uvedeno níže

PageTwoComponent.ts

  ngOnInit(): void {
    console.log(this.route.snapshot.paramMap.get('id')); // get pathparms
    this.route.queryParams.subscribe(params => { // get queryparams
      console.log(params['param1']);
      console.log(params['param2']);
  });
  }

Stručně řečeno, @Controller a implements ErrorController

@GetMapping("/error")
public String error() {
   return "forward:/index.html";
}

After that angular automatically load requested page (routing url)
2021-11-24 12:51:24

V jiných jazycích

Tato stránka je v jiných jazycích

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................