zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    hdu 2485 Destroying the bus stations 迭代加深搜索
    hdu 2487 Ugly Windows 模拟
    hdu 2492 Ping pong 线段树
    hdu 1059 Dividing 多重背包
    hdu 3315 My Brute 费用流,费用最小且代价最小
    第四天 下载网络图片显示
    第三天 单元测试和数据库操作
    第二天 布局文件
    第一天 安卓简介
    Android 获取存储空间
  • 原文地址:https://www.cnblogs.com/ShawnYang/p/6681870.html
Copyright © 2011-2022 走看看