Struts2 token防止再重复提交

来源:互联网 发布:sql server授予权限 编辑:程序博客网 时间:2024/06/06 03:26

由Action转到拦截器
WEB.xml的配置

<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0"     xmlns="http://java.sun.com/xml/ns/javaee"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  <display-name></display-name>   <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>  <filter>    <filter-name>struts2</filter-name>    <filter-class>        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter    </filter-class>  </filter>  <filter-mapping>    <filter-name>struts2</filter-name>    <url-pattern>*.action</url-pattern>  </filter-mapping>  <filter-mapping>    <filter-name>struts2</filter-name>    <url-pattern>*.jsp</url-pattern>  </filter-mapping>    </web-app>

Sturts.xml的配置

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts>    <package name="test" extends="struts-default">        <interceptors>            <interceptor name="myInter" class="com.interceptor.myInterceptor"></interceptor>        </interceptors>    </package></struts>    

JSP的请求

<a href="test.action">filterTest</a>   <br>${msg }

Action类:

package com.act;import javax.interceptor.Interceptor;import org.apache.struts2.convention.annotation.Action;import org.apache.struts2.convention.annotation.InterceptorRef;import org.apache.struts2.convention.annotation.ParentPackage;import org.apache.struts2.convention.annotation.Result;import com.opensymphony.xwork2.ActionSupport;//使用元数据配置,一定要记得带stuts配置的这个包名@ParentPackage("test")public class FilterTest extends ActionSupport{    private String msg;    @Override//这在里配置具体的哪一个请求.action会被拦截处理    @Action(value="test",results={@Result(location="index.jsp")},interceptorRefs={@InterceptorRef("myInter")})//这里代指拦截之后传到哪一个类进行处理    public String execute() throws Exception {        // TODO Auto-generated method stub        msg="Action+done!";        return SUCCESS;    }    public String getMsg() {        return msg;    }    public void setMsg(String msg) {        this.msg = msg;    }    }

Filter处理类:

package com.interceptor;import java.util.Random;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;public class myInterceptor extends AbstractInterceptor{private Random random=new Random();private int ss=5000;    @Override    public String intercept(ActionInvocation arg0) throws Exception {            long beginTimer=System.currentTimeMillis();        Thread.currentThread().sleep(random.nextInt(5)*1000);        //拦截通过        String relcode=arg0.invoke();        System.out.println("处理时间"+(System.currentTimeMillis()-beginTimer));        return relcode;}}

Tomcat的JVM不够,我们可以通过这配置来加大他的内存空间
在windows-preferences-tomcat.xx(版本)-jdk-optional java VM arguments下面添加

-Xms256m -Xmx512m-Dcom.sun.management.jmxremote=true

加了拦截器之后
要实现注入,我们需要将自定义的拦截器加到默认的栈里面
将自己的拦截器加入默认的拦截器栈里面
@Action(value=”test”,results={@Result(location=”index.jsp”)})
我们可以不写默认的拦截器栈



自定义的拦截器栈
@Action(value=”test”,results={@Result(location=”index.jsp”)},interceptorRefs={@InterceptorRef(“myStack”)})
这里我们需要写自己的拦截器栈的名字。才可以是实现注入。

<interceptor-stack name="myStack">                <interceptor-ref name="defaultStack"></interceptor-ref>                <interceptor-ref name="myInter"></interceptor-ref></interceptor-stack>

防止重复提交:token

首先在表单里面添加一个标签

<form action="pay.action">        <s:token />        金钱:<input type="text" name="money"><br> <input            type="submit" value="支付">    </form>

然后在struts.xml里面添加token的拦截器然后会返回一个名为invalid.token的结果(result)我们需要指定这个result

            <interceptor-stack name="defaultStack">                <interceptor-ref name="token" />                <interceptor-ref name="defaultStack"></interceptor-ref>                <interceptor-ref name="myInter"></interceptor-ref>            </interceptor-stack><!--             <interceptor-stack name="myStack">                <interceptor-ref name="token" />                <interceptor-ref name="defaultStack" />                <interceptor-ref name="myInter" />            </interceptor-stack> -->

然后会报result invalid.token 错误 ;我们需要在
Action里面添加一个结果返回的处理
@Action(value=”pay”,results={@Result(location=”index.jsp”),@Result(name=”invalid.token” ,location=”err.jsp”)}/,interceptorRefs={@InterceptorRef(“myStack”)}/)
public String pay(){
msg1=money;
return SUCCESS;
}

我选择的是转发另一个错误页面。那么那边可以显示

<body>    <s:actionerror />    <br></body>

是英语的,选择国际化将其转化一下,配置

文件名:globalMessages.properties文件
内容:struts.messages.invalid.token=\u4F60\u70B9\u592A\u591A

还要配置国际化
文件名:struts.properties
内容:struts.custom.i18n.resources=globalMessages

0 0