controller分支实现前台显示弹框同时转发
controller分支实现前台显示弹框,前台不需要进行什么操作, 前台请求后台的分支即可。
controller层(标红的地方是(alert('账号或密码错误!请重新输入!!!!') window.location.href='/index.jsp'):弹出一个弹框,然后跳转一个页面,如果你只想弹个弹框可以把跳转的页面去掉。):
@RequestMapping("/isLogin") public Object isLogin(HttpServletRequest request, String uname, String upwd, HttpServletResponse response) throws IOException { Userinfo userinfo=new Userinfo(); userinfo.setUname(uname); userinfo.setUpwd(upwd); Userinfo login = service.login(userinfo); response.setContentType("text/html;charset=utf-8"); PrintWriter out=response.getWriter(); if (login!=null){ System.out.println("0"); return "/index.jsp"; }else { //out.print("<script language=\"javascript\">alert('登录失败!');</script>"); out.print("<script language=\"javascript\">alert('账号或密码错误!请重新输入!!!!');window.location.href='/index.jsp'</script>"); System.out.println("1"); return out; } }
页面:
<%@ page language="java" contentType="text/html; utf-8" isELIgnored="false" pageEncoding="utf-8" %> <html> <title>我是登录</title> <body> <h2>Hello World!</h2> <form action="/isLogin" method="post" > 用户:<input type="text" name="uname"><br> 密码:<input type="text" name="upwd"><br><br> <input type="submit" value="确定登录"> <button><a href="uen.jsp">点击注册</a></button> </form> </body> </html>