我想制作一些 Spring Boot 应用程序。我已经创建了 spring boot 项目,但有一个问题。我的 Controller 看起来像:
@RestController
public class IndexController {
@RequestMapping(value="/home",method = RequestMethod.GET)
public String homepage(){
return "index";
}
}
但是我看到的不是index.html,而是一个“index”字样。 因此我想使用 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: '/'});
}]);
和 Controller :
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
如何使这个 Angular Controller 工作并正确返回 View ?
最佳答案
如果您实际将 /index
绑定(bind)为请求映射,而不是 /home
,我认为您的设置应该有效。
@RequestMapping(value="/index",method = RequestMethod.GET)
public String homepage(){
return "index";
}
假设您在某处没有一些随机的 @EnableWebMvc
注释配置来终止您的初始启动设置。
关于java - Spring Boot 和 AngularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39317587/