Home > Mobile >  How can we redirect to required angular page (routing page) from spring boot controller - angular in
How can we redirect to required angular page (routing page) from spring boot controller - angular in

Time:12-16

I have integrated angular application inside spring boot application.. i.e. angular build files are placed inside static folder of spring boot application like following image. enter image description here

I have defined two angular routing urls like

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

when I access above urls from browser, which are handled by server (spring boot application) directly and not by the angular. so I end up in page not found.

We can redirect to index page from spring boot like this

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

but we can only redirect to index.html page not to routing urls "/pageone" or "/pagetwo".

I don't want to end up in index page.

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

I tried to send attributes along with index.html, but it doesn’t work. How can we fix this problem.

CodePudding user response:

I found a solution..

Access below urls from browser

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

Request will reach spring controller directly and will be handled by below 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 page will be dispatched after that angular automatically load requested page, for example : '/pageone' or 'pagetwo'

app-routing.modules.ts

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

You can access received pathvariable and requestparam as below

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']);
  });
  }

In short, @Controller and implements ErrorController

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

After that angular automatically load requested page (routing url)
  • Related