Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to implement Struts 2 token interceptor with hyperlink

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>
            
like image 827
beginner Avatar asked Sep 16 '13 07:09

beginner


2 Answers

The s:token tag merely places a hidden element that contains the unique token.

There's not need to use token with url, because the form should be submitted. If you want to pass some token as a parameter then you need to use s:param tag.

Define the parameter

  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;
  }

in the 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>
like image 196
Roman C Avatar answered Nov 17 '22 05:11

Roman C


The most simple way to use token with url is to use <s:token/> tag to set token value into session and retrieve it in <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>
like image 35
Aleksandr M Avatar answered Nov 17 '22 03:11

Aleksandr M