Spring Boot - where to place the jsp files

前端 未结 8 2141
无人及你
无人及你 2020-12-20 15:55

I am trying to develop a new Spring boot application using MVC as a first step to move my existing Spring MVC application to Spring boot.

However, I am facing an is

相关标签:
8条回答
  • 2020-12-20 16:23

    I am facing The Same issue and I remove this problem by following changes:

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

    No need to make ServletInitializer new class.

    0 讨论(0)
  • 2020-12-20 16:25

    The issue was with the version of jar spring-boot-starter-parent. For some reason this doesn't work with the version 1.5.3 RELEASE. It works until version 1.5.2 RELEASE.

    I have updated the pom.xml's parent tag as below:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
    0 讨论(0)
  • 2020-12-20 16:27

    Why do have two separate java classes(MyFirstAppApplication,ServletInitializer) extending SpringBootServletInitializer class ?

    Remove ServletInitializer.java & move configure method from ServletInitializer to MyFirstAppApplication.

    0 讨论(0)
  • 2020-12-20 16:28

    There is a dependency to include as Spring boot doesn't know how to translate JSP to Servlet. So,

    1. Check the version of your tomcat-embed-core-.jar.

    2. Go to that corresponding version release on https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper

    3. Copy the dependency, it will look like-

    <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jasper</artifactId>
            <version>9.0.37</version> 
    </dependency> 
    

    here version needs to be updated as per your tomcat-embed-core jar version

    1. Update Maven Project so that it can download the jar from repo.

    Now you are good to go with your project.

    thanKs.

    0 讨论(0)
  • 2020-12-20 16:30

    I have gone exactly the same path. Upgrading a jsp, xml-based spring application to spring boot 2. There are few things you need to consider when migrating:

    First remove @EnableWebMvc.

    Second you need @ComponentScan on top of MyFirstAppApplication class.

    Try to read this article, it helped me alot https://htr3n.github.io/2018/12/jsp-spring-boot/

    Third you also need this dependeny together with embed-jasper:

    <dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-core</artifactId>
    <version>9.0.22</version>
    </dependency>
    

    Last but not least this is a shortcut for creating a view handler in java

    spring.mvc.view.prefix=/WEB-INF/jsp/
    spring.mvc.view.suffix:.jsp
    

    and as far as I know it only works in spring boot 2. You can have Java implementation instead and debug it to see if it ever hit that.

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");       
        registry.viewResolver(resolver);
    }
    

    Because in my case for example I didn't even need a specific or default resolver. I had different resolver for each Controller that I had to define in xml/bean and inject in each class.

    0 讨论(0)
  • 2020-12-20 16:35

    I have created a demo project which is rendering jsp

    Git URL : https://github.com/rksharma1401/spring-boot-war

    take checkout then mvn package java -jar target\simple-web-app-tomcat-0.0.1-SNAPSHOT.war URL : http://localhost:8081/w

    0 讨论(0)
提交回复
热议问题