1. 程式人生 > 其它 >Spring MVC-檢視

Spring MVC-檢視

目錄

概括


Spring MVC定義了viewResolver和View介面,在瀏覽器中呈現模型中的資料。Spring MVC檢視預設有轉發檢視(InternalResourceView)和重定向檢視(RedirectView)

環境

IDE:idea 2020.3
構建工具:maven-3.8.1
伺服器:tomcat9
Spring版本:5.3.1

<dependencies>
    <!-- SpringMVC -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.3.1</version>
    </dependency>

    <!-- 日誌 -->
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.2.3</version>
    </dependency>

    <!-- ServletAPI -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>

    <!-- Spring5和Thymeleaf整合包 -->
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring5</artifactId>
        <version>3.0.12.RELEASE</version>
    </dependency>
</dependencies>
<?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"
       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">

<!--   springMVC.xml -->
    <!-- 自動掃描包 -->
    <context:component-scan base-package="com.fly.controller"/>

    <!-- 配置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>

</beans>
<!--  web.xml  -->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceResponseEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springMVC.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

ThymeleafView

當控制器方法的檢視名稱沒有字首時,檢視名稱會被檢視解析器解析,通過檢視解析器的字首、字尾得到路徑後以轉發的形式跳轉

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>首頁</title>
</head>
<body>
<!--index.html-->
<!--index.html-->
<a th:href="@{/testThymeleafView}">testThymeleafView</a><br/>
</body>
</html>
package com.fly.mvc.controller;

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

/**
 * @author 26414
 */
@Controller
public class ViewController {

 @RequestMapping("/")
  public String index() {
    return "index";
  }

  @RequestMapping("/testThymeleafView")
  public String testThymeleafView() {
    return "view";
  }

}

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>view</title>
</head>
<body>
<!--view.html-->
<h2>view</h2>
</body>
</html>

在testThymeleafView方法上打斷點,以debug模式啟動伺服器

InternalResourceView

SpringMVC中預設的轉發檢視是InternalResourceView,當控制器方法中所設定的檢視名稱以"forward:"為字首時,建立InternalResourceView檢視。

  /**
   *ViewController
   */
  @RequestMapping("/testInternalResourceView")
  public String testInternalResourceView() {
    return "forward:/testThymeleafView";
  }
<!--index.html-->
<a th:href="@{/testInternalResourceView}">testInternalResourceView</a><br/>

在方法testInternalResourceView上打斷點,以debug模式啟動伺服器

RedirectView

SpringMVC中預設的重定向檢視是RedirectView,當控制器方法中所設定的檢視名稱以"redirect:"為字首時,建立RedirectView檢視。

  /**
   *ViewController
   */
  @RequestMapping("/testRedirect")
  public String testRedirect() {
    return "redirect:/testThymeleafView";
  }
<!--index.html-->
<a th:href="@{/testRedirect}">testRedirect</a><br/>

在方法testRedirect上打斷點,以debug模式啟動伺服器

view-controller

如果一個控制器方法僅僅用來實現頁面跳轉,可以將處理器方法使用view-controller標籤進行表示,當SpringMVC中設定任何一個view-controller時,其他控制器中的請求對映將全部失效,此時需要在SpringMVC的核心配置檔案中設定開啟mvc註解驅動的標籤

<!--  springMVC.xml  -->
    <mvc:view-controller path="/" view-name="index"/>
<!--  開啟mvc註解驅動  -->
    <mvc:annotation-driven />