在Spring MVC中使用Model绑定对象

时间:2015-02-19 17:52:54

标签: spring model-view-controller

我想将list变量与我在JSP页面中使用的Model Object绑定。但我无法在JSP页面上找到Model值。它只返回null。但如果我使用System.out.println(s)打印此对象,它们将打印出完美的值,但我无法在JSP页面上找到此对象值。这背后可能的原因是什么?

${list}                             //nothing to print on jsp page
<%=session.getAttribute("list")%>   //this will return null on jsp

我的控制器代码在这里: -

@RequestMapping(value = "admin/orderList/detailview/{orderId}")
public String viewDetails(@PathVariable("orderId") Integer orderId,HttpSession session, Model m) {

    List<OrderFullViewDTO> s=orderService.fullViewDetails(orderId);
    System.out.println("-----output is------"+s);
    m.addAttribute("list", s);
    session.setAttribute("list", s);

    return "redirect:/admin/orderList";
}


        @RequestMapping("/admin/orderList") public String orderList(Model m) { 
//Show all order placed by the user 
    m.addAttribute("op", orderDao.findAll()); 
    m.addAttribute("notify", notifyDao.totalListRecords());    m.addAttribute("notifyOrder", notifyDao.totalOrderRecord()); 
    return "OrderList"; 
} 

2 个答案:

答案 0 :(得分:1)

您正在重定向到某个URL,因此浏览器会执行新的GET请求并加载页面,您应该使用flash属性概念来保存参数;

REFER

@RequestMapping(value = "admin/orderList/detailview/{orderId}")
public String viewDetails(@PathVariable("orderId") Integer orderId,HttpSession session, Model m, RedirectAttributes redirectAttrs) {

    List<OrderFullViewDTO> s=orderService.fullViewDetails(orderId);
    System.out.println("-----output is------"+s);
 redirectAttrs.addFlashAttribute("list", s);
    return "redirect:/admin/orderList";
}

在JSP中,您可以访问与模型属性相同的内容。

<强>更新

@RequestMapping("/admin/orderList") public String orderList(Model m, @RequestParam List<OrderFullViewDTO> list) { 
//Show all order placed by the user 
    m.addAttribute("op", orderDao.findAll()); 
    m.addAttribute("notify", notifyDao.totalListRecords());    m.addAttribute("notifyOrder", notifyDao.totalOrderRecord()); 
m.addAttribute("list", list);
    return "OrderList"; 

答案 1 :(得分:1)

@RequestMapping("/admin/orderList")
public String orderList(Model m) {
    //Show all order placed by the user
    m.addAttribute("op", orderDao.findAll());
    m.addAttribute("notify", notifyDao.totalListRecords());
    m.addAttribute("notifyOrder", notifyDao.totalOrderRecord());
    return "OrderList";
}