Welcome to Spring Boot (With Embedded Tomcat Features)

Welcome to Spring Boot (With Embedded Tomcat Features)
Welcome to Spring Boot (With Embedded Tomcat Features)
Spring Boot, Maven, and Tomcat can form a powerful alliance. This sample web app shows the interplay between them and how you can easily configure them for your needs.

In this post, we will be developing a sample Spring MVC app with embedded Tomcat features provided by Spring Boot using Maven. The source code can be downloaded right here .

Environment Setup
Maven Dependencies

org.springframework.boot spring-boot-starter-parent 1.3.3.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat javax.servlet jstl org.apache.tomcat.embed tomcat-embed-jasper

Spring Bean Configuration

SpringBootServletInitializer enables the process used in Servlet 3.0 using web.xml EnableAutoConfiguration . This will help Spring Boot to automatically identify how to configure Spring, based on the JAR dependencies that we have added. Since spring-boot-starter-web added Tomcat and Spring MVC, the auto-configuration will assume that we are developing a web application and setup Spring accordingly.

@Configuration @ComponentScan(basePackages = "com.developerstack") @Import({BeanConfig.class, WebConfig.class}) @EnableAutoConfiguration public class Application extends SpringBootServletInitializer { private static Class applicationClass = Application.class; public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(applicationClass); } }

The following configuration is responsible for initializing a Spring-based web application. We have implemented WebApplicationInitializer.java to configure the ServletContext programmatically.

public class ApplicationInitializer implements WebApplicationInitializer { public void onStartup(ServletContext container) throws ServletException { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.setServletContext(container); ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(ctx)); servlet.setLoadOnStartup(1); servlet.addMapping("/"); } }

By default, Spring Boot will serve static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath or from the root of the ServletContext. But here we, have defined out custom folder structure for static contents, hence it is required to tell Spring boot about how to render static content.

@Configuration public class WebConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(final ResourceHandlerRegistry registry) { registry.addResourceHandler("/*.js/**").addResourceLocations("/ui/static/"); registry.addResourceHandler("/*.css/**").addResourceLocations("/ui/static/"); } }

Now let's define our controller that will serve the response to the client. Here we have hardcoded some user details.

@Controller public class DashboardController { @RequestMapping(value = "/dashboard", method = RequestMethod.GET) public ModelAndView dashboard() { ModelAndView model = new ModelAndView(); model.addObject("users", getUsers()); model.setViewName("dashboard"); return model; } private List getUsers() { User user = new User(); user.setEmail("johndoe123@gmail.com"); user.setName("John Doe"); user.setAddress("Bangalore, Karnataka"); User user1 = new User(); user1.setEmail("amitsingh@yahoo.com"); user1.setName("Amit Singh"); user1.setAddress("Chennai, Tamilnadu"); User user2 = new User(); user2.setEmail("bipulkumar@gmail.com"); user2.setName("Bipul Kumar"); user2.setAddress("Bangalore, Karnataka"); User user3 = new User(); user3.setEmail("prakashranjan@gmail.com"); user3.setName("Prakash Ranjan"); user3.setAddress("Chennai, Tamilnadu"); return Arrays.asList(user, user1, user2, user3); } }

Client Side

At the client side, there will be a traditional JSP page, which will show the model object using JSTL.

Run Application

There are two ways with which we can run this application.