I'm writing a system where the frontend is in AngularJS and the backend is in Java with Jersey.
But I am having problems using $locationProvider.html5Mode (true);to remove the "#" in url.
If I click in a link on the page, the correct view is shown. But if I press F5 or copy and paste the URL in the browser, the server looks for a resource that doesn't exist and says that it is not available.
I can't redirect, on the server, links to the entry point of the webapp, such as requested here https://docs.angularjs.org/guide/$location.
Also, I tried doing what they said here about servlets AngularJS HTML5 Mode - How do direct links work without server specific changes?, but for me it didn't work.
Does anyone have idea how to make it work?
In NodeJS, it would use app.use ('/', express.static (__ dirname + '/ public')) ;.
Below is the code I'm using:
appRoutes.js
angular.module('appRoutes', [])
.config(
[ '$routeProvider', '$locationProvider',
function($routeProvider, $locationProvider) {
$routeProvider
.when('/', {
templateUrl : 'views/home.html',
controller : 'HomeController'
})
.when('/login', {
templateUrl : 'views/login.html',
controller : 'LoginController'
}).otherwise({
redirectTo : '/'
});
$locationProvider.html5Mode(true);
} ])
.run(function($rootScope, $location, Login) {
$rootScope.$on("$locationChangeStart", function(event, next, current) {
if (!Login.isLogged()) {
$location.path('/login');
}
});
});
web.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
index.html
Resources
org.glassfish.jersey.servlet.ServletContainer
jersey.config.server.provider.packages
sammubr.resources
jersey.config.server.provider.classnames
sammubr.filtro.FiltroSeguranca;org.glassfish.jersey.filter.LoggingFilter
1
Resources
/api/*
Login
org.glassfish.jersey.servlet.ServletContainer
jersey.config.server.provider.packages
sammubr.authenticate
Login
/authenticate/*
Logout
org.glassfish.jersey.servlet.ServletContainer
jersey.config.server.provider.packages
sammubr.logout
Logout
/logout/*
index.html
SampleApp