Javascript 如何修复登录到Ajax成员时出现的会话错误?-弹簧MVC2

Javascript 如何修复登录到Ajax成员时出现的会话错误?-弹簧MVC2,javascript,java,jquery,ajax,spring-mvc,Javascript,Java,Jquery,Ajax,Spring Mvc,MemberService.java: public HashMap<String, Object> login(String id,String pw) { HashMap<String, Object> result = memberDao.selectOne(id); if(result != null) // 존재하는 값이 있으면 { String opwd = (String) result.get("pw"); //

MemberService.java:

public HashMap<String, Object> login(String id,String pw)
{
    HashMap<String, Object> result = memberDao.selectOne(id);

    if(result != null) // 존재하는 값이 있으면
    {
        String opwd = (String) result.get("pw"); // opwd = 존재하는값의 pw값을 가져온 값

        if(opwd.equals(pw)) 
        {
            return result; // true면 존재하는값을 반환
        }
        else
        {
            return null;
            //return null; // 아니면 값을 반환하지 않음.
        }
    }
    else // 존재하는 값이 없다면
    {
        return null;
    }
}
因此,该项目表现良好。但代码似乎效率低下

修改前出现错误的原因是 我认为如果它不在HashMap中,它将返回一个空值

我现在有三个问题

  • 我想知道我认为的问题是否正确

  • 如何才能更有效地编写代码

  • 您应该如何处理MemberService中的异常处理


  • 如果您的代码运行良好,并且您只关心如何改进它,您应该将其发布到。我在那里发布了一个问题。谢谢@nisargshah如果您的代码运行良好,并且您只关心如何改进它,您应该将其发布到。我在那里发布了一个问题。谢谢你@NisargShah
    @RequestMapping("loginOK.do")
    @ResponseBody
    public String login(@RequestParam(required=false) String id, @RequestParam(required=false) String pw,HttpSession session, HttpServletRequest request, HttpServletResponse response) throws IOException 
    {
        HashMap<String, Object> loginIdentify = mService.login(id,pw);
        System.out.println("loginIdentify is that :  " + loginIdentify);
    
        session.setAttribute("id", loginIdentify.get("id"));
    
        if(loginIdentify == null)
        {
            return "0"; // member isn't exist
        }
        else
        {
            return "1"; // member is exist
        }
    }
    
    <%@ page language="java" contentType="text/html; charset=EUC-KR"
        pageEncoding="EUC-KR"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
    <link rel="stylesheet" href="css/normalize.css">
    <link rel="stylesheet" href="css/swiper.min.css">
    <link rel="stylesheet" href="css/loginPage.css">
    <script type="text/javascript" src="js/jquery-3.1.1.min.js"></script>
    <script type="text/javascript" src="js/swiper.jquery.min.js"></script>
    <title>Insert title here</title>
    </head>
    <body>
    <jsp:include page="header.jsp" />
    <div class="loginPage_main">
        <div class="pageName">
            <img class="pageName_image" src="images/greeting_title.jpg">
            <div class="pageName_explanation">
                <p>로그인</p>
            </div>
        </div>
        <div class="location">
        </div>
        <div class="loginForm_area">
            <div class="hidden_area">
                <div class="img_lock_area">
                    <img src="images/lock.png" class="img_lock">
                </div>
                <form action="loginOK.do" id="login_frm" name="frm" method="post" onsubmit="return checkValue()">
                    <div class="input_zone">
                        <div class="input_id">
                            <span>&nbsp;&nbsp;ID</span><input type="text" placeholder="아이디를 입력하세요" style="width:280px;margin-left: 10px;" name="id">
                        </div>  
                        <div class="input_pw">
                            <span>PW</span><input type="password" placeholder="비밀번호를 입력하세요" style="width:280px;margin-left: 10px;" name="pw">
                        </div>
                    </div>
                </form>     
                    <div class="loginBtn" onclick="document.getElementById('login_frm').onsubmit();">
                        <div style="margin-top: 22px;">
                            <span class="loginOK">Login</span>
                        </div>
                    </div>
            </div>
            <div class="loginForm_subBtn_area">
                <div class="btn_value_join">
                    <a class="btn_join" onclick="location.href='move_joinMemberPage.do'">회원가입</a>
                </div>
                <div class="btn_value_find_id">
                    <a class="btn_find_id">아이디 찾기</a>
                </div>
                <div class="btn_value_find_pw">
                    <a class="btn_find_pw">비밀번호 찾기</a>
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript">
    
        function checkValue() 
        {
            var form = document.frm;
            var id = form.id.value;
            var pw = form.pw.value;
    
            if(!form.id.value || !form.pw.value)
            {
                alert('아이디 혹은 비밀번호를 입력하세요.');
                return false;
            }
    
            $.ajax
            ({
                type: 'POST',
                url: 'loginOK.do',
                dataType:"text",
                data:{"id":id,"pw":pw},
                success: function(data)
                {
                    if(data == 1)
                    {
                        window.location.href='main.do';
                    }
                    else
                    {
                        alert('아이디 혹은 비밀번호를 확인하세요.');
                    }
                },
                error: function()
                {
                    alert('error');
                }
            })
        }       
    </script>
    </body>
    </html>
    
    if(loginIdentify == null)
    {
        return "0"; // member isn't exist
    }
    else
    {
        session.setAttribute("id", loginIdentify.get("id"));
        return "1"; // member is exist
    }