spring - IntelliJ 中的 Gradle Spring Boot 应用程序无法识别 WEB-INF 文件夹中的 Web 资源

标签 spring spring-boot spring-mvc gradle intellij-idea

这是一个源 self 之前问题的新问题。我正在编写一个 Spring Boot 应用程序,它使用 .jsp 作为 mvc View 。我的文件夹结构是:

enter image description here

我的application.properties如下:

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
spring.mvc.view.prefix= /WEB-INF/view/
spring.mvc.view.suffix= .jsp

最后我的HomeController.java如下:

@Controller
@Slf4j
public class HomeController {

    @RequestMapping(value="/", method= RequestMethod.GET)
    public String showPage()
    {
        return "main-menu";
    }
}

程序应该在我的主页 localhost:8080 上呈现 main-menu.jsp 文件,但我收到了错误。这是错误的堆栈跟踪:

2021-06-28 20:40:12.243 DEBUG 27388 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : GET "/", parameters={}
2021-06-28 20:40:12.245 DEBUG 27388 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.example.springdemo.mvc.HomeController#showPage()
2021-06-28 20:40:12.257 DEBUG 27388 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, application/signed-exchange;v=b3;q=0.9, */*;q=0.8]
2021-06-28 20:40:12.257 DEBUG 27388 --- [nio-8080-exec-1] o.s.w.servlet.view.InternalResourceView  : View name 'main-menu', model {}
2021-06-28 20:40:12.258 DEBUG 27388 --- [nio-8080-exec-1] o.s.w.servlet.view.InternalResourceView  : Forwarding to [/WEB-INF/view/main-menu.jsp]
2021-06-28 20:40:12.261 DEBUG 27388 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : "FORWARD" dispatch for GET "/WEB-INF/view/main-menu.jsp", parameters={}
2021-06-28 20:40:12.263 DEBUG 27388 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler [Classpath [META-INF/resources/], Classpath [resources/], Classpath [static/], Classpath [public/], ServletContext [/]]
2021-06-28 20:40:12.263  WARN 27388 --- [nio-8080-exec-1] o.s.w.s.r.ResourceHttpRequestHandler     : Path with "WEB-INF" or "META-INF": [WEB-INF/view/main-menu.jsp]
2021-06-28 20:40:12.263 DEBUG 27388 --- [nio-8080-exec-1] o.s.w.s.r.ResourceHttpRequestHandler     : Resource not found
2021-06-28 20:40:12.263 DEBUG 27388 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Exiting from "FORWARD" dispatch, status 404
2021-06-28 20:40:12.264 DEBUG 27388 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed 404 NOT_FOUND
2021-06-28 20:40:12.265 DEBUG 27388 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : "ERROR" dispatch for GET "/error", parameters={}
2021-06-28 20:40:12.266 DEBUG 27388 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
2021-06-28 20:40:12.278 DEBUG 27388 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, text/html;q=0.8]
2021-06-28 20:40:12.281 DEBUG 27388 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Exiting from "ERROR" dispatch, status 404

据我所知,这似乎是 IntelliJ 设置方式或 Gradle 设置方式的问题。无论哪种方式,资源目录或文件都无法被识别。

以下是我做过/尝试过的一些事情:

  1. 在项目结构中,我添加了文件夹 WEB-INF 作为模块中的 Web 资源目录。
  2. 在“设置 >>> 编译器”中,我将 !?.jsp 添加到资源模式
  3. 最后,在 gradle.build 中,我添加了 sourceSet(这导致了错误,我将其删除)。

感谢您为我提供的任何帮助。

编辑:

我已按如下方式更新了我的项目结构。

enter image description here

我还在我的 gradle.build 中添加了 apply plugin: 'war' 并将该文件夹添加到我的项目构面中。

enter image description here

我仍然遇到同样的问题,找不到资源。

最佳答案

我在同事的帮助下解决了这个问题。我必须对 SpringMvcDemoApplication.java 文件和 gradle.build 进行一些更改。以下是更改。

SpringMvcDemoApplication(为InternalResourceViewResolver添加bean):

@SpringBootApplication(exclude=DataSourceAutoConfiguration.class)
public class SpringMvcDemoApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(SpringMvcDemoApplication.class);
    }

    @Bean
    public ViewResolver internalResourceViewResolver() {
        InternalResourceViewResolver bean = new InternalResourceViewResolver();
        bean.setViewClass(JstlView.class);
        bean.setPrefix("/WEB-INF/view/");
        bean.setSuffix(".jsp");
        return bean;
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringMvcDemoApplication.class, args);
    }

}

HomeController.java(已删除@Slf4j)

@Controller
public class HomeController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String showPage()
    {
        return "main-menu";
    }
}

gradle.build(添加了tomcat和jSTL依赖项):

plugins {
    id 'org.springframework.boot' version '2.5.1'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'com.example.springdemo'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}
apply plugin: 'war'

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.projectlombok:lombok:1.18.18'
    implementation group: 'javax.servlet', name: 'jstl', version: '1.2' //addee
    implementation group: 'org.apache.tomcat.embed', name: 'tomcat-embed-jasper'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}


test {
    useJUnitPlatform()
}

现在一切正常。

关于spring - IntelliJ 中的 Gradle Spring Boot 应用程序无法识别 WEB-INF 文件夹中的 Web 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68172452/

相关文章:

java - RestTemplate 是否同步 REST 调用?

Spring MessageBuilder 与 Spring Integration MessageBuilder

java - Spring MessageSource 回退到显式语言环境

java - 在Thymeleaf中,我如何选择第:selected value for an HTML selector based on the current model object?

Spring JPA 定义了多个 EntityManagerFactory 但存储库仍然只访问一个数据库

java - Spring AOP : is there a way to make @target work for indirect annotations?

java - Spring Boot war 在 Elastic Beanstalk 中启动时失败,但在本地运行时正确启动

spring-boot - 无法加载 TestContextBootstrapper - 非 Web 应用程序的 spring-boot 集成测试错误

Java - 如何结合 Validation 和 AOP 注释并在 Spring Controller 中使用它?

java - 我真的可以将我的 web 应用程序的所有模板都移动到 javascript 吗?