SpringMVC学习(四)结果跳转的方式:转发和重定向

通过SpringMVC的方式实现转发和重定向:
无需视图解析器
Spring的配置文件:已经注掉了视图解析器
<?xml version="1.0" encoding="UTF-8"?>
<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
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.zyh.controller"/>
    <mvc:default-servlet-handler/>
    <mvc:annotation-driven/>
<!--    &lt;!&ndash;视图解析器&ndash;&gt;
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>-->

    <bean name="/test" class="com.zyh.controller.HelloController"/>
</beans>
编写Controller

转发

  • 方式一: 直接拼接要跳转的页面
package com.zyh.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ModelController {
    @RequestMapping("/m1/t1")
    public String test1(Model model) {
        model.addAttribute("msg","ModelTest1");
        //转发方式一
        return "/index.jsp";
    }
}

测试:可以看到我们地址栏的URL没有发生变化就跳转到了Index.jsp首页。
在这里插入图片描述

  • 方式二: forward:要跳转的页面
return "forward:/index.jsp";

测试:也可以跳转到我的首页
在这里插入图片描述

重定向:

package com.zyh.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ModelController {
    @RequestMapping("/m1/t1")
    public String test1(Model model) {
        model.addAttribute("msg","ModelTest1");
        //重定向
        return "redirect:/index.jsp";
    }
}

测试:也同样跳转到我的首页,但仔细看就发现我的地址栏的URL发生了变化。
在这里插入图片描述

使用视图解析器:
在Spring的配置文件中注入视图解析器
<?xml version="1.0" encoding="UTF-8"?>
<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
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.zyh.controller"/>
    <mvc:default-servlet-handler/>
    <mvc:annotation-driven/>
    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <bean name="/test" class="com.zyh.controller.HelloController"/>
</beans>

编写Controller
转发:

    @RequestMapping("m1/t2")
    public String test2(Model model) {
        model.addAttribute("msg","ModelTest2");
        //有视图解析器转发
        return "test";
    }

启动Tomcat服务器测试:地址栏URL没有发生变化
在这里插入图片描述
重定向:

    @RequestMapping("m1/t2")
    public String test2(Model model) {
        model.addAttribute("msg","ModelTest2");
        //有视图解析器转发
        return "redirect:/index.jsp";
    }

测试:地址栏URL发生了变化,页面成功跳转。

在这里插入图片描述

总结:
1、资源路径问题
符号描述
/代表根目录
. /代表当目录
. . /代表上一级目录
2、转发和重定向的区别
转发重定向
地址栏URL是否变化不变化变化
请求次数一次请求两次请求
访问资源层次转发只能访问内部资源重定向可以访问外部资源
行为层次转发是服务器的行为重定向是客户端行为,它改变浏览器与服务器之间数据通信的地址,相当于浏览器重新访问的地址
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值