如何在jsp页面中使用ng-view for spring

时间:2015-07-31 07:58:43

标签: angularjs spring jsp spring-mvc angularjs-ng-route

尝试使用角度和弹簧通过简单的http会话实现用户身份验证。

index.jsp(我的主页)中,正在使用ng-view 但由于$routeProvider无法从templateUrl找到网址/login的模板(我认为这是 错误的原因 ),因此只有index.jsp页面被渲染,而ng-view未呈现, 错误 也会出现。<登记/>
下面附有错误和相关代码以及我的文件夹结构的快照。

根据我的有限知识,这是我通过templateUrl引用我的模板的方式,导致错误

enter image description here


错误代码段

Error: [$injector:unpr] Unknown provider: $templateRequestProvider <-   $templateRequest <- $route <- ngViewDirective

的index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html ng-app="PMApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>AUTH</title>
<script data-require="angular.js@*" data-semver="1.2.13"
src="http://code.angularjs.org/1.2.13/angular.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.3/angular-route.js">   </script>
<script type="text/javascript" src="<c:url value="/resources/js/app.js" />"></script>
<script type="text/javascript" src="<c:url value="/resources/js/LoginController.js" />"></script>
<script type="text/javascript" src="<c:url value="/resources/js/MainController.js" />"></script>
</head>
<body>
        <h2>Hello World!Just trying the AUTHENTICATION . . .</h2>
         <div ng-view></div>              

    </body>
    </html>

app.js

var PMApp = {};
var App = angular.module('PMApp', ['ngRoute']);

App.config(['$routeProvider', function($routeProvider) {
   console.log("in routeprovider");
        $routeProvider.when('/login', {

            templateUrl: '/resources/views/loginLayout.html',

            controller: LoginController

        });
        $routeProvider.when('/main', {

            templateUrl: 'resources/views/mainLayout.html',

            controller: MainController

        });
        $routeProvider.otherwise({redirectTo: '/login'});
     }]);

App.run(function($rootScope, $location) {

    // register listener to watch route changes

    $rootScope.$on("$routeChangeStart", function(event, next, current) {


        console.log("Routechanged sessionId="+$rootScope.SessionId);


         if ($rootScope.SessionId == '' || $rootScope.SessionId == null) {


             // no logged user, we should be going to #login

            if (next.templateUrl == "resources/views/loginLayout.html") {

                // already going to #login, no redirect needed

            } else {

                // not going to #login, we should redirect now

                $location.path("/login");

            }

        }

    });

});

如果需要,请询问更多细节。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

这是创建问题的版本。将它更新为1.3.1,一切都像宝石......