How should I setup my jsp folder in spring boot?

时间:2019-02-24 03:05:59

标签: spring-boot model-view-controller servlet-3.0

I have downloaded a spring project in spring.io. However, I cannot find webapp folder. However, there are static and templates folders. Can someone teach me how to create WebMvcConfigurer and Servelet programmatically and run it using tomcat? Thank you.

1 个答案:

答案 0 :(得分:0)

That's actually a very good question.

SHORT ANSWER:

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

LONGER ANSWER:

I recently spent some time trying to get Spring Boot working with JSP, and found that I had to adjust several things:

  1. build.gradle (or equivalently, 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. Update the main class and application.properties for .jsp

    Test7Application.java (main class):

     @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);
        }
        ... 
    

    application.properties:

     # Look here for jsp URLs:
     spring.mvc.view.prefix: /WEB-INF/jsp/
     spring.mvc.view.suffix: .jsp
    
  3. Assign controllers routes as needed.

My complete notes are here:

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

I hope that helps...