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

  • 相关阅读:
    云图说 | GPU共享型AI容器,让AI开发更普及
    手把手带你写Node.JS版本小游戏
    一个银行客户经理的“变形记”
    大厂运维必备技能:PB级数据仓库性能调优
    软件工程开发之道:了解能力和复杂度是前提
    大数据管理:构建数据自己的“独门独院”
    结构体与共用体05 零基础入门学习C语言57
    结构体与共用体04 零基础入门学习C语言56
    PE格式详细讲解1 系统篇01|解密系列
    初步认识PE格式 基础篇06|解密系列
  • 原文地址:https://www.cnblogs.com/ShawnYang/p/6681870.html
Copyright © 2011-2022 走看看