Valerii Sloboda Valerii Sloboda - 3 months ago 24
AngularJS Question

Spring Boot and AngularJS

I want make some spring boot app. I've already created spring boot project, but I have a problem. My controller look like:

@RestController
public class IndexController {

@RequestMapping(value="/home",method = RequestMethod.GET)
public String homepage(){
return "index";
}
}


But all what i see is not index.html, but only a word "index".
Therefore I want use angular.

angular.module('mvcApp', [])
.constant('MODULE_NAME', "index")
.config(['$routeSegmentProvider', '$routeProvider', function ($routeSegmentProvider, $routeProvider) {
$routeSegmentProvider
.when('/', 'index')
.when('/index', 'index')
.when('/configuration', 'configuration')

$routeSegmentProvider.segment('index', {
templateUrl: 'HTML/index.html',
controller: 'IndexCtrl'
});

$routeProvider.otherwise({redirectTo: '/'});
}]);


and controller:

angular.module('mvcApp').controller('IndexCtrl', ['$scope', '$rootScope', function ($scope, $rootScope) {

$scope.hello = "Hello from AngularJS";

}]);


my index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello!</title>
</head>
<body ng-app="mvcApp" ng-controller="IndexCtrl">

Greeting page.
{{hello}}

</body>
</html>


But it doesn't work, I see only error on my localhost.

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun Sep 04 15:33:41 CEST 2016
There was an unexpected error (type=Not Found, status=404).
No message available


How I can make this angular controller works and return properly views?

Answer

There should be class like that:

@Configuration
public class ConfigurationMVC extends WebMvcConfigurerAdapter {
    @Bean
    public ViewResolver getViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/HTML/");
        resolver.setSuffix(".html");
        return resolver;
    }

    @Override
    public void configureDefaultServletHandling(
            DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}