java - Spring Boot 和 AngularJS

标签 java angularjs spring spring-mvc spring-boot

我想制作一些 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/

相关文章:

Javascript 除数

java - 从字符串输入构建树

angularjs - Firefox 上 Karma 中的 ng-blur

javascript - Angular 1.6 : Factory issue initializing values

java - Spring RestTemplate 不会使用超时设置

java - 如何解决错误: cannot find symbol message.回复(...); Vertx 点对点

java - Java 对象能否判断对它的引用何时超出范围?

angularjs - Firebase - 按列过滤

java - Spring 批处理元素分割器

java - 当我们使用 getBean 方法创建原型(prototype) bean 时,是否会创建 beans 属性(singleton)的新实例?