使用SMM框架开发企业级应用-----MVC方法返回值类型

MVC方法返回值类型

  ModelAndView返回值类型:

    1.当返回为null时,页面不跳转。

    2.当返回值没有指定视图名时,默认使用请求名作为视图名进行跳转。

    3.当返回值指定了视图名,程序会按照视图名跳转。

/*添加*/
@RequestMapping("/getSale")
public ModelAndView addSale(Sale sale,HttpServletRequest request,ModelAndView mv){
    if (sale!=null) {
        Double totalPrice = sale.getPrice() * sale.getQuantity();
        sale.setTotalPrice(totalPrice);
        sale.setSaleDate(new Date());
        Users user = (Users) request.getSession().getAttribute("user");
        sale.setUserId(user.getUid());
        int i = userService.addSale(sale);
        if (i > 0) {
            mv.setViewName("saleList");
        } else {
            mv.setViewName("prodectAdd");
        }
    }
    return mv;
}

  Objectl返回值类型;

/*绑定下拉框*/
@RequestMapping("/prodectName")
@ResponseBody
public Object getprodectName(){
    List<Product> products = userService.getproductName();
    return products;
}

  String返回值类型:

    1、如果返回值为null,那么以请求名作为视图名进行跳转

    2、如果指定返回值,那么按照指定返回值作为视图名进行跳转,可以通过model,modeMap携带数据。

    3、如果返回值带有forward或者redirect前缀,那么将会进行相应的请求或重定向,不过不能通过mvc的数据模型携带数据,可以通过ServletApi携带数据。

@RequestMapping("/welcome")
public String welcome(String userName, Model model){
    //将用户名保存到对应的作用域中
    model.addAttribute("userName",userName);
    return "welcome";
}

猜你喜欢

转载自www.cnblogs.com/haohanwuyin/p/11839151.html