struts2之防止表單重複提交 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.ui.theme" value="simple" />
    <!-- struts2在防止表單重複提交的攔截中有2個,分別是:token,tokenSession。tokenSession繼承token而來。
          通常情況下,使用tokenSession客戶端感覺會比較友好。 -->
    <!-- 如果重複提交,會跳轉到error.jsp頁面 -->
    <package name="person" namespace="/test" extends="struts-default">
        <action name="token" class="com.ljq.action.PersonAction">
            <interceptor-ref name="defaultStack" />
            <interceptor-ref name="token" />
            <!-- 如果重複提交,跳轉到error.jsp頁面 -->
            <result name="invalid.token">/WEB-INF/page/error.jsp</result>
            <result>/WEB-INF/page/message.jsp</result>
        </action>
        <action name="tokenSession" class="com.ljq.action.PersonAction">
            <interceptor-ref name="defaultStack" />
            <interceptor-ref name="tokenSession" />
            <!-- 如果重複提交,不會跳轉到error.jsp頁面 -->
            <result name="invalid.token">/WEB-INF/page/error.jsp</result>
            <result>/WEB-INF/page/message.jsp</result>
        </action>
    </package>
</struts>
PersonAction類
package com.ljq.action;
import java.util.ArrayList;
import java.util.List;
public class PersonAction {
    private String name;
    @SuppressWarnings("unchecked")
    //觀看控制檯
    //如果token生效則不會在控制檯輸出name的值,而會輸出如下警告: 2011-3-14 20:45:32 com.opensymphony.xwork2.util.logging.commons.CommonsLogger
    //warn 警告: Form token EDZ4S96RNDN5VD8B1CQTK6FTHIJUPC66 does not match the session token null.
    public String execute() {
        List ls = new ArrayList();
        ls.add(name);
        for (int i = 0; i < ls.size(); i++) {
            System.out.println(ls.get(i));
        }
        return "success";
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
index.jsp表單頁面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>防止表單重複提交</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">  
  </head>
  <body>
      <!-- 防止表單重複提交,記得在form表單裏填上<s:token></s:token>      -->
      <!-- action="token"、action="tokenSession" -->
      <s:form action="token" namespace="/test" method="post">
          姓名:<s:textfield name="name"/><s:token></s:token>
          <input type="submit" value="發送"/>
      </s:form>
  </body>
</html>
message.jsp返回成功頁面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">  
  </head>
  <body>
     <s:property value="name"/><br/>
     <%=new Date() %>
  </body>
</html>
error.jsp表單重複提交提示頁面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>My JSP 'error.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">   
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  <body>
      您已經提交了表單,請不要重複提交。
  </body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章