2016-09-04 3 views
1

스프링 부트 앱을 만들고 싶습니다. 이미 스프링 부트 프로젝트를 만들었지 만 문제가 있습니다. 내 컨트롤러 모양은 다음과 같습니다 :스프링 부트와 앵글러

@RestController 
public class IndexController { 

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

그러나 내가 보는 모든 것은 index.html이 아니라 "index"단어입니다. 따라서 각도를 사용하고 싶습니다.

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: '/'}); 
    }]); 

및 컨트롤러 :

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

    $scope.hello = "Hello from AngularJS"; 

}]); 

내 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> 

하지만 난 내 로컬 호스트에서만 오류가 작동하지 않습니다.

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 

어떻게이 각도 조절기를 작동시키고 뷰를 올바르게 되돌릴 수 있습니까?

답변

0

같은 클래스가 있어야한다 :

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

실제로는 /index을 요청 매핑으로 연결하고 /home이 아니라면 설정이 제대로 작동한다고 생각합니다.

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

당신이 당신의 초기 부팅 설정을 죽이고 어딘가에 어떤 임의의 @EnableWebMvc 주석 설정이없는 가정.

+0

작동하지만, 나에게 유일한 단어 "인덱스"를 보여줍니다. 컨트롤러는 index.html을 검색하지 않습니다. $ routeSegmentProvider를 사용하고 싶습니다. $ routeSegmentProvider .when ('/', 'index') .when ('/ index', 'index') –

+1

그런 다음'@ RestController'를'@ Controller'로 대체하십시오. – EpicPandaForce

+0

I 클래스 이름 앞에 @RequestMapping()을 사용해야합니다. –

1

@EpicPandaForce는 정답입니다. @RestController이 아닌 @Controller을 사용하려고합니다. @RestController@Controller@ResponseBody 인 메타 주석입니다. @Controller은 등록 된 ViewResolver을 검색하지만, @RestController은 검색하지 않습니다.

0

@RestController를 사용하면 spring은 뷰 해석기를 사용하지 않습니다. 당신이 @Controller에 @RestController을 변경해야합니다 있도록

관련 문제