[英]Display all data in a Table List (java spring mvc)
我想將上傳的詳細信息顯示在列表中,我更喜歡采取任何簡單的步驟來顯示所有詳細信息。 以下是我的代碼,每次上傳文件時,該代碼只能顯示一個數據。 我也了解我應該在get方法中而不是post方法中創建“ addObject”。 如何在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>
您應該使用會話范圍而不是請求范圍;
@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());
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.