【问题标题】:Spring Boot can't find /WEB-INF/classes/index.jspSpring Boot 找不到 /WEB-INF/classes/index.jsp
【发布时间】:2018-08-10 14:26:02
【问题描述】:

我的 Spring Boot + Angular 6 + Gradle 应用程序有问题。 我的应用程序如下所示: 家长:
->后端
->前端
后端使用 Spring Boot,配置如下:

@SpringBootApplication
@EnableBatchProcessing
public class BackendApplication extends SpringBootServletInitializer {

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    return configureApplication(builder);
  }

  public static void main(String[] args) {
    configureApplication(new SpringApplicationBuilder()).run(args);
  }

  private static SpringApplicationBuilder configureApplication(SpringApplicationBuilder builder) {
    return builder.sources(BackendApplication.class).bannerMode(Banner.Mode.OFF);
  }
}  

@Controller
@EnableWebMvc
public class IndexController extends WebMvcConfigurerAdapter {

  @RequestMapping(value = "/", method = RequestMethod.GET)
  public ModelAndView index(HttpServletRequest request, Model model) {
    ModelAndView modelAndView = new ModelAndView("index");
    modelAndView.addObject("apiUrl", resolveBaseApiUrl(request));;
    return modelAndView;
  }

  private String resolveBaseApiUrl(HttpServletRequest request) {
    return request.getRequestURL().toString().replace("https://", "//").replace("http://", "//");
  }
}

@EnableWebMvc()
@Configuration
public class ViewConfiguration extends WebMvcConfigurerAdapter {

  @Bean
  public ViewResolver jspViewResolver() {
    InternalResourceViewResolver bean = new InternalResourceViewResolver();
    bean.setPrefix("/WEB-INF/classes/");
    bean.setSuffix(".jsp");
    return bean;
  }

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/**").addResourceLocations("/WEB-INF/classes/");
  }
}

此外,我还有一些依赖项应该添加以从战争中运行此应用程序。

compile 'javax.servlet:jstl:1.2'
compile group: 'org.apache.tomcat.embed', name: 'tomcat-embed-jasper', version: '9.0.10'

我正在构建我的 Angular 6 前端并使用 gradle sourceSets 将 dist 目录的内容添加到 WAR:

sourceSets {
    main {
        resources {
            srcDirs = ["$webappDir/dist", "$projectDir/src/main/resources"]
        }
    }
}

构建后的WAR文件如下所示(如您所见,有WEB-INF/classes/index.jsp文件): Screenshot of WAR file

你知道为什么我仍然得到 404 吗?

Whitelabel Error Page
There was an unexpected error (type=Not Found, status=404).
/WEB-INF/classes/index.jsp  

【问题讨论】:

    标签: java spring spring-boot tomcat war


    【解决方案1】:

    我自己也在努力解决类似的问题。我认为 Spring Boot 正在classes 内寻找static 目录。 有关详细信息,请参阅this link。对于 Spring Boot 应用程序,您可以将静态内容(包括 index.html)放在以下任何位置:

    • /META-INF/resources/
    • /资源/
    • /静态/
    • /公共/

    在 spring.io 链接的 cmets 部分有很多关于面临类似挑战的人的讨论。我正在努力通过它来尝试正确设置。 https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot

    您的 sourceSets 似乎将 Angular 内容放入 main/resources,但在生成的 WAR 屏幕截图中没有 /resources 目录。也许尝试将 sourceSets 更改为:

    sourceSets {
        main {
            resources {
                srcDirs = ["$webappDir/dist", "$projectDir/src/main/resources/static"]
            }
        }
    }
    

    我认为这将创建静态目录,然后当您点击 localhost:8080/WARname 时 Spring Boot 应该能够找到您的 index.html

    【讨论】:

      【解决方案2】:

      问题:

      问题是你配置的,资源请求路径应该是 在/WEB-INF/classes/ 中搜索,这就是为什么您在此路径上遇到错误/WEB-INF/classes/index.jsp


      解决方案:

      现在,如果你使用 AngularJS 并且它是一个单页应用程序,那么配置应该是这样的:

      @EnableWebMvc()
          @Configuration
          public class ViewConfiguration extends WebMvcConfigurerAdapter {
      
      
              @Override
              public void addResourceHandlers(final ResourceHandlerRegistry registry) {
                  registry.addResourceHandler("/resources/static/js/**")
      
                      .addResourceLocations("/resources/static/js/");
      
                  registry.addResourceHandler("/resources/static/css/**")
      
                      .addResourceLocations("/resources/static/css/");
      
                  registry.addResourceHandler("/resources/static/views/**")
      
                      .addResourceLocations("/resources/static/views/");
      
                  registry.addResourceHandler("/resources/static/**")
      
                      .addResourceLocations("/resources/static/");
      
                  registry.addResourceHandler("/webjars/**")
      
                      .addResourceLocations("/webjars/");
      
                  registry.addResourceHandler("/**")
                      .addResourceLocations("classpath:/static/views/index.html")
                      .setCachePeriod(cachePeriod).resourceChain(true)
                      .addResolver(new PathResourceResolver() {
                          @Override
                          protected Resource getResource(String resourcePath,
                              Resource location) throws IOException {
                              return location.exists() && location.isReadable() ? location :
                                  null;
                          }
                      });
              }
      
              @Bean
              public InternalResourceViewResolver viewResolver() {
                  InternalResourceViewResolver resolver = new InternalResourceViewResolver();
                  resolver.setPrefix("/resources/static/views/");
                  resolver.setSuffix(".html");
                  return resolver;
              }
      
      
          }
      

      你的意思是说我的资源,如 js、css、图像应该在其相应的文件夹中搜索,所有其他路由应该由 index.html 在classpath:/static/views/index.html 提供服务


      • 请注意,您可能应该更改上述代码以匹配您的文件夹结构。我希望你明白整个想法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-03-26
        • 2011-06-11
        • 1970-01-01
        • 1970-01-01
        • 2016-10-02
        • 2023-03-21
        • 2022-10-14
        • 1970-01-01
        相关资源
        最近更新 更多