显示表列表中的所有数据(java spring mvc)
我想将上传的详细信息显示到列表中,我更愿意使用任何简单的步骤来显示所有的详细信息。下面是我的代码,每次上传文件时只能显示一个数据。我也明白我应该在get方法中创建“addObject”,而不是post方法。如何以arrayList或任何其他方式显示?任何帮助都将不胜感激显示表列表中的所有数据(java spring mvc),java,html,spring-mvc,Java,Html,Spring Mvc,我想将上传的详细信息显示到列表中,我更愿意使用任何简单的步骤来显示所有的详细信息。下面是我的代码,每次上传文件时只能显示一个数据。我也明白我应该在get方法中创建“addObject”,而不是post方法。如何以arrayList或任何其他方式显示?任何帮助都将不胜感激 This is controller @RequestMapping(value = "/product", method = RequestMethod.GET) public Object uploadProduc
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:
文件名
文件大小
文件类型
上载日期
{{{#文件列表}
{{name}}
{{content}}
{{contentType}}
{{上传日期}
{{/fileList}
您应该使用会话范围而不是请求范围
@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());
}
}
@RequestMapping(value=“/product”,method=RequestMethod.POST,consumes=“多部分/表单数据”)
公共对象上载产品(@RequestParam(“file”)MultipartFile文件,HttpServletRequest请求){
试一试{
Session Session=request.getSession();
UploadCreate UploadCreate=新建UploadCreate();
uploadCreate.setName(文件.getOriginalFilename());
uploadCreate.setContentType(file.getName());
uploadCreate.setContent(file.getBytes());
uploadCreate.setUploadedDate(新日期());
uploadService.uploadProducts(uploadCreate);
列表文件列表=
(List)session.getAttribute(“List”);
if(fileList==null){
fileList=newarraylist();
}
添加(上传创建);
session.setAttribute(“列表”,文件列表);
返回新模型和视图(“产品/上传”)
.addObject(“错误”,“已计划产品上载”);
//addObject()方法意味着向请求中添加数据;
//前一个请求和当前请求不能共享相同的数据;
}捕获(例外e){
返回新的ModelAndView(“产品/上传”).addObject(“错误”,例如getMessage());
}
}
是否要在文件列表中显示每个会话的所有上载文件??
@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());
}
}