美文网首页
关于 WebRoot 配置

关于 WebRoot 配置

作者: 9995857 | 来源:发表于2025-04-14 22:15 被阅读0次

可以考虑将 webroot 目录放到 项目jar的同级

@Configuration
@Slf4j
public class WebConfig implements WebMvcConfigurer {
   

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        ApplicationHome home = new ApplicationHome(getClass());
        String jarParentPath = home.getSource().getParentFile().getAbsolutePath();

        String webrootPath = jarParentPath + "/webroot/";

        log.info("webrootPath:{}", webrootPath);
        registry.addResourceHandler("/**")
                .addResourceLocations("file:" + webrootPath);
    }

//设定 默认访问 index.html
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/index.html");
    }
}

POM 配置

需要将 webroot 在编译的时候 ,放到 target中 方便在调试的时候 可以访问到

 <!-- 其他插件配置保持不变 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.3.1</version>
                <executions>
                    <execution>
                        <id>copy-webroot</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/webroot</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>./webroot</directory>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

相关文章

网友评论

      本文标题:关于 WebRoot 配置

      本文链接:https://www.haomeiwen.com/subject/fxadbjtx.html