显示表列表中的所有数据(java spring mvc)

时间:2015-11-20 08:19:34

标签: java html spring-mvc

我想将上传的详细信息显示在列表中,我更倾向于使用任何简单的步骤来显示所有细节。以下是我的代码,每次上传文件时只能显示一个数据。我也明白我应该在get方法中创建“addObject”,而不是post方法。如何以arrayList或任何其他方式显示?任何帮助将不胜感激!

This is controller 
@RequestMapping(value = "/product", method = RequestMethod.GET)
    public Object uploadProducts(@ModelAttribute UploadCreate uploadCreate,HttpSession session,RedirectAttributes redirectAttributes) {

     //   redirectAttributes.addFlashAttribute("list",employeeDetail.getName());
     //   redirectAttributes.addFlashAttribute("name",employeeDetail.getName());;

        return "product/upload";

    }

    @RequestMapping(value = "/product", method = RequestMethod.POST, consumes = "multipart/form-data")
    public Object uploadProducts(@RequestParam("file") MultipartFile file) {

        try {

            UploadCreate uploadCreate = new UploadCreate();
            uploadCreate.setName(file.getOriginalFilename());
            uploadCreate.setContentType(file.getName());
            uploadCreate.setContent(file.getBytes());
            uploadCreate.setUploadedDate(new Date());
            uploadService.uploadProducts(uploadCreate);
            return new ModelAndView("product/upload")
                    .addObject("error", "Product upload scheduled.")
                    .addObject("fileList", uploadCreate);


        } catch (Exception e) {
            return new ModelAndView("product/upload").addObject("error", e.getMessage());
        }
    }

HTML:

<table id="uploaded-files">
    <tr>
        <th>File Name</th>
        <th>File Size</th>
        <th>File Type</th>
        <th>Uploaded Date</th>
    </tr>
    {{#fileList}}
    <tr>
        <td>{{name}}</td>
        <td>{{content}}</td>
        <td>{{contentType}}</td>
        <td>{{uploadedDate}}</td>
    </tr>
    {{/fileList}}
</table>

1 个答案:

答案 0 :(得分:2)

你应该使用会话范围而不是请求范围;

@RequestMapping(value = "/product", method = RequestMethod.POST, consumes = "multipart/form-data")
public Object uploadProducts(@RequestParam("file") MultipartFile file,HttpServletRequest request) {

    try {
        Session session = request.getSession();
        UploadCreate uploadCreate = new UploadCreate();
        uploadCreate.setName(file.getOriginalFilename());
        uploadCreate.setContentType(file.getName());
        uploadCreate.setContent(file.getBytes());
        uploadCreate.setUploadedDate(new Date());
        uploadService.uploadProducts(uploadCreate);
        List<UploadCreate> fileList =     
        (List<UploadCreate>)session.getAttribute("list");
        if(fileList==null){
          fileList = new ArrayList<UploadCreate>();
        }
        fileList.add(uploadCreate);
        session.setAttribute("list",fileList);

        return new ModelAndView("product/upload")
                .addObject("error", "Product upload scheduled.");
         //the method addObject() means to add data into request ; 
         //and the previous request and current request can not share the same data ;


    } catch (Exception e) {
        return new ModelAndView("product/upload").addObject("error", e.getMessage());
    }
}