0

I can't figure it out why that is not working. That is the jsp that make me problems. I follow a tutorial from youtube and my jsp looks the same like the jsp from the video. I adapted the code from the video but i don't think that is the problem because the controller and the jsp are the same like those from the video. here is the tutorial, jsp is at min 24

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Upload Page</title>
</head>
<body>

<spring:url value="/doUpload" var="doUplodaURL" />
<form:form method="post" modelAttribute="formUpload" action="${doUplodaURL 
 }" enctype="multipart/form-data" >
<form:input path="files" type="file" multiple="multiple"/>
<form:errors path="files" /><br/>

<button type="submit" >Upload</button>

</form:form>
</body> 
</html>

and that is my controller

@Controller
public class CsvController {

@Autowired
FileValidator fileValidator;

@Autowired
CsvServices csvServices;

@RequestMapping(value = "/uploadPage", method = RequestMethod.GET)
public ModelAndView getPage() {
    ModelAndView model = new ModelAndView("upload_page");
    FileUpload formUpload = new FileUpload();
    model.addObject("formUpload", formUpload);

    return model;
}

@RequestMapping (value="/doUpload", method=RequestMethod.POST)
public String doUpload(@ModelAttribute("formUpload") FileUpload fileUpload, BindingResult result, RedirectAttributes redirectAttributes ) throws IOException {
    fileValidator.validate(fileUpload, result);

    if(result.hasErrors()) {
        return "uploadPage";
    } else {
        redirectAttributes.addFlashAttribute("fileNames", uploadAndImportDb(fileUpload));

        return "redirect:/succes";
    }
}

@RequestMapping(value = "/succes", method = RequestMethod.GET)
public ModelAndView succes() {
    ModelAndView model = new ModelAndView("succes");
    return model;
}

private List<String> uploadAndImportDb(FileUpload fileUpload) throws IOException{
    List<String> fileNames = new ArrayList<String>();
    List<String> paths = new ArrayList<String>();
    CommonsMultipartFile[] commonsMultipartFiles = fileUpload.getFiles();

    String filePath = null;

    for(CommonsMultipartFile multipartFile : commonsMultipartFiles) {
        filePath = "C:\\Users\\bogda\\Desktop\\input\\" + multipartFile.getOriginalFilename();
        File file = new File(filePath);

        FileCopyUtils.copy(multipartFile.getBytes(), file);
        fileNames.add(multipartFile.getOriginalFilename());

        paths.add(filePath);
    }

    //parse and import
    csvServices.process(paths);

    return fileNames;
}
}

OUTPUT

  • 1
    Possible duplicate of [Why am I getting a NoClassDefFoundError in Java?](https://stackoverflow.com/questions/34413/why-am-i-getting-a-noclassdeffounderror-in-java) – M. le Rutte May 29 '18 at 16:41
  • How you deploy your jsp/controller? Do you use Maven to build war and deploy that war? – Ivan May 29 '18 at 16:57

1 Answers1

0

Kindly add commons-fileupload-x.x.jar inside WEb_INF/lib folder.

SanTech
  • 128
  • 2
  • 14