2016-09-04 20 views
1

私はいくつかの春のブートアプリを作りたいです。私はすでに春のブートプロジェクトを作成しましたが、私は問題があります。私のコントローラは次のようになります:Spring Boot and AngularJS

@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

を@Controllerする@RestControllerを変更する必要がありますようにあり、そのようなクラスでなければなりません:

@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 .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はviewリゾルバを使用しません。あなたは