gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:10:50 24 4
gpt4 key购买 nike

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

最佳答案

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

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

关于java - 显示表列表中的所有数据(java spring mvc),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33821958/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com