我在 SO 上阅读了有关此问题的所有问题,并仔细遵循了官方 Struts 2 文件上传文档,但我仍然遇到问题。动作完成没有错误,但我似乎无法捕捉上传的文件,因为动作中的 3 个文件属性始终为空。就像 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;
}
}