Unable to implement Struts 2 token interceptor with hyperlink

前端 未结 2 583
挽巷
挽巷 2020-12-01 17:14

I tried to implement token interceptor with the tag but its showing error on the first click. i.e The form has already been processed or

相关标签:
2条回答
  • 2020-12-01 17:53

    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>
    
    0 讨论(0)
  • 2020-12-01 18:16

    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>
    
    0 讨论(0)
提交回复
热议问题