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

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;
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值