如何将 FileUploadInterceptor 与 struts2 约定插件一起使用
我试图通过注释在操作上设置 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 {...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果设置任何拦截器,则必须设置所有拦截器。基本上,您关闭了除上传拦截器之外的所有内容,这不是您想要的。
如果您要手动配置拦截器,并且需要引用多个拦截器,请按照@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.