Reputation: 59
I'm trying to upload a file using struts2.
In the jsp page there is a button for uploading the file and a Submit
button. No error message is shown in the program, but when I click the Submit
button nothing happens. See my code below:
Action class
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 config
<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>
Upvotes: 4
Views: 1252
Reputation: 50193
You have a self closing form...
Try like this:
<s:form action="doUpload.action"
method="POST"
enctype="multipart/form-data" >
<s:datetimepicker label="Select Date"
name="date"
displayFormat="yyyy-MM-dd"
required="true" />
<s:file label="File:" name="upload" />
<s:submit value="submit" />
</s:form>
Thanks to Anu for corrections, +1...
Upvotes: 3