开发者社区> 问答> 正文

springboot如何直接读取webapp下页面?:报错

公司改用springboot的时候,将页面相关的文件都放在了src/main/webapp下,我直接通过main方式启动的时候,无法读取到src/mian/webapp下文件,但是通过spring-boot:run方法又可以读取到src/mian/webapp下文件,这是什么原因?

要如何才能让main方式可以调用到src/mian/webapp下文件,因为我现在用spring-boot:run方法启动的时候无法debug

展开
收起
kun坤 2020-06-07 17:05:42 1276 0
1 条回答
写回答
取消 提交回答
  • 试试下面的步骤:

    1.添加spring-boot-starter-web依赖:

    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>1.5.4.RELEASE</version>
    </dependency>
    

    2.在Application类中添加 @EnableWebMvc 注解。

    加上@EnableWebMvc表示忽略SpringBoot默认的/static目录,使用src/main/webapp目录作为静态资源目录。此外,你可能还需要重新制定静态资源的处理方式,就像这样:

    @SpringBootApplication
    @EnableWebMvc
    public class ServerApplication extends WebMvcConfigurerAdapter {
    	public static void main(String[] args) {
    		SpringApplication.run(ServerApplication.class, args);
    	}
    
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            super.addResourceHandlers(registry);
    
            registry.addResourceHandler("/**").addResourceLocations("/");
        }
    }

     

     

     

     

    ######

    楼主解决了吗,正碰都这个问题,半天没搞定

    ######在楼下发了解决方案,可以看下######

    在网上找到的解决方案:pom.xml中添加以下内容,意思就是编译的时候把webapp文件放到resource下

    <resources>
       <resource>
          <directory>src/main/webapp</directory>
          <!--注意此次必须要放在此目录下才能被访问到 -->
          <targetPath>META-INF/resources</targetPath>
          <includes>
             <include>**/**</include>
          </includes>
       </resource>
       <resource>
          <directory>src/main/resources</directory>
          <filtering>true</filtering>
          <includes>
             <include>**/*</include>
          </includes>
       </resource>
    </resources>
    ######谢谢!有用
    2020-06-07 17:05:46
    赞同 展开评论 打赏
问答分类:
问答地址:
相关产品:
问答排行榜
最热
最新

相关电子书

更多
使用Spring.Initializr定制工程脚手架 立即下载
陈曦:使用Spring.Initializr定制工程脚手架 立即下载
Java Spring Boot开发实战系列课程【第15讲】:Spring Boot 2.0 API与Spring REST Docs实战 立即下载

相关实验场景

更多