运维开发网

Spring MVC Login Demo1

运维开发网 https://www.qedev.com 2020-03-02 08:09 出处:51CTO 作者:zjy1002261870
加上上一篇拦截器文章代码综合一下<body><div><inputtype="hidden"id="txtHiddenCtx"value="${pageContext.request.contextPath}"/>用户名:<inputtype="text"id="tx

加上上一篇拦截器文章代码综合一下

<body>

<div>

<input type="hidden" id="txtHiddenCtx" value="${pageContext.request.contextPath}" />

用户名:<input type="text" id="txtuserid"/><br>

密码:<input type="password" id="txtpassword"/>

<input type="button" value="登录" onclick="loginc();"/>

</div>

<script type="text/javascript">

var ctx = $("#txtHiddenCtx").val();

function loginc() {

var paramData = new Object();

paramData.userid = $("#txtuserid").val();

paramData.password = $("#txtpassword").val();

    var jsonData = JSON.stringify(paramData);

    $.ajax({
        url : ctx + "/auth/loginc",
        type : "POST",
        async : true,
        // contentType:"application/x-www-form-urlencoded",
        contentType : "application/json;charset=utf-8",
        dataType : "json",
        data : jsonData,
        beforeSend : function(XMLHttpRequest) {
        },
        success : function(data, textStatus) {
            if(data.flag == 1){
                window.location.href = ctx + "/";
            }else{
                alert(data.msg);
            }
        },
        error : function(XMLHttpRequest, textStatus, errorThrown) {
            // 通常情况下textStatus 和errorThown只有其中一个有值

            alert("error: " + XMLHttpRequest.responseText);

        },
        complete : function(XMLHttpRequest, textStatus) {
        }
    });

}

</script></body>

</html>

@Controller
br/></body>

</html>

@Controller

@RequestMapping(value="/")
public String index() {
    return "index";
}

}

@Controller@RequestMapping(value="/auth")
br/>@RequestMapping(value="/auth")

@RequestMapping(value="/loginc",method=RequestMethod.POST)
public void LoginPage(@RequestBody Map<String, Object> map,HttpServletRequest request,HttpServletResponse response) {
    Map<String,Object> result = new HashMap<String,Object>();
    String userid = String.valueOf(map.get("userid"));
    String password = String.valueOf(map.get("password"));
    if(StringUtils.isEmpty(userid)) {
        result.put("flag", 0);
        result.put("msg", "userid isEmpty");
         MvcHelper.WriteResponseInfo(response,result, request.getCharacterEncoding());
    }
    if(StringUtils.isEmpty(password)) {
        result.put("flag", 0);
        result.put("msg", "password isEmpty");
         MvcHelper.WriteResponseInfo(response,result, request.getCharacterEncoding());
    }
    if("590031i".equals(userid) && "123".equals(password)) {
        result.put("flag", 1);
        HttpSession session = request.getSession();
        session.setMaxInactiveInterval(1800);  //设置过期时间,单位是秒
        session.setAttribute("userid", userid);

    }else {
        result.put("flag", 0);
        result.put("msg", "id or password error.");
    }
     MvcHelper.WriteResponseInfo(response,result, request.getCharacterEncoding());
}

@RequestMapping(value="/loginout",method=RequestMethod.POST)
public void LoginOut(@RequestBody Map<String, Object> map,HttpServletRequest request,HttpServletResponse response) {
    Map<String,Object> result = new HashMap<String,Object>();
    HttpSession session = request.getSession();
    session.invalidate();
    result.put("flag", 1);
     MvcHelper.WriteResponseInfo(response,result, request.getCharacterEncoding());
}

}

0

精彩评论

暂无评论...
验证码 换一张
取 消