SpringMVC中的視圖是View
接口,視圖的作用渲染數據,將模型Model
中的數據展示給用戶
SpringMVC視圖的種類很多,默認有轉發視圖和重定向視圖
當工程引入jstl
的依賴,轉發視圖會自動轉換為JstlView
若使用的視圖技術為Thymeleaf
,在SpringMVC的配置文件中配置了Thymeleaf
的視圖解析器,由此視圖解析器解析之后所得到的是ThymeleafView
ThymeleafView
當控制器方法中所設置的視圖名稱沒有任何前綴時,此時的視圖名稱會被SpringMVC配置文件中所配置的視圖解析器解析,視圖名稱拼接視圖前綴和視圖后綴所得到的最終路徑,會通過轉發的方式實現跳轉
轉發視圖
SpringMVC中默認的轉發視圖是InternalResourceView
SpringMVC中創建轉發視圖的情況:
當控制器方法中所設置的視圖名稱以"forward:"
為前綴時,創建InternalResourceView
視圖,此時的視圖名稱不會被SpringMVC配置文件中所配置的視圖解析器解析,而是會將前綴"forward:"
去掉,剩余部分作為最終路徑通過轉發的方式實現跳轉
index.html
<a th:href="@{/testForward}">測試請求轉發</a>
控制器
@RequestMapping("/testForward")
public String testForward(){
return "forward:/wel";
}
@RequestMapping("/wel")
public String toWelcome() {
return "welcome";
}
主頁超鏈接對/testForward
的請求到達控制器后被轉發給/wel
,注意到地址欄路徑是/testForward
而不是/wel
實現了請求轉發。
重定向視圖
SpringMVC中默認的重定向視圖是RedirectView
當控制器方法中所設置的視圖名稱以"redirect:"
為前綴時,創建RedirectView
視圖,此時的視圖名稱不會被SpringMVC配置文件中所配置的視圖解析器解析,而是會將前綴"redirect:"
去掉,剩余部分作為最終路徑通過重定向的方式實現跳轉
index.html
<a th:href="@{/testRedirect}">測試請求重定向</a>
控制器
@RequestMapping("/testRedirect")
public String testRedirect(){
return "redirect:/wel";
}
@RequestMapping("/wel")
public String toWelcome() {
return "welcome";
}
主頁超鏈接對/testRedirect
的請求到達控制器后被轉發給/wel
,注意到地址欄路徑是/wel
,實現了請求重定向。
視圖控制器view-controller
當控制器方法中,僅僅用來實現頁面跳轉,即只需要設置視圖名稱時,可以將處理器方法使用view-controller
標簽進行表示
<!--
path:設置處理的請求地址
view-name:設置請求地址所對應的視圖名稱
-->
<mvc:view-controller path="" view-name=""></mvc:view-controller>
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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.springmvc.gonghr.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>
</beans>
注意:當SpringMVC中設置任何一個view-controller
時,其他控制器中的請求映射將全部失效,此時需要在SpringMVC的核心配置文件中設置開啟mvc注解驅動的標簽:<mvc:annotation-driven />