我正在尝试使用 struts2 上传文件。
在jsp页面中有一个上传文件的按钮和一个Submit
按钮。程序中没有显示错误消息,但是当我单击Submit
按钮时没有任何反应。请参阅下面的代码:
动作类
package com.scrolls.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.io.File;
import java.util.Map;
import org.apache.commons.io.FileUtils;
public class UploadAction extends ActionSupport {
private File upload;
private String uploadContentType;
private String uploadFileName;
public String fileUpload() {
try {
String fullFileName = "c:/sample/mystruts/myfile.txt";
File theFile = new File(fullFileName);
FileUtils.copyFile(upload, theFile);
} catch (Exception e) {
System.out.println(e.toString());
return ERROR;
}
return SUCCESS;
}
// Plus public getters/setters for upload properties.
}
JSP
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<s:head theme="ajax" />
</head>
<body>
<s:form action="doUpload.action" enctype="multipart/form-data"/>
<s:datetimepicker name="date" displayFormat="yyyy-MM-dd" />
<s:file name="upload" />
<s:submit value="submit"/>
</body>
</html>
Struts 配置
<struts>
<package name="register3" extends="struts-default">
<action name="doUpload" class="com.scrolls.action.UploadAction" method="fileUpload">
<result name="success">/suc.jsp</result>
<result name="error">/fail.jsp</result>
</action>
</package>
</struts>