Link to home
Start Free TrialLog in
Avatar of ambuli
ambuliFlag for United States of America

asked on

Refreshing Static page in a Spring Boot application

Spring Boot and Angular: Serving static content

Hi Experts,

I have a springboot backend application and an angular front end. I have built the angular front-end copied the contents of dist directory to /src/main/resource/static.  The index.html in the static directory is served when I go to https://localhost:8443 and the links are working.  However, when I refresh the page, it goes to the whitelabel error page.  I have found a proposed solution to this from StackOverflow.  It too doesn't work.
@Controller
public class ForwardController {

    @RequestMapping(value = "/**/{[path:[^\\.]*}")
    public String redirect() {
        // Forward to home page so that route is preserved.
        return "forward:/";
    }
} 

Open in new window

However, using this just prints forward:/ on the refreshed browser.   Also, this too does not look like the solution I want.  I want to just refresh the page.
Any help would be appreciated.  Thank you
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Which version of Angular are you using? I am assuming 1.x?
Avatar of ambuli

ASKER

Angular 6.
Then just setup a redirect on all links (.htaccess) to the index.html page.

This is the code I use
 RewriteEngine on

# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# Rewrite everything else to index.html to allow HTML5 state links
RewriteRule ^ index.html [L]

Open in new window

This can be fixed with Routing strategy  on Angular 6 App,

These have changed a little in Angular 6, please refer these:

https://codecraft.tv/courses/angular/routing/routing-strategies/

with RouterModule.forRoot(routes, {useHash: true})

https://www.javascripttuts.com/getting-started-with-the-new-angular-2-router/
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.