Developing Spring Boot Application


Spring Boot

Spring boot is a utility for setting up applications quickly. It helps us to create stand-alone application in the form of JAR with an embeded servlet container. Spring boot simplifies dependencies and automatically configure resources whenever possible.


Read the previous post of developing SpringMVC using maven


Step 1 :

First of All Setup Dynamic Web Project using Maven as described in my previous post Dynamic Web Project using Maven

Step 2 : Create index.jsp file

  • Right Click on src/main/webapp > New > JSP File
  • Enter File name index.jsp
  •  <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
          pageEncoding="ISO-8859-1"%>  
     <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
     <html>  
     <head>  
     <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
     <title>Insert title here</title>  
     </head>  
     <body>  
          <center>  
               <h2>Welcome...</h2>  
               <h3>  
                    <a href="student?studentName=Ram">Click Here</a>  
               </h3>  
          </center>  
     </body>  
     </html>  
    

Step 3 : Configure pom.xml file

     <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
      <modelVersion>4.0.0</modelVersion>  
      <groupId>com.stosh</groupId>  
      <artifactId>MyFirstSpringBootTut</artifactId>  
      <version>0.0.1-SNAPSHOT</version>  
      <packaging>war</packaging>  
      <parent>  
         <groupId>org.springframework.boot</groupId>  
         <artifactId>spring-boot-starter-parent</artifactId>  
         <version>1.2.5.RELEASE</version>  
       </parent>  
       <dependencies>  
         <dependency>  
           <groupId>org.springframework.boot</groupId>  
           <artifactId>spring-boot-starter-web</artifactId>  
         </dependency>  
       </dependencies>  
       <properties>  
         <java.version>1.8</java.version>  
       </properties>  
       <build>  
         <plugins>  
           <plugin>  
             <groupId>org.springframework.boot</groupId>  
             <artifactId>spring-boot-maven-plugin</artifactId>  
           </plugin>  
         </plugins>  
       </build>  
     </project>  
    

Step 4 : Configure Spring

  • Create Java Base Configuration class
  •  package com.stosh.config;  
     import org.springframework.context.annotation.Bean;  
     import org.springframework.context.annotation.ComponentScan;  
     import org.springframework.context.annotation.Configuration;  
     import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;  
     import org.springframework.web.servlet.config.annotation.EnableWebMvc;  
     import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;  
     import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;  
     import org.springframework.web.servlet.view.InternalResourceViewResolver;  
     @EnableWebMvc  
     @Configuration  
     public class AppConfig extends WebMvcConfigurerAdapter{  
          @Override  
       public void addResourceHandlers(ResourceHandlerRegistry registry) {  
         registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");  
       }  
          @Override  
       public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {  
         configurer.enable();  
       }  
          @Bean  
       public InternalResourceViewResolver getInternalResourceViewResolver() {  
         InternalResourceViewResolver resolver = new InternalResourceViewResolver();  
         resolver.setPrefix("/WEB-INF/jsp/");  
         resolver.setSuffix(".jsp");  
         return resolver;  
       }  
     }  
    
  • Create an Application class
  •  package com.stosh;  
     import java.util.Arrays;  
     import org.springframework.boot.SpringApplication;  
     import org.springframework.boot.autoconfigure.SpringBootApplication;  
     import org.springframework.context.ApplicationContext;  
     @SpringBootApplication  
     public class Application {  
       public static void main(String[] args) {  
         ApplicationContext ctx = SpringApplication.run(Application.class, args);  
       }  
     }  
    

Step 5 : Create Spring Controller and view

  • Create "StudentController" under src/main/java as shown below
  •  package com.stosh.controller;  
     import org.springframework.stereotype.Controller;  
     import org.springframework.ui.Model;  
     import org.springframework.web.bind.annotation.RequestMapping;  
     import org.springframework.web.bind.annotation.RequestParam;  
     import org.springframework.web.servlet.ModelAndView;  
     @Controller  
     public class StudentController {  
          String message = "Congratulation ....!";  
          @RequestMapping("/student")  
          public ModelAndView showMessage(  
                    @RequestParam(value = "studentName", required = false, defaultValue = " Your name!!!") String studentName) {  
               ModelAndView mv = new ModelAndView("studentList");  
               mv.addObject("message", message);  
               mv.addObject("studentName", studentName);  
               return mv;  
          }  
     }  
    
  • Create view studentList.jsp under /WEB-INF/jsp/
  •  <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
          pageEncoding="ISO-8859-1"%>  
     <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
     <html>  
     <head>  
     <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
     <title>Insert title here</title>  
     </head>  
     <body>  
          <center>  
               <h2>WelCome to this page</h2>  
               <h2>${message} ${studentName}</h2>  
          </center>  
     </body>  
     </html>  
    

Step 6 :Final Project Structure

finale Project structure
Note: Like our facebook page to get updated of free classes and tutorials.