SpringMVC视图

SpringMVC视图

视图的作用是渲染数据,将模型Model中的数据展示给客户,SpringMVC中视图的种类有很多,默认有转发视图(InternalResourceView)和重定向视图(RedirectView)。
当工程引入jstl的依赖,转发视图会自动跳转jstlView,若使用的视图为Thymeleaf,在SpringMVC的配置文件中配置了Thymeleaf的视图解析器,由此视图解析器解析之后所得到的ThymeleafView。

1、ThymeleafView

当控制器方法中所设置的视图名称没有任何前缀时,此时的视图名称会被SpringMVC配置文件中所配置的视图解析器解析,视图名称将拼接视图前缀和后缀所得到的最终路径会通过转发的方式实现跳转。

test_view.html

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" xmlns:th="http://www.thymeleaf.org">
    <title>Titletitle>
head>
<body>
<a th:href="@{/testThymeleafView}">测试ThymeleafViewa>
body>
html>

ViewController

@Controller
public class ViewController {
    @RequestMapping("/testThymeleafView")
    public String testThymeleafView(){
        return "success";
    }
}

success.html

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

在这里插入图片描述

在这里插入图片描述

2、转发视图

SpringMVC中默认的转发视图是InternalResourceView,SpringMVC中创建视图的情况:
当控制器方法中设置的视图名称以"forward:"为前缀时,创建InternalResourceView视图,此时的视图名称不会被SpringMVC配置文件中所配置的视图解析器解析,而是会将前缀为"forward:"去掉,剩下部分作为最终路径通过转发方式实现跳转。如:“forward:/”,“forward:/employee”

ViewController

@Controller
public class ViewController {
    @RequestMapping("/testThymeleafView")
    public String testThymeleafView(){
        return "success";
    }

    @RequestMapping("/testForward")
    public String testForward(){
        return "forward:/testThymeleafView";
    }
}

test_view.html

<a th:href="@{/testForward}">测试InternalResourceViewa>

在这里插入图片描述

3、重定向视图

SpringMVC中默认的重定型视图是RedirectView,当控制器方法中设置的视图名称以"redirect:"为前缀时,创建RedirectView视图,此时视图名称不会被SpringMVC配置文件中所配置的视图解析器解析,而是会将前缀"redirect:"去掉,剩余部分作为最终路径通过重定向方式实现跳转。如:“redirect:/”,“redirect:employee”
转发和重定向不能访问web-inf下的文件。

ViewController

@RequestMapping("/testRedirect")
public String testRedirect(){
    return "redirect:/testThymeleafView";
}

test_view.html

<a th:href="@{/testRedirect}">测试RedirectViewa>

在这里插入图片描述

4、视图控制器

当控制器方法中,仅仅用来实现页面跳转,即只需要设置视图名称时,可以将处理器方法使用view_controller标签进行表示。

如:在上面的程序中,创建一个首页跳转控制器就是达到页面跳转的,可以使用xml配置的形式完成相关功能。

<mvc:view-controller path="/" view-name="firstPage">mvc:view-controller>

注意:当使用上面的配置文件的时候springMVC的所有Controller都会失效,这时候需要使用标签开启注解驱动

完整springMVC配置文件


<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/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    
    <context:component-scan base-package="com.louis.controller">context:component-scan>
    <bean id="ThymeleafViewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <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="firstPage">mvc:view-controller>
    
    <mvc:annotation-driven>mvc:annotation-driven>
beans>

jsp页面实现SpringMVC视图

环境搭建

web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
  <filter>
    <filter-name>CharacterEncodingFilterfilter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    <init-param>
      <param-name>encodingparam-name>
      <param-value>UTF-8param-value>
    init-param>
    <init-param>
      <param-name>forceResponseEncodingparam-name>
      <param-value>trueparam-value>
    init-param>
  filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilterfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>

  <servlet>
    <servlet-name>springMVCservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    <init-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>classpath:springMVC.xmlparam-value>
    init-param>
    <load-on-startup>1load-on-startup>
  servlet>
  <servlet-mapping>
    <servlet-name>springMVCservlet-name>
    <url-pattern>/url-pattern>
  servlet-mapping>
web-app>

springMVC.xml


<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/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    
    <context:component-scan base-package="com.louis.controller">context:component-scan>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/templates/"/>
        <property name="suffix" value=".jsp"/>
    bean>
beans>

maven坐标

<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-webmvcartifactId>
    <version>5.3.1version>
dependency>
<dependency>
    <groupId>org.thymeleafgroupId>
    <artifactId>thymeleaf-spring5artifactId>
    <version>3.0.12.RELEASEversion>
dependency>
<dependency>
    <groupId>javax.servletgroupId>
    <artifactId>javax.servlet-apiartifactId>
    <version>4.0.1version>
dependency>
<dependency>
    <groupId>jstlgroupId>
    <artifactId>jstlartifactId>
    <version>1.2version>
dependency>

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    首页


首页

success.jsp

success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


成功跳转


JspController

@Controller
public class JspController {
    @RequestMapping("/success")
    public String success(){
        return "success";
    }
}

SpringMVC视图_第1张图片

测试

在这里插入图片描述

在这里插入图片描述

你可能感兴趣的:(springMVC,java,MVC)