zoukankan      html  css  js  c++  java
  • struts2 自带的 token防止表单重复提交拦截器

    在struts2中,我们可以利用struts2自带的token拦截器轻松实现防止表单重复提交功能!

    1. 在相应的action配置中增加:

       <interceptor-ref name="token"></interceptor-ref>

      <result name="invalid.token">/error.jsp</result>

    2. 增加error.jsp文件,代码如下:

        <h1>禁止重复提交</h1>

    3. 在所提交的表单上增加:<s:token></s:token>标记。

        <form action="firstAction">

        <input name="uname" value="zhangsan"><br>
        <s:token></s:token>
        <input type="submit" value="提交">
      </form>

    web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app id="WebApp_9" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
      <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
      </filter>
      <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
    </web-app>

    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>
      <package name="crm" namespace="/" extends="struts-default" >
        <interceptors>
          <interceptor name="myInter" class="com.huawei.interceptor.MyInterceptor" />
          <interceptor-stack name="myStack">
            <interceptor-ref name="defaultStack" />
            <!-- <interceptor-ref name="myInter" /> -->
            <interceptor-ref name="token" />
          </interceptor-stack>
        </interceptors>
        <default-interceptor-ref name="myStack" />
        <!-- 全局result -->
        <global-results>
          <result name="success">/ok.jsp</result>
        </global-results>
      </package>
      <package name="default" namespace="/" extends="crm">
        <action name="firstAction" class="com.huawei.s2.action.FirstAction" >
          <result name="invalid.token" >/error.jsp</result>
        </action>
      </package>
    </struts>

    Action:

    package com.huawei.s2.action;
    public class FirstAction {
      private String uname;
      public String execute(){
        System.out.println(uname+"========FirstAction=======");
        return "success";
      }
      public String getUname() {
        return uname;
      }
      public void setUname(String uname) {
        this.uname = uname;
      }
    }

    interceptor:

    package com.huawei.interceptor;

    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

    /**
    * 要么实现 Interceptor 接口 要么 继承类
    * @author Administrator
    *
    */
    public class MyInterceptor extends AbstractInterceptor{

      @Override
      public String intercept(ActionInvocation invocation) throws Exception {
        System.out.println(invocation.getAction());
        System.out.println(invocation.getInvocationContext());
        System.out.println(invocation.getProxy().getActionName());
        System.out.println(invocation.getProxy().getMethod());
        System.out.println(invocation.getInvocationContext().getParameters());
        System.out.println("action执行前");
        invocation.invoke();
        System.out.println("action执行后");
        return null;
      }

    }

    jsp:

    1.jsp:

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@taglib prefix="s" uri="/struts-tags" %>
    <%
    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%>">
      <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
      <title>This is my JSP 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">
    </head>
    <body>
      <form action="firstAction">
        <input name="uname" value="zhangsan"><br>
        <s:token></s:token>
        <input type="submit" value="提交">
      </form>
    </body>
    </html>

    ok.jsp:

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    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%>">
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
        <title>This is my JSP 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">
      </head>
      <body>
        <h1>提交成功</h1>
      </body>
    </html>

    error.jsp:

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    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%>">
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
        <title>This is my JSP 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">
      </head>
      <body>
        <h1>禁止重复提交</h1>
      </body>
    </html>

  • 相关阅读:
    Executors提供的四种线程池和自定义线程池
    ava8并发教程:Threads和Executors
    Java 信号量 Semaphore 介绍
    Condition-线程通信更高效的方式
    ReentrantLock详解 以及与synchronized的区别
    FutureTask 源码解析
    Java多线程编程:Callable、Future和FutureTask浅析
    Callable 和 Runnable 的区别
    javascript之url转义escape()、encodeURI()和decodeURI()
    yii2.0安装ElasticSearch及使用
  • 原文地址:https://www.cnblogs.com/hwgok/p/5540452.html
Copyright © 2011-2022 走看看