我试图通过在会话中设置只使用一次令牌并将其与请求进行比较来防止浏览器刷新导致的多个表单提交。但是,当我试图重置会话令牌时,它不会被更新。下面是我的密码。
表示jsp页的Set令牌
getSession().setAttribute(Globals.TRANSACTION_TOKEN_KEY, "123");这个在我的jsp里
<s:hidden name="token" value="123"/>行动类
String requestToken = getToken();
String sessionToken = (String)
getRequest().getSession().getAttribute(Globals.TRANSACTION_TOKEN_KEY);
if(requestToken.equalsIgnoreCase(sessionToken)){
//reset the token
getRequest().getSession().setAttribute(Globals.TRANSACTION_TOKEN_KEY, "124");
//perform update
}会话令牌没有更新到124。请协助。
谢谢,普拉希尔。
发布于 2014-04-26 11:50:20
您可以在操作中使用下面的代码。
在载入上一页时
ActionContext.getContext().getSession().put(Globals.TRANSACTION_TOKEN_KEY, "123");在jsp页面中
<s:hidden name="token" value="123"/>在表格提交的行动类中
Map<String,Object> session = ActionContext.getContext().getSession();
if(getToken().toString().equalsIgnoreCase(session.get(Globals.TRANSACTION_TOKEN_KEY).toString())){
System.out.println("request is handled");
//reset the token
//perform update
session.put(Globals.TRANSACTION_TOKEN_KEY, "124");
}在两个jsp页面中,请使用以下命令
< %
response.setHeader("Cache-Control", "no-cache, no-store");
response.setDateHeader("Expires", 0);
response.setHeader("Vary", "*");
% >最简单的方法是
if (session.getAttribute("recordInsertedSuccessfully") == null )
{
//update records
//after inserting into the database we should do :
session.putAttribute("recordInsertedSuccessfully","true");
} else {
//case of form re-submission
}您的问题是再次调用将会话变量设置为123的方法。
创建另一个方法runinspect2(),从runinspect()复制粘贴代码,但从runinspect2()中删除行,后者将会话变量设置为123,如下行所示
ActionContext.getContext().getSession().put(Globals.TRANSACTION_TOKEN_KEY, "123");并在runinspect2()方法中调用update()方法,非常简单
https://stackoverflow.com/questions/23306990
复制相似问题