自定义异常,向页面展示异常信息

来源:互联网 发布:如何提高六级听力 知乎 编辑:程序博客网 时间:2024/05/29 07:11

AJAX部分

$.ajax({    url:"safety/loginCheck.action",        data:{            "user.loginAct":username,            "user.loginPwd":password        },        dataType:"json",        success:function(data){            if(data.success){                            window.location.href="workbench/index.jsp";                        }else{                            $("#myMsg").html("<font color='#FF0000'>"+data.msg+"</font>");                        }                    }                })

java代码部分

自定义一个异常类,继承RuntimeException

public class ApplicationException extends RuntimeException{    private static final long serialVersionUID = 1L;    public ApplicationException(){}    public ApplicationException(String msg){        super(msg);    }}

Action部分:

public String loginCheck(){

    HttpServletRequest request = ServletActionContext.getRequest();    String ip = request.getRemoteAddr();    UserService userService = new UserServiceImpl();    try{        UserVo loginCheck = userService.loginCheck(user,ip);        request.getSession().setAttribute("user",loginCheck);        map.put("success",true);    }catch(ApplicationException e){        e.printStackTrace();        String msg = e.getMessage();        map.put("msg",msg);    }    return SUCCESS;}
原创粉丝点击