我在這裏閱讀所有有關此問題,因此我仔細地遵循official Struts 2 file upload docs但我仍然有問題。該操作完成後沒有錯誤,但我似乎無法捕獲上傳的文件,因爲操作內部的3個文件屬性始終爲空。這就像fileUploadInterceptor沒有完成它的工作。這裏是我的代碼:Struts 2文件上傳 - 文件屬性的實際操作爲空
動作映射:
<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;
}
}
謝謝和抱歉,對於遲到接受:)我只是現在有時間玩這個問題。 – stojke