1

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?

4 Answers 4

1

I think your setup should work if you actually bind the /index as the request mapping, and not /home.

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

Assuming you don't have some random @EnableWebMvc annotated config somewhere that kills your initial boot setup.

Sign up to request clarification or add additional context in comments.

3 Comments

it works, but it shows me only word "index". Controller don't search for index.html. I want use angular, and this provider: $routeSegmentProvider .when('/', 'index') .when('/index', 'index')
Then replace @RestController with @Controller
I guess you should use @RequestMapping() before class name.
1

@EpicPandaForce has the correct answer. You want to use @Controller, not @RestController. @RestController is a meta-annotation with @Controller and @ResponseBody. @Controller will search through the registered ViewResolvers, whereas @RestController will not.

Comments

0

When you use @RestController spring does not use the view resolver. So that you should change @RestController to @Controller

Comments

0

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();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.