【问题标题】:Display all data in a Table List (java spring mvc)显示表格列表中的所有数据(java spring mvc)
【发布时间】:2016-02-22 15:53:14
【问题描述】:

我想将上传的详细信息显示到列表中,我更喜欢使用任何简单的步骤来显示所有详细信息。以下是我每次上传文件时只能显示一个数据的代码。我也明白我应该在 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>

【问题讨论】:

  • 您想在fileList 中显示每个Session 上传的所有文件??

标签: java html spring-mvc


【解决方案1】:

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

@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());
    }
}

【讨论】:

    猜你喜欢
    • 2016-06-11
    • 2012-05-15
    • 2015-12-15
    • 2020-10-23
    • 1970-01-01
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    • 2010-11-26
    相关资源
    最近更新 更多