I start my web application with spring boot. It use a simple main class to start an embedded tomcat server:
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
I want to configure the server in the way that he can handle angularjs html5mode that will be activated with
$locationProvider.html5Mode(true);
Relevant postings from other users shows that you need to redirect to the root. the html5 mode remove the hashbag from the url. If you refresh the page the server doesnt find the page cause he do not handle the hash. see: AngularJS - Why when changing url address $routeProvider doesn't seem to work and I get a 404 error
I finally get my Angular 5 application working with spring boot with or without
spring-boot-starter-tomcat
asprovided
(embedded) or not!1- first you create new Controller then copy and paste simple below code
3- remove 2 below item from angular app
2- in angular application you must add
$locationProvider.html5Mode(true);
to app route3- Don't forget to place the base tag before any http request in your index.html file
it's work fine for me
I had same problem. As far as I know, in html5 mode, angularjs don't resolve hash but entered url or url added through pushState.
The problem was that PathResourceResolver map directories but not files. Because it intended to serve requested files from directory but not to rewrite urls. For app it's mean, if you refresh your browser window or type url like http://example.com/mystate, it's query "/mystate" from the server. If spring don't know url, they return 404. One of the solutions is map every possible state to index.html like here (source, btw look at webjars - it's great!). But in my case I can safely map "/**" to index.html and therefore my solution is to override PathResourceResolver#getResource:
I just encountered the similar issue where I wanted to configure Resources and at the same time I wanted to use AngularJS Html5 mode enabled.
In my case my static files were served from
/public
route so I used the following request mapping on my index action and it all works fine.I had the same problem while using angular Html5Mode. The solution that worked for me was to configure error page for 404 in web.xml assigning the path to my Index view in my case "/".
Similarly, you can try configuring error page in spring boot. for reference, you can check this link.
Spring boot and custom 404 error page
A small adjustment to a previous code which works to me.
}