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项目即可

  • 相关阅读:
    SpringBoot_10_打成jar包后使用外部配置文件中的配置来启动工程
    SpringBoot_09_使用jar包中配置的Bean(starter配置)
    猪齿鱼_03_领域模型
    Git_学习_11_Git rebase合并提交信息
    猪齿鱼_02_微服务组件间联系
    猪齿鱼_01_环境搭建(三)_整合业务服务
    猪齿鱼_01_环境搭建(二)_微服务支撑组件部署(Docker形式)
    【BZOJ】3996: [TJOI2015]线性代数
    【BZOJ】3994: [SDOI2015]约数个数和
    【BZOJ】3993: [SDOI2015]星际战争
  • 原文地址:https://www.cnblogs.com/z-qinfeng/p/13966685.html
Copyright © 2011-2022 走看看