SSM框架,ajax实现登陆界面验证和登陆成功后页面跳转问题
賬號(hào)、密碼和驗(yàn)證碼都正確后,使用了ajax實(shí)現(xiàn)驗(yàn)證,驗(yàn)證結(jié)束后不能像正常一樣返回一個(gè)字符串,用視圖解析器來(lái)跳轉(zhuǎn)頁(yè)面
<!--配置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方法 ,不用加載整個(gè)界面,對(duì)界面進(jìn)行局部的部刷新--> <script type="text/javascript">function isLogin(){//獲取用戶名,賬號(hào)和驗(yàn)證碼的值,放在url參數(shù)傳遞到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數(shù)組轉(zhuǎn)換為json對(duì)象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,登陸成功了以這樣的方式來(lái)跳轉(zhuǎn)頁(yè)面window.location.href="http://localhost:8080/hotel/jsp/uindex.jsp";}else{if(i=="wrongPassword"){//如果是賬號(hào)或密碼錯(cuò)誤,則清空賬號(hào)、密碼,驗(yàn)證碼不清空$("#loginName").val("");$("#password").val(""); }else if(i=="wrongCode"){//如果只是驗(yàn)證碼錯(cuò)誤,清空驗(yàn)證碼,賬號(hào)和密碼不清空$("#code").val(""); }//輸出彈窗提示,比如:賬號(hào)或密碼錯(cuò)誤 或 驗(yàn)證碼錯(cuò)誤,請(qǐng)重新輸入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);// 驗(yàn)證驗(yàn)證碼,在CodeServlet中已經(jīng)保存在了session中String sessionCode = request.getSession().getAttribute("code").toString();System.out.println(sessionCode+"123");if (use == null){System.out.println("用戶名或密碼錯(cuò)誤");//設(shè)置字符集response.setContentType("text/html;charset=utf-8");//把基礎(chǔ)類型的數(shù)據(jù)變成jonJSONObject json = new JSONObject();json.put("wrongPassword", "賬號(hào)或密碼錯(cuò)誤");//ajax回調(diào)response.getWriter().println(json); }else{if(!code.equalsIgnoreCase(sessionCode)){System.out.println("用戶名和密碼正確但驗(yàn)證碼錯(cuò)誤");JSONObject json = new JSONObject();json.put("wrongCode","驗(yàn)證碼錯(cuò)誤,請(qǐng)重新輸入");//ajax回調(diào)response.getWriter().println(json);}else{//登陸成功,把use放入session中,別的頁(yè)面要用到System.out.println("登陸成功,我是:"+use.getNamee());request.getSession().setAttribute("user", use);JSONObject json = new JSONObject();json.put("success","登陸成功");//ajax回調(diào)response.getWriter().println(json);//return "uindex";//request.getRequestDispatcher("jsp/uindex.jsp").forward(request, response);}}}catch (Exception e){e.printStackTrace();}return null;} }1.登陸成功后跳轉(zhuǎn)頁(yè)面的第一種方式:
賬號(hào),密碼和驗(yàn)證碼都驗(yàn)證成功后,在Controller最后的
else{
? ? ? //登陸成功,把use放入session中,別的頁(yè)面要用到
? ? ? request.getSession().setAttribute("user", use);
? ? ?JSONObject json = new JSONObject();
? ? ?json.put("success","登陸成功");
? ? ?response.getWriter().println(json);
}
語(yǔ)句里添加了,json作為標(biāo)記,正常返回。
success:function(result){
? ? ? ?var json = JSON.parse(result);? ? ? ? ??//把son字符串或json數(shù)組轉(zhuǎn)換為json對(duì)象
? ? ? ? $.each( json,function(i,c){
? ? ? ? ? ? if(json[i]=="登陸成功"){
? ? ? ? ? ? ? ? ? ? ? ?//使用了ajax,登陸成功以這樣的方式來(lái)跳轉(zhuǎn)頁(yè)面
? ? ? ? ? ? ? ? ? ? ? window.location.href="http://localhost:8080/hotel/jsp/uindex.jsp";
})
}
利用window.location.href來(lái)跳轉(zhuǎn)頁(yè)面。
?
2.登陸成功后跳轉(zhuǎn)頁(yè)面的第二種方式:
把之前的json標(biāo)記的三行代碼刪除掉,
加入 request.getRequestDispatcher("jsp/uindex.jsp").forward(request, response);(ps:我也不知道為什么要這樣),如下:
else{
//登陸成功,把user放入session中,別的頁(yè)面要用到
request.getSession().setAttribute("user", use);
request.getRequestDispatcher("jsp/uindex.jsp").forward(request, response);
}
然后ajax沒(méi)有返回值就返回失敗了,就回調(diào)到
error:function(){
?window.location.href="http://localhost:8080/hotel/jsp/uindex.jsp";
}
然后在該函數(shù)里同樣利用 window.location.href 進(jìn)行頁(yè)面跳轉(zhuǎn)。
?
總結(jié)
以上是生活随笔為你收集整理的SSM框架,ajax实现登陆界面验证和登陆成功后页面跳转问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 架构设计方法学
- 下一篇: 题注自动带章节编号 and怎样删除Wor