通过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/>
<!-- <!–视图解析器–>
<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是否变化 | 不变化 | 变化 |
请求次数 | 一次请求 | 两次请求 |
访问资源层次 | 转发只能访问内部资源 | 重定向可以访问外部资源 |
行为层次 | 转发是服务器的行为 | 重定向是客户端行为,它改变浏览器与服务器之间数据通信的地址,相当于浏览器重新访问的地址 |