用贯了spring mvc的注入式文件上传,回到struts中都忘了怎么写,翻了翻老项目,记录下。
struts config中,定义formBean,action中用name指定formBean。
<struts-config>
<form-beans>
<form-bean name="fileManagerForm" type="com.dorole.FileManagerForm" />
</form-beans>
<action path="..." type="..." parameter="method" name="fileManagerForm">
<forward name="..." path="..."></forward>
</action>
</struts-config>
FileManagerForm如下
public class FileManagerForm extends ActionForm {
private FormFile file;
public void setFile(FormFile file) {
this.file = file;
}
public FormFile getFile() {
return file;
}
}
FileManagerAction如下
public ActionForward upload(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
FileManagerForm fmf = (FileManagerForm) form;
FormFile formFile = fmf.getFile();
if (formFile.getFileData().length != 0) {
...
}
return null;
}
jsp如下
<form action="..." method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="upload" />
</form>
