当使用angularjs到弹簧控制器进行请求时,如何重定向到其他页面

时间:2016-02-29 07:11:38

标签: angularjs spring-mvc call using

#MY Login page code snippet
--------------------------------

    <div class="login-container">
            <form ng-submit="onLogin()" name="form" class="pure-form login-form"
                ng-controller="LoginCtrl as vm">
                <fieldset class="pure-group">
                    <legend>Log In</legend>
                    <label for="inputUsername" class="sr-only">Username</label> <input type="text" id="inputUsername" class="form-control"
                        placeholder="Username" required autofocus ng-model="vm.username">
                    <label for="inputPassword" class="sr-only">Password</label> <br>
                    <input type="password" id="inputPassword" class="form-control"
                        placeholder="Password" required ng-model="vm.password">
                </fieldset>

                <button type="submit"
                    class="pure-button pure-input-1 pure-button-primary">Log In</button>
                <div class="alert alert-danger" role="alert" ng-show="errorMsg">{{errorMsg}}</div>

            </form>
        </div>


        <div id="content" class="col-lg-10 col-sm-10">
            <div class="row">
                <div class="box col-md-12">
                    <div class="box-inner"></div>
                </div>
            </div>
        </div>


# login.js
-----------------

angular.module('loginApp', [])
        .controller('LoginCtrl', ['$scope', '$http', function ($scope, $http) {
            $scope.onLogin = function () {

        var encodedString = '?username=' +
                    encodeURIComponent($scope.vm.username) +
                    '&password=' +
                    encodeURIComponent($scope.vm.password);             

                var url = "http://localhost:8080/servicesummary/employees/view"+encodedString;

                $http({
                    method: 'GET',
                    url: url                
                })
                .success(function(data) {
                    //alert(JSON.stringify(data));
                    var container = 'box-inner';

                    $('.' + container).html(response);  


                })
                .error(function(data) {
                    $scope.$error = 'Unable to submit form';
                })
            };

        }]);


# Spring Controller-
--------------------------

  @RequestMapping(value = "/view", method = RequestMethod.GET, produces = "application/json")
 public ModelAndView getEmployeeInJSON(@RequestParam("username") String username, @RequestParam("password") String password) {

            final ModelAndView mv = new ModelAndView();
            try {
                AppUserDTO appUserdb = userService.getObject(username);

                if (null != appUserdb && (appUserdb.getPassword() != null)) {
                    String decodedPass = UtilityService.decode(appUserdb.getPassword());
                    if (decodedPass.equals(password)) {

                    System.out.println("Database Match.............");
                    mv.setViewName(VIEWNAME_HOME);
                    } else {
                        mv.setViewName(VIEWNAME_LOGIN);
                    }
                } else {
                    mv.addObject("uNameNotValid", false);
                    mv.setViewName(VIEWNAME_LOGIN);
                }    
            } catch (Exception e) {
                logger.error("ERROR handleGetLogin() {}", e);
                System.out.println("Exception is occured.....");
                return new ModelAndView("redirect:/home-exception");    
            }    
            return mv;
        } 

您好我有用户输入的用户名和密码,我尝试使用来自angularjs的spring controller(TestController.java)从数据库验证。我打算使用login.js编写调用控制器,但无法处理使用angularjs返回的ModelAndView对象。请参阅我的login.js以供参考。

1 个答案:

答案 0 :(得分:0)

您可以更新$ http方法,如下所示

$http({
    method: 'GET',
    url: url                
})
.success(function(data) {
    //alert(JSON.stringify(data));
    $window.location.href = 'SuccessUrl'; 


})
.error(function(data) {
    $scope.$error = 'Unable to submit form';
})