1

I have a spring mvc project and want to convert into spring boot. So I changed in project :

POM File:

    <modelVersion>4.0.0</modelVersion>
    <groupId>spring-boot</groupId>
    <artifactId>spring-boot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>    
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <start-class>com.ApplicationStartup</start-class>
    </properties>    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-entitymanager</artifactId>
                </exclusion>
            </exclusions>
        </dependency>    
        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>6.4.0</version>
        </dependency>    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </dependency>    
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>    
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>    
        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcprov-jdk15on</artifactId>
            <version>1.49</version>
            <optional>true</optional>
        </dependency>    
        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcpkix-jdk15on</artifactId>
            <version>1.49</version>
            <optional>true</optional>
        </dependency>    
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>    
        <dependency>
            <groupId>org.apache.santuario</groupId>
            <artifactId>xmlsec</artifactId>
            <version>1.5.1</version>
            <optional>true</optional>
        </dependency>    
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
        </dependency>    
        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.1-901.jdbc4</version>
        </dependency>    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Startup(main) class:

enter image description here

Only these changes, i have done. Using this StartupApplication is starting but application is not open. I want to open a jsp as welcome file.

Project Directory Structure :

enter image description here

So how to call jsp in spring boot based on my code or steps of migration of spring mvc into boot based on my project stucture. Thanks in advance. Using this code, i got output like this : enter image description here

Abhishek Rana
  • 107
  • 2
  • 14
  • I would suggest to change the folder structure to "spring boot recommended" structure – smilyface Dec 26 '18 at 09:01
  • What did you mean about `Using this StartupApplication is starting but application is not open` ? How about opening a browser and type the URL? – VinhNT Dec 26 '18 at 09:09
  • After run, just show output like above but browser is not opening – Abhishek Rana Dec 26 '18 at 09:22
  • spring.mvc.view.prefix: /WEB-INF/jsp/ and spring.mvc.view.suffix: .jsp Have you given these properties in your application yaml / properties file ? – smilyface Dec 26 '18 at 09:33

2 Answers2

3

-This is not a complete/exact answer for your question-

I have done migration from spring to spring-boot for different projects. As per my experience I would suggest to create a simple spring-boot application, and then move your required things to newly created app. So that it will be clean code without any unwanted files/dependencies.

For example, the folder structure you are following is : src/com/..class files here and there is src/main/java which seems empty. Where as in spring boot, your class files will be in src/main/java only. There is default path for each directories like resources, static files, properties, test etc.

You may create a simple spring-boot app (you can download zip file) https://start.spring.io/ here. Please don't forget to add required dependencies in the text box provided. And then run it successfully with the example blah blah whatever they provided.

If you create spring-boot directly from your old project, so many unwanted files and dependencies will be there, at last which you cannot identify whether it is actually required or not. Also default structure would be different and yes, the maintenance would be much tedious task. Also, moving on, you will be keep on getting other errors/issues like this (your jsp issue) if you use the same project.

At first, you may feel it as a tedious task (the method I mentioned above) but it would be easy for you once you started using it.

smilyface
  • 5,021
  • 8
  • 41
  • 57
1

Spring Boot : Developing Web Applications. Features

When you are converting the project form Spring MVC Applcaiotn to Spring BOOT Appication,
you need to follow these steps.

  • Firstly we need to deploy war file into a Server into order to serve the application. Where as Spring Boot come's with an Embed Tomcat server. So, need to create a war file to deploy. It automatically use the embed server and servers the application.
  • Second advantage is no need use XML configuration only uses the Java Configuration. So, no need to use web.xml file we can eliminate it.

For Welcome Page's « It first looks for an index.html file in the configured static content locations. If one is not found, it then looks for an index template. ResourceUrlEncodingFilter that is auto-configured for Thymeleaf and FreeMarker.

For JSP you need to configure jsp template as InternalResourceViewResolver
This ViewResolver allows us to set properties such as prefix or suffix to the view name to generate the final view page URL. src/main/resources/templates | src/main/webapp/WEB-INF

@Configuration
public class JspViewResolverConfig {
    @Bean
    public ViewResolver jspViewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();

        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/pages/");
        viewResolver.setSuffix(".jsp");
        viewResolver.setContentType("text/html");

        return viewResolver;
    }
}
Yash
  • 9,250
  • 2
  • 69
  • 74