读书人

struts2中表单重复提交的有关问题

发布时间: 2012-10-25 10:58:58 作者: rapoo

struts2中表单重复提交的问题
看了网上说的有关struts2不重复刷新有很多,基本上都是在同一个页面中可以,那么我说下在一个页面中有一个按钮当点击后,到另一个页面。这个页面有许多列表记录,点击保存后再一次跳转到上一次页面中。那么在当前页面点击右键刷新,问题就在这了。

以前的
html:
<s:form action="topic" method="post">
<s:token name="topic.name"/>
</s:form>


struts.xml:
<action name="topic_*" method="{1}" type="dispatcher">/common/noBack.jsp</result>
<interceptor-ref name="defaultStack" />
<interceptor-ref name="token">
<param name="includeMethods">addTopicElement</param>
</interceptor-ref>
<interceptor-ref name="token">
<param name="excludeMethods">default</param>
</interceptor-ref>

</action>

注意:
【1】:
<s:token>标签创建一个新的令牌值,并用你所指定的令牌名把令牌保存到session中。而这个令牌值是随即产生的经过加密的字符序列,不会重复。

其次需要为action配置TokenInterceptor或者TokenSessionStoreInterceptor拦截器。这两个拦截器都已经在struts-default.xml中定义,但没有包含在defaultStack拦截器栈中。

【2】:
<interceptor-ref name="token"/>和<interceptor-ref name="defaultStack"/>是两个拦截器的配置,目的是为了接收页面传入

的token令牌及参数,<result name="invalid.token">/common/noBack.jsp</</result>指当发现重复提交时,需要流转到的页面.
【3】:
excludeMethods指定要排除的方法,includeMehtods是要拦截的方法。



如果是用javascript来提交的话,看下边----
function selectA() {

var url = "relation_addRelationshipTopic.do?relation.rnId=<%=rn != null ? rn.getId().longValue() : -1%>&relation.selectRn="+selectRn+"&fresh=" + Math.random()+"";
var token = "struts.token.name=relation.token";
var token2 = "relation.token=";
token2 += document.getElementsByName("relation.token")[0].value;
url += "&" + token + "&";
url += token2;
location = url;
}

注:
<s:token name="relation.token"/>
它回自动的生成两个hidden,一个是struts.token.name,一个是reletion.token.

读书人网 >软件架构设计

热点推荐