6

I tried to implement token interceptor with the <s:url .. tag but its showing error on the first click. i.e.

The form has already been processed or no token was supplied, please try again.

I want to implement this interceptor, because if users already deleted a row and refresh the page once again then the same action should not perform once again.

<s:url id="linkdelete" action="DeleteLatestUpload.action" namespace="/admin/insecure/upload">
     <s:param name="latestUploadId" value="latestUploadId"></s:param>
     <s:token name="token"></s:token>
</s:url> 
<a href='<s:property value="#linkdelete"/>' style="color: white;text-decoration:  none;" class="delbuttonlink">Clear current Uploads</a>

and my struts.xml:

 <action name="DeleteLatestUpload" class="v.esoft.actions.UploadExcel" method="deleteUploads">                   
     <interceptor-ref name="token"></interceptor-ref>
     <interceptor-ref name="basicStack"></interceptor-ref>  
     <result name="success" type="tiles"> uploadforward</result>
     <result name="invalid.token" type="tiles">uploadforward </result>
 </action>
            
4

2 回答 2

7

s:token 标记仅放置一个包含唯一标记的隐藏元素。

不需要在 url 中使用 token,因为应该提交表单。如果你想传递一些令牌作为参数,那么你需要使用s:param标签。

定义参数

  private String token;

  public String getToken() {
    return token;
  }

  public void setToken(String token) {
    this.token = token;
  }

  public String execute() throws Exception {
    Map<String, Object> context = ActionContext.getContext().getValueStack().getContext();
    Object myToken = context.get("token");
    if (myToken == null) {
        myToken = TokenHelper.setToken("token");
        context.put("token", myToken);
    }
    token = myToken.toString();
    return SUCCESS;
  }

在 JSP 中

<s:url var="linkdelete" namespace="/admin/insecure/upload" action="DeleteLatestUpload" ><s:param name="struts.token.name" value="%{'token'}"/><s:param name="token" value="%{token}"/></s:url>
于 2013-09-16T08:03:30.693 回答
5

将 token 与 url 一起使用的最简单方法是使用<s:token/>tag 将 token 值设置到 session 并在<s:param>tag 中检索它。

<s:token/>

<s:url var="..." action="...">
  <s:param name="struts.token.name" value="'token'"/>
  <s:param name="token" value="#session['struts.tokens.token']"/>
</s:url>
于 2013-09-16T19:23:39.027 回答