请教java高手!

我现在使用s2sh框架做一个小项目,发现一个问题,提交action后,再刷新页面总是会重复提交,无意中了解到使用拦截器可以解决这个问题,可是这个我不会配哎,有会的大侠们,... 我现在使用s2sh框架做一个小项目,发现一个问题,提交action后,再刷新页面总是会重复提交,无意中了解到使用拦截器可以解决这个问题,可是这个我不会配哎,有会的大侠们,能帮小妹解决这个问题吗???跪谢了!!
你们真是太辛苦了!我这里说的是s2sh,说的是struts2,不是struts1……

T_T
展开
 我来答
百度网友dfc2c6d
2010-08-03
知道答主
回答量:36
采纳率:0%
帮助的人:13.8万
展开全部
解决Struts重复提交
文章分类:Java编程 关键字: struts 重复提交
问题的产生原因
页面提交给Action去进行业务处理,Action再跳转回前台页面,但这时URL依然是“页面提交给Action的链接”,这时前台刷新一下页面,就变成再次执行了一次提交操作。

解决思路
1,在Action页面中跳转的时候用重定向,可以在struts-config.xml中配置<forward ... redirect=“true”>
不过这种方法会使得request中放置数据丢失;
2,用Token令牌环来实现
提交到Action的时候,进行一系列操作,然后保存一个标志,这时再跳转到前台页面。如果前台页面刷新的话,Action通过查看是否有标志,就能判断用户是刷新还是提交。

Action中操作令牌的方法
一: saveToken(HttpServletRequest request)
创建一个新令牌,并将其保存在当前用户的会话中,如果用户的会话不存在,将首先创建新会话对象
二: isTokenValid(HttpServletRequest request)
判断存储在当前会话中的令牌值和请求参数中的令牌值是否匹配,如果匹配,返回true,否则返回false 。
以下情况返回false:
1,用户的HttpSession不存在
2,用户Session中没有保存令牌值
3,在用户请求参数中没有令牌值
4,存储在用户Session范围内的令牌值和请求参数中 的令牌值不匹配
三:resetToken()
删除保存在Session范围内的令牌值

示例代码

1,进入目标页,通过IndexAction转发,其中需要保存令牌:

Java代码
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
saveToken(request);
return mapping.findForward("index");
}

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
saveToken(request);
return mapping.findForward("index");
} 2,目标页面(test.jsp)需要使用Struts标签库:

Html代码
<body>
<html:form action="test.do" method="post">
<input type="submit" value="提交" />
</html:form>
</body>

<body>
<html:form action="test.do" method="post">
<input type="submit" value="提交" />
</html:form>
</body> 3,当提交test.jsp时提交到TestAction处理:

Java代码
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
/* 如果令牌有效,则执行业务逻辑 */
if (isTokenValid(request)) {
/* 删除令牌 */
resetToken(request);
return mapping.findForward("test");
}
return mapping.findForward("error");
}

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
/* 如果令牌有效,则执行业务逻辑 */
if (isTokenValid(request)) {
/* 删除令牌 */
resetToken(request);
return mapping.findForward("test");
}
return mapping.findForward("error");
}

4,struts-config.xml配置:

Xml代码
<struts-config>
<form-beans>
<form-bean name="testForm" type="com.tanlan.struts.form.TestForm">
</form-bean>
</form-beans>
<action-mappings>
<action path="/index" type="com.tanlan.struts.action.IndexAction">
<forward name="index" path="/index.jsp"></forward>
</action>
<action path="/test" type="com.tanlan.struts.action.TestAction"
name="testForm" input="/index.jsp">
<forward name="test" path="/test.jsp"></forward>
<forward name="error" path="/error.jsp"></forward>
</action>
</action-mappings>
</struts-config>

<struts-config>
<form-beans>
<form-bean name="testForm" type="com.tanlan.struts.form.TestForm">
</form-bean>
</form-beans>
<action-mappings>
<action path="/index" type="com.tanlan.struts.action.IndexAction">
<forward name="index" path="/index.jsp"></forward>
</action>
<action path="/test" type="com.tanlan.struts.action.TestAction"
name="testForm" input="/index.jsp">
<forward name="test" path="/test.jsp"></forward>
<forward name="error" path="/error.jsp"></forward>
</action>
</action-mappings>
</struts-config>

至此,大功告成!

完整示例请参考附件。

参考资料: http://tanlan.javaeye.com/blog/424148

challenge234
2010-08-03 · 超过11用户采纳过TA的回答
知道答主
回答量:35
采纳率:0%
帮助的人:26.9万
展开全部
response.sendRedirect("/match.do?XXXXXXXXXXXXXXXX);

你跳转的时候跳另一页面就可以了。我都这样用,这样比较简单。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
Jon0000
2010-08-03 · 超过20用户采纳过TA的回答
知道答主
回答量:86
采纳率:0%
帮助的人:53.4万
展开全部
页面用<s:token>标签
xml里配置这个拦截器,tokeninterceptor
如果想要有一个等待页面在配置下这个拦截器tokeninterceptor
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
yangt_s2010
2010-08-03 · 超过12用户采纳过TA的回答
知道答主
回答量:137
采纳率:0%
帮助的人:55万
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式