Unable to implement Struts 2 token interceptor with hyperlink

I tried to implement an intercept token with the <s:url .. tag, but its error display on the first click. ie The form has already been processed or no token was supplied, please try again.

I want to implement this interceptor, because if users have already deleted the line and reloaded the page again, then the same action should not be performed 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> 
+5
source share
3 answers

The s: token tag simply places a hidden element containing a unique token.

No need to use a token with a url because the form must be submitted. If you want to pass some token as a parameter, you need to use the s:param tag.

Define a 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 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> 
+5
source

The easiest way to use the token with url is to use the <s:token/> to set the value of the token in the session and get it in the <s:param> .

 <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> 
+4
source
 <action name="resetScoreHistory" class="scoreAction" method="resetHistory"> <result name="success" type="redirectAction"> <param name="actionName">scoreAction</param> <param name="backToScoringFlag">${#parameters['backToScoringFlag']}</param> <param name="token">${#parameters['token']}</param> </result> </action> 
0
source

All Articles