登陆输入为空或错误时的提示信息

来源:互联网 发布:java测试工程师面试 编辑:程序博客网 时间:2024/06/05 15:34

 <script type="text/javascript" src="./jQuery/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" src="./jQuery/jquery-ui-1.10.4.custom.min.js"></script>
    <script type="text/javascript" src="./jQuery/layer/layer.min.js"></script>
    <script type="text/javascript">
        function setNextFocus(str){
            var keyCode = event.keyCode ? event.keyCode
                    : event.which ? event.which : event.charCode;
            if(keyCode==13){
                if(str=="account" && document.getElementById("account").value!=""){
                    document.getElementById("password").focus();
                }else if(str=="password" && document.getElementById("password").value!=""){
                    login();
                }
            }
        }


        window.onload = function() {
            $("#account").focus();
        };

//点击登陆按钮提交前,判断是不是为空的
        function login() {
            var username = $("#account");
            var password = $("#password");
            var authcode = $("#authcode");
            if (username.val() == null || username.val() == "") {
                layer.msg("请输入用户名!", 2, 1);
                layer.shift('top',500);
                username.focus();
                return false;
            }
            if (password.val() == null || password.val() == "") {
                layer.msg("请输入密码!", 2, 1);
                layer.shift('top',500);
                password.focus();
                return false;
            }
            if (authcode.val() == null || authcode.val() == "") {
                layer.msg("请输入验证码!", 2, 1);
                layer.shift('top',500);
                password.focus();
                return false;
            }
            $("#loginForm").submit();//如果都不为空就提交
        }
      


<%--提示信息--%>//判断是不是错误的

<input type="hidden"id="tips" value="${tips}"/>



<c:if test="${flag==true}">
    <script type="text/javascript">
        layer.msg(decodeURI($("#tips").val()), 2, 1);
        layer.shift('top',500);
    </script>
</c:if>
<c:if test="${flag==false}">
    <script type="text/javascript">
        layer.msg(decodeURI($("#tips").val()), 2, 5);
        layer.shift('top',500);
    </script>

</c:if>

 后台部分代码:

/**
     * 用户登录
     * @param account
     * @param password
     * @return
     * @throws IOException 
     */
@RequestMapping(value = "login", method = RequestMethod.POST)
public ModelAndView login(
@RequestParam(value = "account", required = false, defaultValue = "") String account,
@RequestParam(value = "password", required = false, defaultValue = "") String password,
@RequestParam(value = "authcode", required = false, defaultValue = "") String authcode,
HttpServletRequest request, HttpServletResponse response) throws IOException {
HashMap queryParams = new HashMap();
try {
account = EncodeFilter.encode(URLDecoder.decode(account, "utf-8"));
password = URLDecoder.decode(password, "utf-8");
authcode = EncodeFilter.encode(URLDecoder.decode(authcode, "utf-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
queryParams.put("account", account);

if (!account.equals("")) {
User user = userService.findUser(queryParams);
if (user != null) {
String currPass = SecurityUtil.EncryptECB(Constant.SECURITY_KEY, password);
if (currPass.equals(user.getPassword())) {
String authcodes=(String) request.getSession().getAttribute("rand");
                    //toLowerCase大写转换为小写
if(authcode.toLowerCase().equals(authcodes.toLowerCase())){

logger.info("user " + user.getAccount() + " login");
// 设置权限和菜单
setOperAndMenu(user);

request.getSession().setAttribute("user",user);
SessionUtils.saveLoginUser(account);// 做个标记
SessionUtils.saveSessionAttribute(Constant.USER, user);
// 查询所有的菜单信息
SessionUtils.saveSessionAttribute(Constant.MENUS, userService.queryMenu(null));


                    /*SessionUtils.saveSessionAttribute(ADDV,informationService.getAddv());
                    SessionUtils.saveSessionAttribute(VALLEY,informationService.getValley());
                    SessionUtils.saveSessionAttribute(PROJECT_TYPE,informationService.getProjectType());
                    SessionUtils.saveSessionAttribute(PROJECT_GRADE,informationService.getProjectGrade());
                    SessionUtils.saveSessionAttribute(MANAGE_COM,informationService.getManageCom());
                    SessionUtils.saveSessionAttribute(PROJECT_NAME,informationService.getProjectName());*/
                        return new ModelAndView("main",null);
                   }else{
                    queryParams.put("tips", URLEncoder.encode("验证码错误!","utf-8"));
    queryParams.put("flag",false);
    return new ModelAndView("login",queryParams);
                    
                   }
} else {
queryParams.put("tips", "密码错误!");
queryParams.put("flag", false);
return new ModelAndView("login", queryParams);
}
} else {
logger.info("user " + account + " not exists");
queryParams.put("tips", "用户名" + account + "不存在!");
queryParams.put("flag",false);
return new ModelAndView("login",queryParams);
}
} else {
return new ModelAndView(new RedirectView("index.do"), queryParams);
}


}
/** 
* 生成随机颜色 
*/ 
private Color getRandColor(int fc, int bc) { 
Random random = new Random(); 
if (fc > 255) 
fc = 255; 
if (bc > 255) 
bc = 255; 
int r = fc + random.nextInt(bc - fc); 
int g = fc + random.nextInt(bc - fc); 
int b = fc + random.nextInt(bc - fc); 
return new Color(r, g, b); 

    /**
     * 用户注销
     * @throws UnsupportedEncodingException 
     *
     */
    @RequestMapping(value="logout")
    public ModelAndView logout() throws UnsupportedEncodingException{
        SessionUtils.removeSessionAllAttribute();
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("flag",true);
        model.put("tips", URLEncoder.encode("您已成功退出系统。","utf-8"));
        return new ModelAndView("login", model);
    }


    /**
     * url过滤
     * @return
     */
    @RequestMapping(value = "index")
    public ModelAndView index() {
     
        return new ModelAndView("login");
    }
  login.jsp部分代码

<table style="margin:0 auto; border-collapse:collapse; width:100%;">
    <tr>
        <td style="background:url(./images/back_left.jpg); padding:0;">&nbsp;</td>
        <td style="width:1278px; padding:0;">
            <table style="width:1278px; height:764px; margin:0 auto; border-collapse:collapse; background:url(./images/login_shadow.jpg);font-size: 40px; color: #FFF;">
                <tr>
                    <td height="68" colspan="3" align="center"><span class="header"><img src="./images/logo.png" width="60" height="62" alt="psxq">深圳市坪山新区水务及三防信息化系统</span></td>
                </tr>
                <tr>
                    <td width="248" height="561">&nbsp;</td>
                    <td width="765" height="561" align="center" valign="middle"><div id="logdiv">
                        <form id="loginForm" action="./login.do" method="post">
                            <table style="width:397px; height:160px; font-size: 14px; color: #264EBD; font-family: Arial, Helvetica, sans-serif;">
                                <tr height="80">
                                    <td width="68">&nbsp;</td>
                                    <td width="89">&nbsp;</td>
                                    <td><span style="color:red;font-size:12px;"></span>&nbsp;</td>
                                </tr>
                                <tr>
                                    <td>&nbsp;</td>
                                    <td align="right">用户名:</td>
                                    <td><label for="account"></label> <input
                                            name="account" type="text" id="account" maxlength='24' style="width:160px;hight:18px;vertical-align:middle;" onKeyPress="setNextFocus(this.id);"/></td>
                                </tr>
                                <tr>
                                    <td>&nbsp;</td>
                                    <td align="right">密&nbsp;&nbsp;码:</td>
                                    <td><label for="password"></label> <input name="password" maxlength='24' type="password" id="password"
                                                                              style="width:160px;hight:18px;vertical-align:middle;" onKeyPress="setNextFocus(this.id);"/></td>
                                </tr>
                                   <tr>
                                    <td>&nbsp;</td>
                                    <td align="right">验证码:</td>
                                    <td ><label for="authcode"></label>
                                     <input name="authcode" type="text" id="authcode" maxlength='4' style="width:90px;hight:20px;vertical-align:middle;" onKeyPress="setNextFocus(this.id);"/>
                                     <img alt="验证码" style="width:65px;hight:18px;vertical-align:middle;" id="imagecode" src="<%=request.getContextPath() %>/servlet/ImageServlet"/>
                                     <a href="javascript: reloadCode();" style="color:#264EBD;">看不清楚</a>
                                   </tr> 
                            </table>
                            <div
                                    style="width: 240px; height: 50px; margin-left: 20px; margin-top: 40px;">
                                <div
                                        style="margin: 3px; width: 80px; height: 40px; display: inline-block; cursor: pointer;"
                                        onclick="login();"></div>
                                <div style="display: inline-block; width: 40px;"></div>
                                <div
                                        style="margin: 3px; width: 80px; height: 40px; display: inline-block; cursor: pointer;"
                                        onclick="loginForm.reset();"></div>
                            </div>
                        </form>
                    </div></td>
                    <td width="267" height="561">&nbsp;</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td colspan="3" align="center"><br /> <span class="copyright">CopyRight
                        &copy; 2001 - 2013 深圳市路路通网络通信有限公司</span>&nbsp;</td>
                </tr>
          </table>
        </td>
        <td background="./images/background.jpg">&nbsp;</td>
    </tr>
</table>

0 0
原创粉丝点击