如何将 FileUploadInterceptor 与 struts2 约定插件一起使用

发布于 2025-01-03 10:25:35 字数 1710 浏览 5 评论 0 原文

我试图通过注释在操作上设置 FileUploadInterceptor :

@Namespace("/")
@ParentPackage("my-package")
@Result(name = "success", location = "/WEB-INF/jsp/result.jsp")
@InterceptorRef("fileUpload")
public class UploadAction extends ActionSupport {

  private File upload;
  private String uploadContentType;
  private String uploadFileName;

  public void setUpload(File upload) {
    this.upload = upload;
  }
  public void setUploadContentType(String uploadContentType) {
    this.uploadContentType = uploadContentType;
  }
  public void setUploadFileName(String uploadFileName) {
    this.uploadFileName = uploadFileName;
  }
  public File getUpload() {
    return this.upload;
  }
  public String getUploadContentType() {
    return this.uploadContentType;
  }
  public String getUploadFileName() {
    return this.uploadFileName;
  }
  @Override
  @Action("doUpload")
  public String execute()
  {
    System.out.println("Upload ok : " + (this.upload != null));
    return SUCCESS;
  }
}

我的问题是,只有当我不在操作类上设置任何拦截器时,它才有效。一旦我设置了拦截器,即使是像上面这样的 FileUploadInterceptor,该属性也不会被填充。

基本上,这有效:

public class UploadAction extends ActionSupport {...

但这不起作用:

@InterceptorRefs({
  @InterceptorRef("fileUpload")
})
public class UploadAction extends ActionSupport {...

或者

@InterceptorRefs({
  @InterceptorRef("fileUpload"),
  @InterceptorRef("myOtherinterceptor")
})
public class UploadAction extends ActionSupport {...

我发现!解决方案是:

@InterceptorRefs({
  @InterceptorRef("fileUpload"),
  @InterceptorRef("basicStack")
})
public class UploadAction extends ActionSupport {...

I'm trying to set the FileUploadInterceptor on an action by annotation :

@Namespace("/")
@ParentPackage("my-package")
@Result(name = "success", location = "/WEB-INF/jsp/result.jsp")
@InterceptorRef("fileUpload")
public class UploadAction extends ActionSupport {

  private File upload;
  private String uploadContentType;
  private String uploadFileName;

  public void setUpload(File upload) {
    this.upload = upload;
  }
  public void setUploadContentType(String uploadContentType) {
    this.uploadContentType = uploadContentType;
  }
  public void setUploadFileName(String uploadFileName) {
    this.uploadFileName = uploadFileName;
  }
  public File getUpload() {
    return this.upload;
  }
  public String getUploadContentType() {
    return this.uploadContentType;
  }
  public String getUploadFileName() {
    return this.uploadFileName;
  }
  @Override
  @Action("doUpload")
  public String execute()
  {
    System.out.println("Upload ok : " + (this.upload != null));
    return SUCCESS;
  }
}

My issue is that it works only if I don't set ANY interceptor on the action class. As soon as I set an interceptor, even FileUploadInterceptor like above, the attribute are not filled.

Basicaly, this works :

public class UploadAction extends ActionSupport {...

But this DOESN'T work :

@InterceptorRefs({
  @InterceptorRef("fileUpload")
})
public class UploadAction extends ActionSupport {...

or

@InterceptorRefs({
  @InterceptorRef("fileUpload"),
  @InterceptorRef("myOtherinterceptor")
})
public class UploadAction extends ActionSupport {...

I found ! The solution is :

@InterceptorRefs({
  @InterceptorRef("fileUpload"),
  @InterceptorRef("basicStack")
})
public class UploadAction extends ActionSupport {...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木落 2025-01-10 10:25:35

如果设置任何拦截器,则必须设置所有拦截器。基本上,您关闭了除上传拦截器之外的所有内容,这不是您想要的。

如果您要手动配置拦截器,并且需要引用多个拦截器,请按照@InterceptorRefs“wrapper”注释2.x/docs/convention-plugin.html#ConventionPlugin-InterceptorRefannotation" rel="nofollow">InterceptorRef 文档

If you set any interceptor, you must set all interceptors. Basically you're turning off all but the upload interceptor, which isn't what you want.

If you are going to manually configure interceptors, and need to refer to multiple interceptors, use the @InterceptorRefs "wrapper" annotation as per the InterceptorRef docs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文