• Struts2_使用token拦截器控制重复提交(很少用)


    控制重复提交的方式:1、表单提交后页面重定向;2、Struts2.x token拦截器

    大致流程:

    例子:

    index.jsp

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <%
     4 String path = request.getContextPath();
     5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     6 %>
     7 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     8 <html>
     9 <head>
    10 <base href="<%=basePath %>"/>
    11 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    12 <title>Hello Struts2</title>
    13 </head>
    14 <body>
    15     <a href="input.action">输入用户信息</a>
    16 </body>
    17 </html>

    input.jsp

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <%
     4 String path = request.getContextPath();
     5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     6 %>
     7 <%@ taglib uri="/struts-tags" prefix="s" %>
     8 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     9 <html>
    10 <head>
    11 <base href="<%=basePath %>"/>
    12 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    13 <title>Hello Struts2</title>
    14 </head>
    15 <body>
    16     <form action="user.action" method="post">
    17         name:<input type="text" name="name"/>
    18         age:<input type="text" name="age"/>
    19         <input type="submit" value="提 交"/>
    20         <s:token></s:token>
    21     </form>
    22 </body>
    23 </html>

    addOK.jsp

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <%
     4 String path = request.getContextPath();
     5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     6 %>
     7 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     8 <html>
     9 <head>
    10 <base href="<%=basePath %>"/>
    11 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    12 <title>Hello Struts2</title>
    13 </head>
    14 <body>
    15     addOK.
    16 </body>
    17 </html>

    error.jsp

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <%
     4 String path = request.getContextPath();
     5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     6 %>
     7 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     8 <html>
     9 <head>
    10 <base href="<%=basePath %>"/>
    11 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    12 <title>Hello Struts2</title>
    13 </head>
    14 <body>
    15     严禁做重复的事!
    16 </body>
    17 </html>

    struts.xml

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE struts PUBLIC
     3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
     4     "http://struts.apache.org/dtds/struts-2.3.dtd">
     5 
     6 <struts>
     7     <constant name="struts.enable.DynamicMethodInvocation" value="true" />
     8     <constant name="struts.configuration.xml.reload" value="true"/>
     9     
    10     <package name="test" namespace="/" extends="struts-default">
    11     
    12         <action name="input" class="com.bjsxt.action.InputAction">
    13             <result>/input.jsp</result>
    14         </action>
    15     
    16         <action name="user" class="com.bjsxt.action.UserAction">
    17             <result>/addOK.jsp</result>
    18             <interceptor-ref name="defaultStack"></interceptor-ref>
    19             <interceptor-ref name="token"></interceptor-ref>
    20             <result name="invalid.token">/error.jsp</result>
    21         </action>
    22         
    23     </package>
    24     
    25 </struts>

    InputAction

     1 package com.bjsxt.action;
     2 
     3 import com.opensymphony.xwork2.ActionSupport;
     4 
     5 public class InputAction extends ActionSupport {
     6 
     7     private static final long serialVersionUID = -8003780600877800393L;
     8     
     9     public String execute(){
    10         return SUCCESS;
    11     }
    12     
    13 }

    UserAction

     1 package com.bjsxt.action;
     2 
     3 import com.opensymphony.xwork2.ActionSupport;
     4 
     5 public class UserAction extends ActionSupport {
     6 
     7     private static final long serialVersionUID = -8003780600877800393L;
     8     
     9     private String name;
    10     
    11     private int age;
    12     
    13     public String execute(){
    14         System.out.println("a user added!");
    15         return SUCCESS;
    16     }
    17     
    18     public String getName() {
    19         return name;
    20     }
    21 
    22     public void setName(String name) {
    23         this.name = name;
    24     }
    25 
    26     public int getAge() {
    27         return age;
    28     }
    29 
    30     public void setAge(int age) {
    31         this.age = age;
    32     }
    33     
    34 }

    链接: http://pan.baidu.com/s/1mi1BnoW 密码: rpee

  • 相关阅读:
    [转]CROSS APPLY 和outer apply 的区别
    第一次WCF部署成功心得(UP+证书)
    .NET 简单实现广播
    【转】 c#注册全局快捷键
    软件人才成长链
    [转]关于VS2005智能设备中无法找到PInvoke DLL问题
    [转]我倡导无政府主义编程—Fred George访谈录
    运行Windows Mobile程序时报错:无法找到 PInvoke DLL SQLite.Interop.065.DLL
    系统分析师考试说明
    [转]Oracle 字符集的查看和修改
  • 原文地址:https://www.cnblogs.com/ShawnYang/p/6681870.html
Copyright © 2020-2023  润新知