SSM框架,ajax实现登陆界面验证和登陆成功后页面跳转问题
賬號、密碼和驗證碼都正確后,使用了ajax實現驗證,驗證結束后不能像正常一樣返回一個字符串,用視圖解析器來跳轉頁面
<!--配置JSP 顯示ViewResolver(視圖解析器)--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property><property name="prefix" value="/jsp/"></property><property name="suffix" value=".jsp"></property> </bean>登陸界面的ajax代碼如下:
<!--AJAX方法 ,不用加載整個界面,對界面進行局部的部刷新--> <script type="text/javascript">function isLogin(){//獲取用戶名,賬號和驗證碼的值,放在url參數傳遞到Controller控制器var loginName = $("#loginName").val();var password = $("#password").val();var code = $("#code").val();$.ajax({url:"${pageContext.request.contextPath}/user/Login?loginName="+loginName+"&password="+password+"&code="+code,type:"post",data:"null",success:function(result){//把son字符串或json數組轉換為json對象var json = JSON.parse(result);//i是key值,c是value值,既json[i]。比如 json={"tom":"zs"},i=tom,c=zs=json[i]$.each(json,function(i,c){if(json[i]=="登陸成功"){//使用了ajax,登陸成功了以這樣的方式來跳轉頁面window.location.href="http://localhost:8080/hotel/jsp/uindex.jsp";}else{if(i=="wrongPassword"){//如果是賬號或密碼錯誤,則清空賬號、密碼,驗證碼不清空$("#loginName").val("");$("#password").val(""); }else if(i=="wrongCode"){//如果只是驗證碼錯誤,清空驗證碼,賬號和密碼不清空$("#code").val(""); }//輸出彈窗提示,比如:賬號或密碼錯誤 或 驗證碼錯誤,請重新輸入alert(c);}});},error:function(){alert("失敗");//window.location.href="http://localhost:8080/hotel/jsp/uindex.jsp";}});} </script>Controller控制器的代碼如下:
@Controller @RequestMapping("/user") public class UserController {@AutowiredUserService userService;@AutowiredUser users;@RequestMapping("/Login")//登陸public String login(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "loginName") String loginName, @RequestParam(value="password")String password, @RequestParam(value = "code") String code){try {System.out.println(loginName+":"+password+":"+code);users.setUserName(loginName);users.setPasswordd(Integer.valueOf(password));User use = userService.login(users);// 驗證驗證碼,在CodeServlet中已經保存在了session中String sessionCode = request.getSession().getAttribute("code").toString();System.out.println(sessionCode+"123");if (use == null){System.out.println("用戶名或密碼錯誤");//設置字符集response.setContentType("text/html;charset=utf-8");//把基礎類型的數據變成jonJSONObject json = new JSONObject();json.put("wrongPassword", "賬號或密碼錯誤");//ajax回調response.getWriter().println(json); }else{if(!code.equalsIgnoreCase(sessionCode)){System.out.println("用戶名和密碼正確但驗證碼錯誤");JSONObject json = new JSONObject();json.put("wrongCode","驗證碼錯誤,請重新輸入");//ajax回調response.getWriter().println(json);}else{//登陸成功,把use放入session中,別的頁面要用到System.out.println("登陸成功,我是:"+use.getNamee());request.getSession().setAttribute("user", use);JSONObject json = new JSONObject();json.put("success","登陸成功");//ajax回調response.getWriter().println(json);//return "uindex";//request.getRequestDispatcher("jsp/uindex.jsp").forward(request, response);}}}catch (Exception e){e.printStackTrace();}return null;} }1.登陸成功后跳轉頁面的第一種方式:
賬號,密碼和驗證碼都驗證成功后,在Controller最后的
else{
? ? ? //登陸成功,把use放入session中,別的頁面要用到
? ? ? request.getSession().setAttribute("user", use);
? ? ?JSONObject json = new JSONObject();
? ? ?json.put("success","登陸成功");
? ? ?response.getWriter().println(json);
}
語句里添加了,json作為標記,正常返回。
success:function(result){
? ? ? ?var json = JSON.parse(result);? ? ? ? ??//把son字符串或json數組轉換為json對象
? ? ? ? $.each( json,function(i,c){
? ? ? ? ? ? if(json[i]=="登陸成功"){
? ? ? ? ? ? ? ? ? ? ? ?//使用了ajax,登陸成功以這樣的方式來跳轉頁面
? ? ? ? ? ? ? ? ? ? ? window.location.href="http://localhost:8080/hotel/jsp/uindex.jsp";
})
}
利用window.location.href來跳轉頁面。
?
2.登陸成功后跳轉頁面的第二種方式:
把之前的json標記的三行代碼刪除掉,
加入 request.getRequestDispatcher("jsp/uindex.jsp").forward(request, response);(ps:我也不知道為什么要這樣),如下:
else{
//登陸成功,把user放入session中,別的頁面要用到
request.getSession().setAttribute("user", use);
request.getRequestDispatcher("jsp/uindex.jsp").forward(request, response);
}
然后ajax沒有返回值就返回失敗了,就回調到
error:function(){
?window.location.href="http://localhost:8080/hotel/jsp/uindex.jsp";
}
然后在該函數里同樣利用 window.location.href 進行頁面跳轉。
?
總結
以上是生活随笔為你收集整理的SSM框架,ajax实现登陆界面验证和登陆成功后页面跳转问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 架构设计方法学
- 下一篇: 题注自动带章节编号 and怎样删除Wor