SpringMVC的视图
一、什么是SpringMVC的视图
- SpringMVC中的视图是View接口,视图的作用渲染数据,将模型Model中的数据展示给用户。
- SpringMVC视图的种类很多,默认有转发视图InternalResourceView和重定向视图RedirectView。
- 当工程引入jstl的依赖,转发视图会自动转换为JstlView。
- 若使用的视图技术为Thymeleaf,在SpringMVC的配置文件中配置了Thymeleaf的视图解析器,由此视图解析器解析之后所得到的是ThymeleafView。
二、ThymeleafView
1. SpringMVC.xml
<?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="znb.mvc.controller"></context:component-scan>
<!-- 配置Thymeleaf视图解析器 -->
<bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<property name="order" value="1"/>
<property name="characterEncoding" value="UTF-8"/>
<property name="templateEngine">
<bean class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver">
<bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<!-- 视图前缀 -->
<property name="prefix" value="/WEB-INF/templates/"/>
<!-- 视图后缀 -->
<property name="suffix" value=".html"/>
<property name="templateMode" value="HTML5"/>
<property name="characterEncoding" value="UTF-8" />
</bean>
</property>
</bean>
</property>
</bean>
<mvc:view-controller path="/" view-name="index"></mvc:view-controller>
<mvc:view-controller path="/test_rest" view-name="test_rest"></mvc:view-controller>
<!--开启mvc的注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
2. 转发实现跳转
- 在配置好Thymeleaf的视图解析器后,当控制器方法中所设置的视图名称没有任何前缀时,此时的视图名称会被SpringMVC配置文件中所配置的视图解析器解析,视图名称拼接视图前缀和视图后缀所得到的最终路径,会通过转发的方式实现跳转。
- 例如有如下方法:
@RequestMapping("/testThymeleafView")
public String testThymeleafView(){
//转发-创建ThymeleafView
return "success";
}
- 该方法的请求路径为,视图前缀(/WEB-INF/templates/) + hello + 视图后缀(.html)
- 访问链接:http://localhost:8080/SpringMvc03/testThymeleafView
三、转发视图
- SpringMVC中默认的转发视图是InternalResourceView
- SpringMVC中创建转发视图的情况:
当控制器方法中所设置的视图名称以"forward:"为前缀时,创建InternalResourceView视图, 此时的视图名称不会被SpringMVC配置文件中所配置的视图解析器解析,而是会将前缀"forward:"去掉,剩余部分作为最终路径通过转发的方式实现跳转
- 例如有如下方法:
@RequestMapping("/testForword")
public String testForword(){
//转发,会访问 /testThymeleafView进行转发
return "forward:/testThymeleafView";
}
- 访问链接:http://localhost:8080/SpringMvc03/testForword
四、重定向视图
-
SpringMVC中默认的重定向视图是RedirectView
-
当控制器方法中所设置的视图名称以"redirect:"为前缀时,创建RedirectView视图,此时的视图名称不会被SpringMVC配置文件中所配置的视图解析器解析,而是会将前缀"redirect:"去掉,剩余部分作为最终路径通过重定向的方式实现跳转
重定向视图在解析时,会先将redirect:前缀去掉,然后会判断剩余部分是否以/开头, 若是则会自动拼接上下文路径
-
例如有如下方法:
@RequestMapping("/testRedirect")
public String testRedirect(){
//重定向-/testThymeleafView
return "redirect:/testThymeleafView";
}
- 访问链接:http://localhost:8080/SpringMvc03/testRedirect
1. 转发和重定向的区分
- 转发是一次请求,第一次是浏览器请求,第二次是服务器内部的请求;重定向是两次请求第一次是浏览器访问servlet,第二次是浏览器访问重定向的地址。
- 转发第二次是服务器内部的跳转,地址栏中的地址保持不变还是第一次发生请求的地址;重定向是浏览器发生了两次请求,最终地址栏中的地址是第二次请求的地址。
- 转发可以获取请求域中的数据,转发是一次请求用到的request对象是同一个;重定向不可以,两次请求对应两个request对象。
五、视图控制器view-controller
- 当控制器方法中,仅仅用来实现页面跳转,即只需要设置视图名称时,可以将处理器方法使用view-controller标签进行表示
<!--
path:设置处理的请求地址
view-name:设置请求地址所对应的视图名称
-->
<mvc:view-controller path="/" view-name="index"></mvc:view-controller>
- 测试
/*@RequestMapping("/")
public String index(){
return "index";
}*/
-
注意事项
当SpringMVC中设置任何一个view-controller时,其他控制器中的请求映射将全部失效, 此时需要在SpringMVC的核心配置文件中设置开启mvc注解驱动的标签:
<!--开启mvc的注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
- 完整的配置文件
<?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="znb.mvc.controller"></context:component-scan>
<!-- 配置Thymeleaf视图解析器 -->
<bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<property name="order" value="1"/>
<property name="characterEncoding" value="UTF-8"/>
<property name="templateEngine">
<bean class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver">
<bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<!-- 视图前缀 -->
<property name="prefix" value="/WEB-INF/templates/"/>
<!-- 视图后缀 -->
<property name="suffix" value=".html"/>
<property name="templateMode" value="HTML5"/>
<property name="characterEncoding" value="UTF-8" />
</bean>
</property>
</bean>
</property>
</bean>
<mvc:view-controller path="/" view-name="index"></mvc:view-controller>
<mvc:view-controller path="/test_rest" view-name="test_rest"></mvc:view-controller>
<!--开启mvc的注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>