zoukankan      html  css  js  c++  java
  • 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";
    }

     

  • 相关阅读:
    愤怒
    Eclipse的调试功能的10个小窍门
    PL/SQL之基础篇
    PL/SQL之高级篇
    luogu P1015 回文数
    Noip2011 提高组 Day1 T3 Mayan游戏
    各种各样的——玄学卡常技巧
    北京清北 综合强化班 Day5
    [UVA12003] Array Transformer(分块,二分,暴力)
    [POJ3468] A Simple Problem with Integers(分块)
  • 原文地址:https://www.cnblogs.com/wnwn/p/11825467.html
Copyright © 2011-2022 走看看