基类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提示