这样的,本人在一个管理系统,本人想在出现后台报业务异常时候弹出一个提示框提示。 例如参数错误,缺失,超时等等。
throw new BusinessException(“msg”);
最好给个demo
throw new BusinessException(“msg”);
最好给个demo
解决方案
25
public class BException extends Exception {
private static final long serialVersionUID = 1L;
private String msg;//错误消息
private boolean async; //能否异步
public BException(){
super();
}
public BException(String msg) {
super(msg);
this.async = false;
this.msg = msg;
}
public BException(Exception e){
if(e instanceof BException){
BException self = (BException) e;
this.msg = self.getMsg();
}else{
this.msg = e.getMessage();
}
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public boolean isAsync() {
return async;
}
public void setAsync(boolean async) {
this.async = async;
}
}
然后在你的控制层出错的地方直接throw new BException(“错误信息”);
private static final long serialVersionUID = 1L;
private String msg;//错误消息
private boolean async; //能否异步
public BException(){
super();
}
public BException(String msg) {
super(msg);
this.async = false;
this.msg = msg;
}
public BException(Exception e){
if(e instanceof BException){
BException self = (BException) e;
this.msg = self.getMsg();
}else{
this.msg = e.getMessage();
}
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public boolean isAsync() {
return async;
}
public void setAsync(boolean async) {
this.async = async;
}
}
然后在你的控制层出错的地方直接throw new BException(“错误信息”);
40
springmvc有异常拦截器,可以通过拦截自定义指定异常返回错误内容到前端展示
15
1.在你的某个全局页面中定义一段js,例如这样:
<c:if test="${!empty error_msg}">alert("${error_msg}");</c:if>
2.在你的后台逻辑处理中返回error_msg,例如这样:request.setAttribute(“error_msg”,”错误消息”);