• 03【Controller方法返回值详解】



    三、Controller方法返回值详解

    3.1 返回普通字符串

    3.1.1 跳转

    package com.dfbz.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    @RequestMapping("/demo")            // 为此Controller命名一个请求路径,以后访问此Controller下的任意方法都需要加上/demo
    public class DemoController {
        
        @RequestMapping("/demo01")		// 后台请求可以不写.from
        public String demo01(){
            return "hello";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    访问:http://localhost:8080/demo/demo01.form

    在这里插入图片描述

    客户端请求的URL未发生改变,请求却转到了/demo/hello,说明服务器内部发生跳转;

    也就是说,在返回字符串的情况下,SpringMVC默认当做视图进行跳转;

    3.1.2 设置视图解析器

    通常来说,我们的视图(页面)都是放在某个文件夹进行管理的,并且后缀通常都是固定的,要么是.html或者是.jsp再或者是其他的;因此我们希望可以固定好前缀(存放页面的文件夹名称)和后缀(文件名的后缀)

    在这里插入图片描述

    使得我们的代码改为:

    /**
     * 指定进行视图跳转
     *
     * @return
     */
    @RequestMapping("/demo02")
    public String demo02() {
        return "index02";          // 自动跳转到 /WEB-INF/views/index02.jsp
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    很显然,我们现在肯定是做不到的;

    我们在前面查看SpringMVC源码时,发现SpringMVC默认使用的是InternalResourceViewResolver来进行视图页面的解析;这个类中提供了视图的前缀和后缀的配置;

    在这里插入图片描述

    dispatcher-servlet.xml中配置InternalResourceViewResolver

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        <context:component-scan base-package="com.dfbz"/>
    
       
        <mvc:view-resolvers>
            <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                
                <property name="prefix" value="/WEB-INF/views/"/>
    
                
                <property name="suffix" value=".jsp">property>
            bean>
        mvc:view-resolvers>
        
        
        <mvc:annotation-driven/>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    访问:http://localhost:8080/demo/demo02.form

    在这里插入图片描述

    3.2 返回ModelAndView

    ModelAndView:翻译过来就是模型和视图的意思,该对象保存了我们我们填充的数据(在request域中)和要跳转的视图地址;

    3.2.1 普通视图

    通过ModelAndView设置的视图,SpringMVC默认将其跳转到这个视图,并且该视图会经过视图解析的前后缀处理;

    /**
     * 指定进行视图跳转
     *
     * @return
     */
    @RequestMapping("/demo03")
    public ModelAndView demo03() {
        ModelAndView mv=new ModelAndView();
        
        // 跳转到/WEB-INF/views/index03.jsp
        mv.setViewName("index03");              
        mv.addObject("msg","index03~~");
    
        return mv;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    创建:/WEB-INF/views/index03.jsp

    在这里插入图片描述

    3.2.2 RedirectView

    RedirectView是一种特殊的视图,SpringMVC会将其重定向到这个视图,并且RedirectView允许携带重定向参数;

    /**
     * 使用RedirectView视图进行重定向
     *
     * @return
     */
    @RequestMapping("/hello")
    public ModelAndView hello() {
    
        RedirectView redirectView = new RedirectView();
    
        // RedirectView不会参与视图解析器的前后缀处理
        redirectView.setUrl("/hello.jsp");
        redirectView.addStaticAttribute("name","xiaohui");
        redirectView.addStaticAttribute("age",20);
    
        return new ModelAndView(redirectView);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 准备JSP页面:
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    
    
        Title
    
    
        

    name: ${param.name}

    age: ${param.age}

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    Tips:params是jsp的内置参数,用于获取请求该JSP所携带的参数;

    访问:http://localhost:8080/demo/hello.form

    在这里插入图片描述

    3.3 返回特殊字符串

    我们前面的测试中,在返回普通字符串时SpringMVC默认是将其作为视图进行跳转,并且可以收视图解析器的前缀/后缀所控制,那么如果我们配置了前缀/后缀同时某个跳转不需要加上前缀后缀呢?

    在SpringMVC提供了两个特殊字符串前缀:

    • forwrad:进行页面的跳转,该跳转不经过前缀和后缀处理
    • redirect:进行页面的重定向,该重定向不经过前缀和后缀处理
    /**
     * 返回特殊字符串:forward-->转发(不会经过视图解析器)
     * @return
     */
    @RequestMapping("/demo04")
    public String demo04() {
    
        return "forward:/index04.jsp";          // 转发到http://localhost:8080/index04.jsp
    }
    
    /**
     * 返回特殊字符串:redirect(不会经过视图解析器)
     * @return
     */
    @RequestMapping("/demo05")
    public String demo05() {
    
        return  "redirect:/index05.jsp";         // 重定向到http://localhost:8080/index05.jsp
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    访问:http://localhost:8080/demo/demo04.form

    在这里插入图片描述

    访问:http://localhost:8080/demo/demo05.form

    在这里插入图片描述

    3.4 返回void

    我们知道,SpringMVC把方法的返回值当做视图进行跳转,如果返回void代表的就是不需要提供视图,一般用于ajax请求,只需要响应数据,不需要返回视图

    /**
     * 返回void(用于ajax请求,不需要提供页面,只需要响应数据即可)
     *
     * @param response
     * @throws IOException
     */
    @RequestMapping("/demo06")
    public void demo06(HttpServletResponse response) throws IOException {        // 接收request和response
        // 写出数据给前端
        response.getWriter().write("hello springmvc!");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    Tips:只要是Controller中的方法,都可以自动绑定request、response、session这些servlet的原生api;

    访问:http://localhost:8080/demo/demo06.form

    在这里插入图片描述

  • 相关阅读:
    4、QT中的网络编程
    Redis全文搜索教程之创建索引并关联源数据
    中小微企业中的营业额与收入评估的风险模型预测
    计算结构体成员相对于起始位置的偏移量 —— 宏 offsetof
    直播岗位认知篇
    尖端AR技术如何在美国革新外科手术实践?
    win10安装.net3.5
    笔试算法题ACM模式输入输出处理
    7个在Github上的flutter开源程序
    数据结构——折半插入排序
  • 原文地址:https://blog.csdn.net/Bb15070047748/article/details/128105239