zoukankan      html  css  js  c++  java
  • springmvc框架(五)——处理器返回值类型分类

    首先,创建一个index.jsp页面:

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
        
        <h3>返回值类型的分类</h3><br/>
        <a href="haha/hello">点击我!</a>
    
    </body>
    </html>

    前端资源目录:

    返回值类型分类:

    1.返回类型为String

    1. Controller方法返回字符串可以指定逻辑视图的名称,根据视图解析器为物理视图的地址。

    @RequestMapping("/haha")
    public class HelloController {
        @RequestMapping(value="/hello")
        //返回字符串
        public String sayHello() throws Exception{
            System.out.println("返回类型为字符串");
            return "success";
        }
    }

    若想在返回值类型为String的前提下实现请求转发和重定向,可以使用springmvc框架提供的forward关键字和redirect关键字实现!!!

    现在是不走视图解析器的,所以路径要写完整

        @RequestMapping("/hello")
        //使用forward关键字进行请求转发或redire关键字重定向
        //不走视图解析器,所以路径要写完整
        public String sayHello() throws Exception{
            
            /*System.out.println("使用forward关键字进行请求转发");
            return "forward:/WEB-INF/pages/success.jsp";*/
            
            System.out.println("使用redirect关键字进行重定向");
            return "redirect:/param.jsp";
    
        }

    注意:重定向redirect不能访问WEB-INF目录下的资源!!!!!转发可以!

    2. 返回值类型是void

    1. 如果控制器的方法返回值编写成void,可以使用请求转发或者重定向跳转到指定的页面(不经过视图解析器),也可以使用response直接响应数据。

        @RequestMapping("/hello")
        //返回类型为void
        public void sayHello(HttpServletRequest request, HttpServletResponse response)throws Exception{
            System.out.println("请求转发或者重定向");
            //请求转发
            /*request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request,response);*/
            //重定向
            /*response.sendRedirect("/param.jsp");*/
            
            //直接响应数据
            response.setContentType("text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write("哈哈");
        }

    使用response直接响应数据,由于默认查找jsp页面是查询不到的,所以会默认根据@RequestMapping("/hello")跳转到hello页面:

    3. 返回值类型是ModelAndView

    1. ModelAndView对象是Spring提供的一个对象,可以用来调整具体的JSP视图

    2. 具体的代码如下

    success.jsp页面:

    <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
        成功!!!<br/>
        ${user}
    </body>
    </html>

    处理器:

        @RequestMapping("/hello")
        //返回类型为ModelAndView
        public ModelAndView sayHello() throws Exception{
            System.out.println("返回类型为ModelAndView");
            ModelAndView mv = new ModelAndView();
            User user = new User();
            user.setName("瓜瓜");
            user.setAge(10);
            user.setDate(new Date());
            mv.addObject("user",user);
            mv.setViewName("success");//路径借助视图解析器,与视图解析器里的配置有关
            return mv;
        }

    结果展示:

  • 相关阅读:
    [转载-声网]18个实时音视频开发中会用到开源项目
    linux shell命令之wc/split及特殊字符
    RTP推流及验证
    Ubuntu18.04提示wifi无法连接
    ubuntu上Android开发环境及依赖项
    HLS playlist典型示例
    ffmpeg-hls实现分析
    AOSP中的HLS协议解析
    使用ffmpeg搭建HLS直播系统
    流媒体之HLS——综述(二)
  • 原文地址:https://www.cnblogs.com/churujianghudezai/p/12367792.html
Copyright © 2011-2022 走看看