"Welcome To Ashok IT" "Spring Boot and MicroServices" Topic : Spring Boot MVC - Introduction Date : 15/05/2025 (Session - 47) _____________________________________________________________________________________________________________________________ Spring Boot MVC =============== * Spring Boot MVC is umbrella project of Spring Boot. * Spring Boot MVC We can develop either WebApplications (or) Distributed Applications. * Spring Boot automatically configures spring application based on starter selection(DependencyManagement,SpringBeans etc.,) * Typically in spring MVC driven application lot of configuration such as DispatcherServlet,View Resolvers,datasource,transaction management etc., * Spring Boot MVC Module will auto configure the DispatcherServlet if spring mvc jar is present in the classpath. * It will auto configures the Jackson library if the jackson jar present in the classpath. * It will auto configures the data source if Hibernate jar present in the classpath. * Spring Boot Provides the "spring-boot-starter-web" for developing the webapplication using Spring MVC. * Spring Boot autoconfiguration registers and configures the DispatcherServlet automatically. There fore, we don't need to register the DispatcherServlet manually. * While working with Spring Boot MVC we no need to install the Server for testing webapplication because spring boot mvc supports Embedded servers(Tomcat,Jetty) (or) external servers. Project Creation ================= 1) Switch to STS IDE and Create new project with below parameters Packaging Mode :: JAR(Testing through Embedded Servers), WAR(Testing through Embedded Servers,External servers) :: WAR (Recommended) 2) Starter Selection : We need to select the web starter by searching word as "web" then you will find option called "Spring web" check the checbox and click on "Finish" button then Project will be created 3) Understanding Folder Structure src/main/resource | ------ static -------------> For placing static resources(images,audios,videos,css,Javascript files etc.,) | ------ templates -----------> For placing Thymeleaf pages. | ------ application.properties ------------> application level configuration entries 4) Observed the Jar files under "mavend dependencies". i.e.,spring mvc,jackson,embedded tomcat etc., 5) Added some folders under standard folder(webapp) webapp >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Standard folder |- >>>>>>>>>>>>>>>> WEB-INF >>>>>>>>>>>>>> standard folder |- >>>>>>>>>>>>>>>> views >>>>>>>>>>>>>>>>> Non-Standard folder. 6) Execute the boot Application by selecting Application.java Run as >>>> Spring Boot Application Error ===== *************************** APPLICATION FAILED TO START *************************** Description: Web server failed to start. Port 8080 was already in use. 7) We can have option to change the port no for embedded server in application.properties application.properties ======================= server.port=1234 8) After adding port number try to run the Spring Boot Application again. Spring boot First Application ============================= 1) Created the Sample Controller by adding two Request Processor Methods in Spring Boot MVC Application package com.ashokit.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; @Controller public class WelcomeController { @RequestMapping(value="/wishes", method=RequestMethod.GET) public String generateWelcomeMessage(Model model) { model.addAttribute("Message", "Welcome To Ashok IT For Spring Boot MVC Development....."); return "welcome"; } @GetMapping(value = "/wishByUser") public ModelAndView generateWishesByUser(@RequestParam("username") String username) { ModelAndView mav = new ModelAndView("welcome"); mav.addObject("wishMessage",String.format("Welcome To Ashok IT For Spring Boot MVC Development ...%s", username)); return mav; } } 2) Created the welcome.jsp under the views folder welcome.jsp ========== <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix = "c" uri="http://java.sun.com/jsp/jstl/core" %> welcome.jsp
${Message} ${wishMessage}
3) Add the JSTL Dependencies & tomcat-jasper in pom.xml pom.xml ======= org.apache.tomcat tomcat-jasper 10.1.12 jakarta.servlet.jsp.jstl jakarta.servlet.jsp.jstl-api org.glassfish.web jakarta.servlet.jsp.jstl 4) Added the prefix and suffix in application.properties application.properties ====================== #changing the port no server.port=1234 # configure the prefix,suffix of view resolver spring.mvc.view.prefix=/views/ spring.mvc.view.suffix=.jsp 5) Open Application.java and right click on it choose Run as >>>> Spring Boot Application after application is started Open our favourite webbrowser and hit request using following url : http://localhost:1234/wishes In the above Request URL of Spring boot mvc application development context path(webapplication name) is not appended into request url. If we want to send request through context path(webapplication name) we need to add one entry in application.properties to specify the context path. server.servlet.context-path=/SpringBootMVC Before adding context path to our application by default context path is empty('') in such cases we need to use above request url only. After ending contextpath in application.properties make sure we need to request through context path only. Request URL : http://localhost:1234/SpringBootMvc/wishes BootStrap library ================= * BootStrap is Frontend framework which provides predefined the css class text-primary >>>>>>>>>>>>>> Displays text as blue color text-danger >>>>>>>>>>>>>> Displays text as red color text-success >>>>>>>>>>>>>> Displays text as Greencolor container >>>>>>>>>>>>>> Grouping the several html component into single block and all these Html elements are organized into center of webpage. * As programmer if we need to utilize the bootstrap library we just need to include bootstrap.css file into our webpage. bootstrap.css >>>>>>>>>> Original File bootstrap-min.css >>>>>>>>>> Minified File(Compressed Version) * We can link the css file into our JSP page by using html tag called