spring boot + Spring MVC + Maven + JSP example

Spring boot jsp example

Directory Structure

pom.xml file

Modify auto created POM file. We need to add parent in pom to make it a spring boot application. After that add spring-boot-starter-web dependency and java version.


  4.0.0

  codesjava
  SpringBoot02
  0.0.1-SNAPSHOT
  jar

  SpringBoot02
  http://maven.apache.org

  
    1.8  
  
  
    
    org.springframework.boot  
    spring-boot-starter-parent  
    1.4.2.RELEASE  
   

  
       
	
		org.springframework.boot
		spring-boot-starter-web
	

	
	
		org.springframework.boot
		spring-boot-starter-tomcat
		provided
	

	
	
		javax.servlet
		jstl
	

	
	
		org.apache.tomcat.embed
		tomcat-embed-jasper
		provided
	

	
	
		org.eclipse.jdt.core.compiler
		ecj
		4.6.1
		provided
	
  
    
      junit
      junit
      3.8.1
      test
    
  
  
  
		
			
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	

Now, right click on project -> click on Maven -> click on update project -> Popup window will open -> click Ok to update the project.
It is a initializer class which runs a SpringApplication.
App.java

package com.w3schools360;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

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

HelloWorldController.java

package com.w3schools360;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloWorldController {
	private String message = "Hello World";

	@RequestMapping("/")
	public String welcome(Map<String, Object> model) {
		model.put("message", this.message);
		return "helloWorld";
	}
}

helloWorld.jsp


<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>



Insert title here


	

Spring Boot JSP Hello World Example

Message: ${message}

application.properties

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

Hit http://localhost:8080/ in the browser, following output will come.
Spring security