首页 > 解决方案 > 我应该如何在 Spring Boot 中设置我的 jsp 文件夹?

问题描述

我在 spring.io 中下载了一个 spring 项目。但是,我找不到 webapp 文件夹。但是,有静态和模板文件夹。有人可以教我如何以编程方式创建 WebMvcConfigurer 和 Servelet 并使用 tomcat 运行它吗?谢谢你。

标签: spring-bootmodel-view-controllerservlet-3.0

解决方案


这实际上是一个非常好的问题。

简短的回答:

src/main/resources/META-INF/resources/WEB-INF/jsp

更长的答案:

我最近花了一些时间尝试让 Spring Boot 与 JSP 一起工作,并发现我必须调整几件事:

  1. build.gradle(或等效的 pom.xml):

    dependencies {
       compile('org.springframework.boot:spring-boot-starter-web')
       compile('javax.servlet:jstl')
       compile('javax.servlet:javax.servlet-api')
       compile('org.apache.tomcat.embed:tomcat-embed-jasper')
       // compile('org.springframework.boot:spring-boot-starter-thymeleaf')  // DISABLE THYMELEAF
       compile('org.webjars:bootstrap:4.1.0')
    ...
    
  2. 更新 .jsp 的主类和 application.properties

    Test7Application.java(主类):

     @SpringBootApplication
     public class Test7Application extends SpringBootServletInitializer {
        ... 
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(Test7Application.class);
        }
        public static void main(String[] args) {
            SpringApplication.run(Test7Application.class, args);
        }
        ... 
    

    应用程序属性:

     # Look here for jsp URLs:
     spring.mvc.view.prefix: /WEB-INF/jsp/
     spring.mvc.view.suffix: .jsp
    
  3. 根据需要分配控制器路由。

我的完整笔记在这里:

https://github.com/paulsm4/HelloSpringBoot/tree/master/test7

我希望这会有所帮助...


推荐阅读