防止表单重复提交

jsp 页面:
  <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>
    <s:if test="null == web">
            新增页面
        </s:if>
        <s:else>
            更新页面
        </s:else>
    </title>
    <s:head theme="xhtml"/>
  </head>
   <body>
    <center>
     	<h3>
       <s:if test="null == web">
            新增页面
        </s:if>
        <s:else>
            更新页面
        </s:else>
        </h3>
    <s:form method="post" action="add">
     	<s:hidden name="web.id"  label="ID号"  theme="xhtml"/><br>
     	<s:textfield name="web.web" label="别名"  theme="xhtml"/><br>
     	<s:textfield name="web.webname" label="对应的网页"  theme="xhtml"/><br>
     	<tr><td></td><td align="center"><input name="cancle" type="button" onclick="javascript:history.go(-1)" value="返回" />
     	 <input type="submit" name="submit" value="提交" /></td></tr>
      
 <s:token />

     	</s:form>
    </center>
  </body>
</html>


只要在JSP页面中表单中放<s:token />这样的标签就行了。

struts.xml配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
  <constant name="struts.i18n.encoding" value="utf-8" /> 
    <package name="test" extends="struts-default" >
 <interceptors>
  <interceptor-stack name="to">
   <interceptor-ref name="defaultStack"></interceptor-ref>
   <interceptor-ref name="token"></interceptor-ref>
  </interceptor-stack>
</interceptors>
<action name="add" class="action.TestAction" method="add">
            <result name="success">/success.jsp</result>
            <result name="error">error.jsp</result>
            <result name="invalid.token">/tokenerror.jsp</result>
            <interceptor-ref name="to"></interceptor-ref>
</action>
   </package>
</struts>

这样就可以实现防止表单重新提交

你可能感兴趣的:(apache,jsp,xml,浏览器,struts)