x Java Java 8 JUnit JSON
  • XML
  • JDBC Spring Boot Microservices React Contact Us

    Spring Boot JSP Example

    This example shows how to render a jsp in Spring Boot application

    Step 1) Create pom.xml and add below maven dependency

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>	
     
        
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>	    
    </dependencies>
    

    Step 2) Create WebApplication class

    package com.example.demo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
    
    @SpringBootApplication  
    public class WebApplication extends SpringBootServletInitializer {
    	
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(WebApplication.class);
        }
        
        public static void main( String[] args ) {
        	SpringApplication.run(WebApplication.class, args);  
        }
    
    }        

    Step 3) Create HelloWorldController class which will expose endpoint to return a String which will be displayed by the JSP page

    package com.example.demo;
    
    import java.util.Map;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    
    @Controller
    public class HelloWorldController {
    
        @GetMapping("/")
        public String hello(Map<String, Object> model) {
        	model.put("message", "Hello world JSP example");
            return "helloWorld";
        }
    }        

    Step 4) Create helloWorld.jsp file under src/main/webapp/WEB-INF/jsp folder

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <html>
        <head>
            <title>Spring Boot JSP example</title>
            <link href="css/main.css" rel="stylesheet">
        </head>
    	
        <body>
            <h1>Spring Boot JSP Example</h1>
            <h2>Message: ${message}</h2>
        </body>
    </html>	
            

    Step 5) Create application.properties file under src/main/resources folder

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

    Step 6) Create main.css file under src/main/resources/static/css folder

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

    Maven Build


























    Step 7) Running WebApplication

    Console Output :

    2019-10-19 09:23:33.576  INFO 4432 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
    2019-10-19 09:23:33.620  INFO 4432 --- [main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
    2019-10-19 09:23:33.620  INFO 4432 --- [main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.21]
    2019-10-19 09:23:33.931  INFO 4432 --- [main] org.apache.jasper.servlet.TldScanner     : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
    2019-10-19 09:23:33.938  INFO 4432 --- [main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
    2019-10-19 09:23:34.591  INFO 4432 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
    2019-10-19 09:23:34.595  INFO 4432 --- [main] com.example.demo.WebApplication          : Started WebApplication in 3.447 seconds (JVM running for 3.933)
    2019-10-19 09:23:39.334  INFO 4432 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
    2019-10-19 09:23:39.334  INFO 4432 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
    2019-10-19 09:23:39.343  INFO 4432 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 9 ms
             

    Step 8) Testing HelloWorldApplication

    Open any browser and launch http://localhost:8080. You will see 'Hello World' message displayed in the broswer.


    Maven Build




















    Comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *











    Share This