使用SpringMVC+Angularjs实现登录功能

一、使用idea+maven配置Spring MVC环境

二、创建登录页面login.html


三、创建js文件并引用到html文件中

    angular.module("myApp",[])
            .controller("loginCtrl",function ($scope,$http) {
                $scope.userData={}
                $scope.login = function(){
                    $http({
                        method:"post",
                        url:"/user/login",
                        params:{
                            "username":$scope.userData.username,
                            "password":$scope.userData.password
                        }
                    }).success(function(data){
                        if(data.success){
                            window.location="http://localhost:8080/static/template/index.html"
                        }else{
                           document.getElementById("showError").innerHTML="用户名或密码错误";
                        }
                    }).error(function() {
                        window.location="http://localhost:8080/static/template/500.html"
                    });
                }
            })

四、编写UserController

package com.grandinsight.business.controller;

import com.grandinsight.business.model.User;
import com.grandinsight.business.service.IUserService;

@Controller
@RequestMapping("/user")
public class UserController extends SupperController { 

    @Autowired public IUserService userService; 
    @RequestMapping(value = "/login") 
    @ResponseBody 
    public String login(String username,String password){ 
          boolean result =false; 
          User user1 = userService.getUserByName(username);
            if(password.equals(user1.getPassword())){ 
                    result = true; 
            } 
              return "index" ; 
      }

五、编写Service,DAO层getUserByName()方法的代码即可实现Springmvc+AngularJS的登录功能

你可能感兴趣的:(使用SpringMVC+Angularjs实现登录功能)