您现在的位置是:首页 > 电脑技术查询 > web开发

数据操作后,页面alert揭示例子3-后台js打印提示信息并重定向

编辑:chaxungu时间:2022-10-10 23:24:28分类:web开发

数据操作后,页面alert提示例子3-后台js打印提示信息并重定向
基类baseAction中java代码:
/** *  * @param msg   alert提示信息 * @param redirectUrl  重定向url,如果为空,则返回到操作当前页 */public void alertMsg(String msg,String redirectUrl){        if(msg==null||"".equals(msg.trim())){        return ;        }        PrintWriter out=null;  try {  //设置回发内容编码        ServletActionContext.getResponse().setContentType("text/html;charset=gbk");   out = ServletActionContext.getResponse().getWriter();  } catch (IOException e) {       e.printStackTrace();  }  StringBuilder sb=new StringBuilder(); sb.append("<script>alert('" +msg+"!');"); if(redirectUrl==null||"".equals(redirectUrl.trim())){ sb.append("history.go(-1);"); }else{ sb.append("location='"+redirectUrl+"';"); } sb.append("</script>"); out.print(sb.toString()); out.flush(); out.close();}/** * alert 提示后跳到当前页面 * @param msg alert提示信息 */public void alertMsg(String msg){alertMsg( msg,null);} 子action中增删改操作后调用例子
public String delete(){defaultUrl();..  your code                     this.alertMsg("操作成功", this.getBasePath()+"course/course_list.do");return null;} 优点:
 1、调用简单,不用修改前端页面,更改代码少
 2、刷新不会重新提交
 3、跳转后的页面再刷新不会重新alert提示