无法使用超链接实现 Struts 2 令牌拦截器 [英] Unable to implement Struts 2 token interceptor with hyperlink

查看:20
本文介绍了无法使用超链接实现 Struts 2 令牌拦截器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 <s:url .. 标签实现令牌拦截器,但在第一次点击时显示错误.即

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>

和我的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>
            

推荐答案

s:token 标签只是放置一个隐藏元素,其中包含独特的令牌.

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

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

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.

定义参数

  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>

这篇关于无法使用超链接实现 Struts 2 令牌拦截器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆