使用SpringMVC+Angularjs实现登录功能

来源:互联网 发布:君子去仁 恶乎成名赏析 编辑:程序博客网 时间:2024/05/17 10:07

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

二、创建登录页面login.html

<form class="m-t" role="form" ng-app="myApp" ng-controller="regCtrl">            <div class="form-group">                <input type="email" class="form-control"                       name="username" id="username"                       placeholder="请输入用户名"                       ng-model="username"                       required="">            </div>            <div class="form-group">                <input type="password" name="password"                       id="password" class="form-control"                       placeholder="请输入密码"                       ng-model="password"                       required="">            </div>            <button ng-submit="regis()" id="send" class="btn btn-primary block full-width m-b">登 录</button>

三、创建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){                            //登录成功执行的代码                        }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;import com.grandinsight.framework.controller.SupperController;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.HttpRequest;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import javax.servlet.http.HttpServletRequest;@Controller@RequestMapping("/user")public class UserController extends SupperController {    @Autowired    public IUserService userService;    @RequestMapping(value = "/login")    @ResponseBody    public String login(String username,String password)s{        boolean result =false;        User user1 = userService.getUserByName(username);        if(password.equals(user1.getPassword())){            result = true;        }        return "index" ;    }

五、编写Service,DAO层的代码

0 0
原创粉丝点击