zoukankan      html  css  js  c++  java
  • springboot render 和 重定向

    一、前提条件

    1、导入thymeleaf

    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring5</artifactId>
    </dependency>

    2、html

    templates文件夹中创建html文件

    <html lang="en"  xmlns:th="http://www.thymeleaf.org">

     3、视图

    @Controller
    public class TestController {
        @GetMapping("/test")
        public String isTest(Model model){
            // 前后端传递数据
            model.addAttribute("msg", "Hello World");
            return "text1";
        }
    }

     二、重定向

    1、通过视图重定向

    / 跳转到 /abc

    @GetMapping("/")
    private String vb(){
        return "redirect:/abc";
    }

    2、通过WebMvcConfigurationSupport重定向

    package com.wt.config;
    
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
    
    @Configuration
    public class WebStaticConfig extends WebMvcConfigurationSupport {
        // 访问静态文件
        @Override
        protected void addResourceHandlers(ResourceHandlerRegistry registry) {
            super.addResourceHandlers(registry);
            registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        }
    
        // 重定向
        @Override
        protected void addViewControllers(ViewControllerRegistry registry) {
            super.addViewControllers(registry);
            // urlPath /  , setViewName -> templates 中html的名字
            registry.addViewController("/").setViewName("test");
        }
    }
  • 相关阅读:
    windows10 ubuntu子系统 WSL文件位置
    cs231n assignment1 KNN
    欧拉计划第五题
    欧拉计划第三题
    梯度下降入门
    Linux交换Esc和Caps
    Python实现bp神经网络识别MNIST数据集
    7-2一元多项式的乘法与加法运算
    Python实现图像直方图均衡化算法
    Python实现图像边缘检测算法
  • 原文地址:https://www.cnblogs.com/wt7018/p/13369105.html
Copyright © 2011-2022 走看看