如何使用带有struts2约定插件的FileUploadInterceptor

时间:2012-02-08 22:50:14

标签: upload struts2 annotations interceptor convention

我正在尝试通过注释设置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 {...

1 个答案:

答案 0 :(得分:0)

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

如果您要手动配置拦截器,并需要引用多个拦截器,请根据InterceptorRef docs使用@InterceptorRefs“包装器”注释。