zoukankan      html  css  js  c++  java
  • Spring MVC中 controller方法返回值

    1、返回ModelAndView

      定义ModelAndView对象并返回,对象中可添加model数据、指定view

    2、返回String

      1、表示返回逻辑视图名

        model对象通过 model.addAttribute("xxx",model)进行设定

      

      2、redirect重定向:

      redirect重定向特点:浏览器地址栏中的url会变化。修改提交的request数据无法传到重定向的地址。因为重定向后重新进行request(request无法共享)

       

      3、forward页面转发:

        通过forward进行页面转发,浏览器地址栏url不变,request可以共享

        

    3、返回void

      在controller方法形参上可以定义request和response,使用request或response指定响应结果:

      1、使用request转向页面,如下:

        request.getRequestDispatcher("页面路径").forward(request, response);

      2、也可以通过response页面重定向:

        response.sendRedirect("url")

      3、也可以通过response指定响应结果,例如响应json数据如下:

        response.setCharacterEncoding("utf-8");

        response.setContentType("application/json;charset=utf-8");

        response.getWriter().write("json串");

  • 相关阅读:
    Linux
    bzoj 1834
    bzoj 1002 找规律(基尔霍夫矩阵)
    bzoj 1005 组合数学 Purfer Sequence
    bzoj 1601 最小生成树
    bzoj 1001 平面图转对偶图 最短路求图最小割
    bzoj 1192 二进制
    bzoj 1012 基础线段树
    bzoj 1044 贪心二分+DP
    bzoj 1011 近似估计
  • 原文地址:https://www.cnblogs.com/liuwt365/p/5686659.html
Copyright © 2011-2022 走看看