Struts2中使用token

Struts2中使用token

表单的重复提交是指用户对同一个请求信息,多此单机提交按钮或者执行刷新操作,导致该请求信息向服务器多次提交,如果应用程序未对表单重复性提交进行处理,则当请求信息需要保存在数据库时会出现重复的记录,或者由于字段的唯一性而导致添加数据操作出现异常。我们首先来看以下例子。

1. 观察表单重复提交
  1. 新建一个Dept类
import java.io.Serializable;
import java.util.Date;

@SuppressWarnings("serial")
public class Dept implements Serializable {
    private Integer deptno;
    private String dname;
    private Date credate;

    public Dept() {
    }
    //setter、getter略
}
  1. 新建一个Action类,负责找到表单
import com.bank.vo.Dept;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.*;

@SuppressWarnings("serial")
@ParentPackage("root")
@Namespace(value="/pages/token")
@Action(value="TokenAction")
@Results(value={
        @Result(name ="token.page",location = "/pages/token/token.jsp",type = "dispatcher"),
        @Result(name ="input",location = "/pages/errors.jsp",type = "dispatcher")
})
public class TokenAction extends ActionSupport {
    private Dept dept = new Dept();
    //setter、getter略
    
    public String goTokenPage(){
        return "token.page";
    }
    public void print(){
        try {
            ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
            ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
            ServletActionContext.getResponse().setContentType("text/html");
            ServletActionContext.getResponse().getWriter().println("部门编号:"+this.dept.getDeptno()+"<br>");
            ServletActionContext.getResponse().getWriter().println("部门名称:"+this.dept.getDname()+"<br>");
            ServletActionContext.getResponse().getWriter().println("注册日期:"+this.dept.getCredate()+"<br>");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  1. 新建提交表单页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Token</title>
</head>
<body>
<s:form action="TokenAction!print.action" method="POST" theme="simple">
    <label>部门编号:</label><s:textfield name="dept.deptno" theme="simple" /><br>
    <label>部门名称:</label><s:textfield name="dept.dname" theme="simple" /><br>
    <label>创建日期:</label><s:textfield name="dept.credate" theme="simple" /><br>
    <s:submit value="提交" />
</s:form>
</body>
</html>
  1. 运行程序,此时发现用户可以无限次的提交。

    提交结果:
2. 使用token拦截器

要避免表单的重复提交,Struts2提供了token拦截器和tokenSession拦截器,我们首先来使用token拦截器。只需要在表单页面加入<s:token>标签,其他程序不用改变。

<s:form action="TokenAction!print.action" method="POST" theme="simple">
    <label>部门编号:</label><s:textfield name="dept.deptno" theme="simple" /><br>
    <label>部门名称:</label><s:textfield name="dept.dname" theme="simple" /><br>
    <label>创建日期:</label><s:textfield name="dept.credate" theme="simple" /><br>
    <s:token name="repeat" /><!--防止重复提交-->
    <s:submit value="提交" />
</s:form>

此时我们发现表单只能提交一次,多次提交都会返回当前页。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值