Struts2 使用token拦截器控制(防止)重复,刷新,后退 提交数据

来源:互联网 发布:ls算法 编辑:程序博客网 时间:2024/05/22 08:37

使用这个拦截器非常简单,也是别人那里转来的,就是这个描述的很清楚,所以转过来,希望能帮助到更多的人。

先来个图片流程:



具体代码:

input.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><%@ taglib uri="/struts-tags" prefix="s" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><base href="<%=basePath %>"/><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Hello Struts2</title></head><body>    <form action="user.action" method="post">        name:<input type="text" name="name"/>        age:<input type="text" name="age"/>        <input type="submit" value="提 交"/>        <s:token></s:token>    </form></body></html>

addOK.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    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" "http://www.w3.org/TR/html4/loose.dtd"><html><head><base href="<%=basePath %>"/><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Hello Struts2</title></head><body>    addOK.</body></html>

error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    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" "http://www.w3.org/TR/html4/loose.dtd"><html><head><base href="<%=basePath %>"/><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Hello Struts2</title></head><body>    不可重复提交数据!<a href="${pageContext.request.contextPath}/index.jsp">返回主页</a></body></html>



struts.xml


<struts>    <constant name="struts.enable.DynamicMethodInvocation" value="true" />    <constant name="struts.configuration.xml.reload" value="true"/>        <package name="test" namespace="/" extends="struts-default">            <action name="input" class="com.bjsxt.action.InputAction">            <result>/input.jsp</result>        </action>            <action name="user" class="com.bjsxt.action.UserAction">            <result>/addOK.jsp</result>    <!--注意这三个是有顺序问题的,在某些博客上没有注明这点,害死人-->            <interceptor-ref name="defaultStack"></interceptor-ref>            <interceptor-ref name="token"></interceptor-ref>            <result name="invalid.token">/error.jsp</result>        </action>    </package></struts>



userAction


package com.bjsxt.action;import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport {    private static final long serialVersionUID = -8003780600877800393L;        private String name;        private int age;        public String execute(){        System.out.println("a user added!");        return SUCCESS;    }        public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    }


如有问题,可留言!!!







阅读全文
0 0