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";
    }

     

  • 相关阅读:
    今年要读的书
    java多线程
    json-lib 使用教程
    tomcat原理
    静态long类型常量serialVersionUID的作用
    使用junit4测试Spring
    MySQL各版本的区别
    spring mvc 下载安装
    hibernate、struts、spring mvc的作用
    【面试】hibernate n+1问题
  • 原文地址:https://www.cnblogs.com/wnwn/p/11825467.html
Copyright © 2011-2022 走看看