UPDATE: I have made the observation that the cause of the null value being passed is enctype="multipart/form-data", but that part is needed in order to pass the file to the controller. The issue below remains.
I have been trying to set up a Spring MVC page that will allow the user to upload a document. I've looked around and seem to have a general idea on how I should go about implementing this (using "commons-fileupload" and "commons-io") so the following is my view, controller and model.
View:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ page session="false" %>
<html>
<head>
<title>Upload Document</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
</script>
<script type="text/javascript" src="resources/rulesextractor.js"></script>
<link rel="stylesheet" type="text/css" href="./resources/css/rulesextractor.css" />
</head>
<body>
<c:import url = "./header.jsp">
<c:param name = "title" value = "Upload Document"/>
<c:param name = "region" value = "${region}"/>
</c:import>
<br>
Please choose a Document to upload
<br>
<form:form method="POST" modelAttribute="uploadForm" commandName="uploadForm" name="uploadForm" id="uploadForm" enctype="multipart/form-data">
<form:input path="fileData" name="fileData" type="file"/>
<br>
<input type="submit" name="submit" value="Submit" id="subbtn"/>
<input type="button" value="Main Menu" onclick="back();"/>
</form:form>
</body>
</html>
Controller:
@Controller
public class UploadController extends BaseController {
private static final Logger logger = LoggerFactory.getLogger(FlowController.class);
//@Autowired
//private UserPreferences userPreferences;
@Autowired
GenericSearchBO genericSearchBO;
@RequestMapping(value = "upload", method = RequestMethod.GET)
public String getUploadPage(Model m){
logger.info("UploadController: Getting Upload page");
updateRegionOnModel(m);
m.addAttribute(new UploadForm());
return "/pages/upload";
}
@RequestMapping(method = RequestMethod.POST)
public String create(@Valid UploadForm UploadForm, BindingResult result){
if (result.hasErrors())
{
for(ObjectError error : result.getAllErrors())
{
System.err.println("Error: " + error.getCode() + " - " + error.getDefaultMessage());
}
return "/pages/upload";
}
// Some type of file processing...
System.err.println("-------------------------------------------");
//.getOriginalFilename()
System.err.println("Test upload: " + UploadForm.getFileData());
System.err.println("-------------------------------------------");
return "redirect:/pages/upload";
}
}
Model:
public class UploadForm {
private CommonsMultipartFile fileData;
public CommonsMultipartFile getFileData() {
System.out.println("REACHED GET METHOD");
return fileData;
}
public void setFileData(CommonsMultipartFile fileData) {
System.out.println("REACHED SET METHOD");
this.fileData = fileData;
}
}
I'm new to Spring, and MVC in general, but I'm slowly getting the hang of it. The issue here is that I am in fact entering the Controller's public String create(@Valid UploadForm UploadForm, BindingResult result) method, but UploadForm is being passed as a null value and I can't, for the life of me, figure it out and it's getting quite frustrating. Any help or hints will be greatly appreciated. Thanks!