angularJs的简单理解和使用(登录)

来源:互联网 发布:淘宝买账号的靠谱吗 编辑:程序博客网 时间:2024/06/15 09:15

1.引用过来angular-1.0.1.min.js

2.在登录页面添加引用,放在head区域中,页面记得不要加form表单

<%    String path = request.getContextPath();    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html ng-app=""><head>    <title>Title</title>    <script src="/resources/angularJs/angular-1.0.1.min.js"></script></head><body ng-controller="myController">用户名:<input type="text" name="name" id="name" ng-model="user.name"><br>密码:<input type="password" name="password" id="password" ng-model="user.password"><br><button ng-click="getUser()">登录</button></body><script>    function myController($scope, $http){        //复初值        $scope.user = {            name:"师",            password:"456"        };        $scope.getUser = function(){            $http({                method: "POST",                url: "<%=basePath%>/user/login.action",                data: $scope.user,                dataType:"json"            }).success(function (data){                if(data.flag == 1)                {                    window.location.href = "<%=basePath%>/user/selUser.action";                }                else                {                    alert("登录失败");                }            }).error(function(){                alert("登录失败");            })        };    }</script></html>

3..controller方法,要标注@ResponseBody,入参要对象前加@RequestBody,返回值类型用Map<String,Object>

@ResponseBody    @RequestMapping(value = "/login", method = RequestMethod.POST)    private Map<String,Object> login(@RequestBody User user, HttpSession session) {        Map<String,Object> map = new HashMap<>();        User my = userService.findUser(user);     if(my != null)        {            map.put("flag",1);            session.setAttribute("name", user.getName());         }        else        {            map.put("flag",0);        }        return map;    }


原创粉丝点击