Home Angular + Grails: Refresh page or type URL
Reply: 1

Angular + Grails: Refresh page or type URL

Gustavo
1#
Gustavo Published in 2017-11-30 19:23:09Z

I'm building an application with Grails 3.3.2 and Angular (currently Angular 5).

I've set up the Grails to give the index.html, which is located in src/main/webapp, when requests are made for the default URL (e.g. http://localhost:8080). I've also set up the server to process all requests started with /api/ as REST endpoints.

Everything works well. The Angular app is loaded and the user interacts with the app and navigate through the routes correctly, but when the user refreshes the page (e.g. pressing F5), he gets the 404 message (page not found).

This happens because a request is made for the server with the currently URL (e.g. http://localhost:8080/users), which is a valid route in the Angular app, but it does not exist in the server.

I'd like to know if it's possible to the server gives the same index.html, no matter the URL, except URLs started with /api/, and the Angular navigates to the correct route based on the URL. For example, if the user refreshes the page when it's in the users page, the server supplies the index.html and the Angular navigates to the users page.

I've got a set up which every request to the server, except the default URL and URLs started with /api/, goes to a controller and this controller redirects to the default URL. This solved the problem with the 404 but when this happens, the Angular app goes to the first route, instead of the route which the user was in.

I appreciate so much if someone could help me. Thanks.

Gerlando Caldara
2#
Gerlando Caldara Reply to 2017-11-30 19:57:02Z

The main problem is that java is handling the browser requests before the angular routing over index.html can act.

My solution was to place a index controller and redirect everything to the index.html except the api and other stuff. Maybe you can use wildcards /users/**.

@RequestMapping(value = {"/","/users"}, method = RequestMethod.GET)
public String redirect() {
   return "redirect:index.html";
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.471404 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO