zoukankan      html  css  js  c++  java
  • springboot项目访问html页面

    1. springboot项目访问html页面

    记录一下在springboot项目中如何访问html页面的配置,免得每次需要的时候又得到处去找,找了又忘

    2. 创建一个基于springboot框架的web应用,不需要其它依赖

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.3.5.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>qinfeng.zheng</groupId>
        <artifactId>springboot-iview</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>springboot-iview</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    

    3. application.properties配置

    # 配置静态资源地址,默认地址:"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/
    spring.resources.static-locations=classpath:/templates/
    # 如果将html页面直接放在templates目录下,可能省略下面这行配置
    spring.mvc.view.prefix=views
    # 必须的,不然报错
    spring.mvc.view.suffix=.html
    server.port=8089
    

    4. 配置文件

    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    /**
     * @Author ZhengQinfeng
     * @Date 2020/11/12 22:04
     * @dec
     */
    @Configuration
    public class WebConfig implements WebMvcConfigurer {
        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
    //        spring.mvc.view.prefix + /index + spring.mvc.view.suffix 构成完整的请求地址
    //        访问项目根目录即是访问 /views/index.html页面
            registry.addViewController("/").setViewName("/index");
        }
    }
    
    

    5. 启动springboot项目即可

  • 相关阅读:
    JS reduce方法的使用
    面试娱录
    sticky置顶功能影响了锚点定位
    postcss-px-to-viewport移动端自适应
    axios请求参数自动拼接到了地址那里
    ping 不通。无法访问目标主机
    JS前后台方法的相互调用
    SQL server2008 无法连接服务器
    Assembly.Load未能加载文件或程序集“”或它的某一个依赖项。系统找不到指定的文件
    JS判断IE和非IE
  • 原文地址:https://www.cnblogs.com/z-qinfeng/p/13966685.html
Copyright © 2011-2022 走看看