SpringMVC中的视图是View接口,视图的作用渲染数据,将模型Model中的数据展示给用户 SpringMVC视图的种类很多,默认有转发视图和重定向视图
当工程引入jstl的依赖,转发视图会自动转换为JstlView
若使用的视图技术为Thymeleaf,在SpringMVC的配置文件中配置了Thymeleaf的视图解析器,由此视 图解析器解析之后所得到的是ThymeleafView
1、ThymeleafView
当控制器方法中所设置的视图名称没有任何前缀时,此时的视图名称会被SpringMVC配置文件中所配置 的视图解析器解析,视图名称拼接视图前缀和视图后缀所得到的最终路径,会通过转发的方式实现跳转
@RequestMapping("/testHello") public String testHello(){ return "hello"; }
2、转发视图
SpringMVC中默认的转发视图是InternalResourceView
SpringMVC中创建转发视图的情况: 当控制器方法中所设置的视图名称以"forward:"为前缀时,创建InternalResourceView视图,此时的视 图名称不会被SpringMVC配置文件中所配置的视图解析器解析,而是会将前缀"forward:"去掉,剩余部 分作为最终路径通过转发的方式实现跳转 例如"forward:/","forward:/employee"
代码:
1.
@Controller
public class TestContorller {
@RequestMapping("/")//此处的/与web.xml文件中的<url-pattern>/</url-pattern> 一致
public String getindex(){
return "index1";
}
@RequestMapping("/testview")
public String testView(){
return "test_view";
}
}
2.
@Controller
public class ViewContorller {
@RequestMapping("/testThymeleaf")
public String testThymeleaf(){
return "success";
}
//转发的一个请求
@RequestMapping("/testForward")
public String testForward(){
return "forward:/testThymeleaf";
}
}
3.test_view.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
testview 页面<br/>
<a th:href="@{/testThymeleaf}">testThymeleaf --》链接</a>
<a th:href="@{/testForward}">testThymeleaf --》链接testForward</a>
</body>
</html>
4.success.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>成功页面</title>
</head>
<body>
success<br/>
thymeleaf的格式写法<br/>
只有th:才会被thymeleaf解析<br/>
</body>
</html>
3、重定向视图
SpringMVC中默认的重定向视图是RedirectView
当控制器方法中所设置的视图名称以"redirect:"为前缀时,创建RedirectView视图,此时的视图名称不 会被SpringMVC配置文件中所配置的视图解析器解析,而是会将前缀"redirect:"去掉,剩余部分作为最 终路径通过重定向的方式实现跳转 例如"redirect:/","redirect:/employee"
@RequestMapping("/testRedirect") public String testRedirect(){ return "redirect:/testHello"; }
转发视图和重定向视图代码:
1.
package com.sun.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
//首页 转发访问
@Controller
public class TestContorller {
@RequestMapping("/")//此处的/与web.xml文件中的<url-pattern>/</url-pattern> 一致
public String getindex(){
return "index1";
}
@RequestMapping("/testview")
public String testView(){
return "test_view";
}
}
2.
package com.sun.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author sunyc
* @create 2022-04-16 14:33
*/
@Controller
public class ViewContorller {
@RequestMapping("/testThymeleaf")
public String testThymeleaf(){
return "success";
}
//转发的一个视图
@RequestMapping("/testForward")
public String testForward(){
return "forward:/testThymeleaf";
}
//重定向视图
@RequestMapping("/testRedirect")
public String testRedirect(){
return "redirect:/testThymeleaf";
}
}
3.test_view.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
testview 页面<br/>
<a th:href="@{/testThymeleaf}">testThymeleaf --》链接</a><br/>
<a th:href="@{/testForward}">testThymeleaf --》SpringMVC中默认的转发视图是InternalResourceView</a><br/>
<a th:href="@{/testRedirect}">testThymeleaf --》SpringMVC中默认的重定向视图是RedirectView</a><br/>
</body>
</html>
4.success.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>成功页面</title>
</head>
<body>
success<br/>
thymeleaf的格式写法<br/>
只有th:才会被thymeleaf解析<br/>
</body>
</html>
4、视图控制器view-controller
当控制器方法中,仅仅用来实现页面跳转,即只需要设置视图名称时,可以将处理器方法使用viewcontroller标签进行表示