My task was to create a download function within a Springframework project. After submitting the jsp form, data would be fetched from the database and saved into a new Excel file, which would then be downloaded by the user.
Below are snippets of relevant code:
<form id="form1" name="form1" method="post" action="" onsubmit="javascript:searchList();">
<input ></input>
…
</form>
function searchList(){
… // ajax call
}
Here is the related Java code:
@RequestMapping(value = "/search", method = RequestMethod.POST)
public Object search(@RequestParam(“xxx”) String xxx, HttpServletResponse response) {
try {
// Creating workbook and sheet, generating content
// Writing Excel file for download
} catch (IOException e) {
// Error handling
} catch (Exception e) {
// More error handling
}
}
After testing the function, I encountered the following issues: 1. The code executed successfully but no file was actually downloaded. 2. A Servlet Exception was thrown with the message indicating that a JSP file was not found.
/report/search was supposed to be a URL endpoint, why did it point to a file path? How can this issue be resolved?