我在这里阅读了关于此问题的所有问题,我仔细地跟着official Struts 2 file upload docs,但我仍然有问题。操作完成没有错误,但我似乎无法捕获上传的文件,因为操作中的3个文件属性始终为null。这就像fileUploadInterceptor没有完成它的工作。这是我的代码:
动作映射:
<action name="merchantSaveOrUpdate" class="merchantSaveOrUpdateAction">
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/jpeg,image/gif,image/png</param>
</interceptor-ref>
<result name="success" type="redirectAction">merchantList</result>
</action>
JSP:
<s:form action="merchantSaveOrUpdate" method="POST" enctype="multipart/form-data">
<label>Merchant Name</label> <input type="text" value="${merchant.name}" name="name"><br />
<label>Merchant Logo</label> <s:file name="logo" /> <br />
<s:submit class="btn submit" />
</s:form>
动作:
public class MerchantSaveOrUpdateAction extends ActionSupport {
private File logo;
private String logoContentType;
private String logoFileName;
private String name;
public File getLogo () {
return logo;
}
public void setLogo ( File logo ) {
this.logo = logo;
}
public String getLogoContentType () {
return logoContentType;
}
public void setLogoContentType ( String logoContentType ) {
this.logoContentType = logoContentType;
}
public String getLogoFileName () {
return logoFileName;
}
public void setLogoFileName ( String logoFileName ) {
this.logoFileName = logoFileName;
}
public String getName () {
return name;
}
public void setName ( String name ) {
this.name = name;
}
public String execute() throws Exception {
String result = super.execute();
// the problem is here - name is populated, but the 3 logo properties are null
return result;
}
}
答案 0 :(得分:2)
为动作定义拦截器时,必须为该动作定义所有拦截器。
换句话说,你的动作目前只有一个拦截器正在运行,即文件拦截器。
请注意,链接页面上的所有配置都不包含 文件拦截器。
defaultStack
包含文件拦截器。该操作可以像这样配置:
<action name="merchantSaveOrUpdate" class="merchantSaveOrUpdateAction">
<interceptor-ref name="defaultStack">
<param name="fileUpload.allowedTypes">image/jpeg,image/gif,image/png</param>
</interceptor-ref>
<result name="success" type="redirectAction">merchantList</result>
</action>
答案 1 :(得分:0)
关于您的问题,您可以在struts.xml中包含文件upload.xml。
<struts>
<include file="fileupload.xml"></include>
<package name="default" extends="struts-default">
<!-- file upload -->
<action name="upload" class="com.action.FileUpload">
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>
</action>
</package>
</struts>
此文件upload.xml应包含拦截器ref ...
<struts>
<package name="fileUploadPackage" extends="struts-default">
<interceptors>
-------------
-------------
<interceptor-ref name="fileUpload">
<param name="maximumSize">your file size</param>
<param name="allowedTypes">image/jpeg,image/gif,image/png</param>
</interceptor-ref>
------------------
------------------
</interceptors>
</package>
</struts>
也可以通过其他方式完成,请查看此网址file upload