怎么防止页面刷新重复提交数据
写个类继承FormTag,renderToken方法中加入如下代码:
StringBuffer results = new StringBuffer();
HttpSession session = pageContext.getSession();
if(session !=null){
String token = (String)session.getAttrubute(TRANSACTION_TOKEN_KEY);
if(token !=null){
results.append("<input type=\"hidden\"name=\"");
results.append(TOKEN_KEY);
results.append("\"value=\"");
results.append(token);
if(this.Xhtml()){
results.append("\"/>");
}
else{
results.append("\">");
}
}
}
return results.toString();
}
创建一个Action名为PrepareInsertAction
execute方法中加入
saveToken(request);
return mapping.findForward(forwardsuccess);
创建在创建一个action名为InsertAction
execute方法中加入
ActionMessages errors = new ActionMessages();
if(!isTokenValid(request)){
errors.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage
("error.invalid.token"));
saveErrors(request,errors);
saveToken(request);
return(new ActionForward(mapping.getInput()));
}
else{
resetToken(request);
}
}
访问顺序
PrepareInsertAction→insert.jsp→InsertAction
[此贴子已经被作者于2007-7-23 23:33:29编辑过]