			 	             "Welcome To Ashok IT"				
			                  "Spring Boot and MicroServices"
                                            Topic  : Spring Boot MVC
                                             Date  : 08/05/2025
                                                (Session - 42)                               
_____________________________________________________________________________________________________________________________
Yesterday Session
=================
Spring MVC
==========
* The Application which can be available through out the globe such type of application called "Web Application".

* Web Application is nothing but collection of WebPages.

* Basically web pages are classified into two types

   1) Static Webpage   >>>>>>>>> The content of webpage will always fixed and common for every user.
                                 Ex: Gmail Login Page etc.,
                                 Ex: Html,Javascript,CSS,JQuery etc.,

   2) Dynamic Webpage  >>>>>>>>> The content of webpage will be changed from one user to another user.
                                 Ex: Gmail Dashboard etc.,

* Inorder to execute the Static webpages (or) static webresource programs we required "Browser"

* Every Web Application will follows the "Client-Server" Architectural Model

                                             Protocal(http)
    Client(Browser) <-----------------------------------------------------------------> Server(Machine)

					     request
    Client(Browser) -------------------------------------------------------------------> Server(Machine)

    					     response
    Client(Browser) <------------------------------------------------------------------- Server(Machine)

* Protocal are nothing but an rules & guideliends between the two parties and we have two types of protocals

   1) Application Protocal : Application -to- Application 

         Example : jdbc:oracle, jdbc:mysql etc.,

   2) Network Protocal : Exchanging the data between machine's through network.

        Example : SMTP,FTP,Telnet,http etc..,

* Inorder to develop the WebApplication In Java Sunmicrosystem provided the initial Technologies i.e.,Servlets & JSP's.

           Servlet >>>>>>>>>>>>>>>>> Defining the Request Processing Logic

                                      1) Collecting (or) Taking inputs from enduser.
 
                                      2) Placing Business logic.

                                      3) Communicating with database through Jdbc Technology.

           JSP(Java Server Pages) >>>>>>>>>>>>>> Recommended for Presenting the Data .


* We can develop the Servlet Program in four ways

    1) By Implementing javax.servlet.Servlet (or) jakarta.servlet.Servlet(I)

    2) By extending javax.servlet.GenericServlet (or) jakarta.servlet.GenericServlet(AC)

    3) By extending javax.servlet.http.HttpServlet (or) jakarta.servlet.http.HttpServlet(AC)

    4) By using @WebServlet Annotation 

* Inorder to execute & Test WebApplication definetly we required "WebServer/Application Server".

    Example: Tomcat,Jetty,Weblogic,JBOSS(Wildfly),WebSphere etc..,

* Typically for every webapplication will have some common requirements such as 

    1) Validations

    2) Exception Handling

    3) Internationalization(I18N).

* Inorder to implement the above common logic for an webapplication using servlet and JSP as programmer need to spend some      time to define the logics and at same time creating and configuring the servlet will be takecare by programmer.

* Inorder to overcome this problems we need to go for "Framework Softwares" developing the webapplications

         1) Struts Framework from Apache Software  >>>>>>>>>>>>> Outdated

         2) Java Server Faces(JSF) from Mozarra    >>>>>>>>>>>>> Outdated

         3) Spring MVC from Spring Source          >>>>>>>>>>>>> Currently some applications using market

              Technical Stack (or) Tech stack

* Every Java Based framework Software will give you some ready made code which are commonly required for an projects which      will reduce the burden for the programmer.

* Every Java Based WebApplication Framework is developed by using one architectural design pattern i.e.MVC Design Pattern.

* MVC Design pattern mainly involves three component's

   1) M  ->  Model   -----------------------> Java Class for communicating with Database 
 
   2) V  ->  View    -----------------------> Presenting the data Ex:Jsp Pages
 
   3) C  -> Controller ---------------------> Servlet Program

* Every Java Based Web Application Framework has given one predefined Controller as ready made class which contains common      code required for Java based webapplication.

         Struts Framework  <--------------------------------> ActionServlet <-------------------> Servlet

         JSF Framework     <--------------------------------> FacesServlet <--------------------> Servlet

         Spring MVC Framework <-----------------------------> DispatcherServlet <---------------> Servlet

Today Session : 
===============
Spring MVC WebApplication Request Flow
======================================
* Please find the Request Flow of Spring MVC in Paint Image.

Steps
=====
1) Sending URL from Browser window as request.

2) Every request of Spring MVC will be trapped by DispatcherServlet as Front Controller.

3) DispatcherServlet will delegates the request to HandlerMapping to find suitable HelperController is existed in application    or not.

4) If HandlerMapping Found the suitable controller in the application then DispatcherServlet will delegates the request to    Helper Controller class.

   If HandlerMapping not found the suitable controller in the application then DispatcherServlet will send back 404 error to    client.

5) DispatcherServlet will be executing the request processing logic in Helper controller class and request processor method
   should return as one Java object i.e.,ModelAndView Object.
 
   ModelAndView is a predefined class in spring mvc library which can hold the data of Model & view Information and
   find below syntax for creating ModelAndView object.

   Syntax-1
   ========
    ModelAndView mav = new ModelAndView();
    //setting view name
    mav.setViewName(String viewName);

    //setting model data
    mav.addObject("Key","value");

   Syntax-2 
   ========
    ModelAndView mav = new ModelAndView(String viewName);

    //setting model data
    mav.addObject("Key","value");

   Syntax-3
   ========
   ModelAndView mav = new ModelAndView(String viewName,String key,String value);

   Ex: ModelAndView mav = new ModelAndView("welcome","wishMessage","Welcome To Ashok IT");

6) After executing the Request Processor method the Dispatcher servlet will receive ModelAndView Object.

7) Onces DispatcherServlet received the ModelAndView Object then will start communicating with ViewResolver component to get    the view page from view folder location based on Logical View Name(LVN).

   ViewResolver is an interface from spring mvc library and one of implementation class is "InternalResourceViewResolver".

   InternalResourceViewResolver class contains two properties i.e.,prefix,suffix

   Prefix is nothing but location of view pages folder in our application

   suffix is nothing but extension of view page either .jsp (or) .html

   This InternalResourceViewResolver class should be configured in spring configuration file as below

      <!--configuring the ViewResolve-->
      <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/views/"></property>
        <property name="suffix" value=".jsp"></property>
      </bean>

8) ViewResolver will return back view page to DispatcherServlet and Will handover to Server.

9) Finally Server will handover to view page as response.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Steps for Developing the First WebApplication using Spring MVC
==============================================================
Step-1 :
=======
* Download and install the ApacheTomcat10 version from below url 
 
    https://tomcat.apache.org/download-90.cgi  >>>> select installer service (32-bit/64-bit Windows Service Installer (pgp,                                                                                sha512))

    During the installation change the tomcat server port no from 8080 to 1234

    During the installation remember the username & password for tomcat manager i.e.,admin/admin.

Step-2 : Adding Tomcat Server into STS IDE
==========================================
* Open the STS IDE and Navigate to "Window" Menu >>>> Again Select "Show View" >>> Again select "Others" and Find "Server"     >> select "Servers" Option and server window will appear.

* Under the Server Section need to Add the Server by clicking on Hyperlink and select "Apache" >>> Find "TomcatV9.0 Server"
  >>> Click on "Next" >> Point the Apache Tomcat Server 10.1 Location >>> Click on "Finish" button.

Step-3 : Creating Project in STS IDE
====================================
 * Open STS IDE and create new Maven project by checking checkbox of skiping the archetype selection

 * Provide below values for artifacts co-ordinates
  
     GroupId : com.ashokit

     ArtifactId :28_SpringMVC_WebApp

     package :com.ashokit

 * Click on  "Finish" Button and then project will be created succesfully in STS IDE.

NOTE
====
 * If the below folders are missing in the Maven Project create the folder as below steps

 * Righ Click on Project folder>>> Select option as "Build Path" >>> Again select "New Source Folder" >>> Provide name as 
   "src/main/java" & "src/main/resource".

Step-4 : Configuring the DispatcherServlet in web.xml
=====================================================
web.xml
=======
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns = "https://jakarta.ee/xml/ns/jakartaee"
         xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation = "https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
         version = "5.0">
  <display-name>AshokITFirstWebApplication</display-name>
      <servlet>
        <servlet-name>ashokit</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>ashokit</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

NOTE
====
* We enabled load on startup with positive value for DispatcherServlet and will create an object during the server startup     time itself.

* We configured the URl pattern as "/" representing will accept any kind of request by DispatcherServlet.

Step-5 : Creating Controller class in Spring MVC
================================================
* Create a Java Class and add the annotation as @Controller will becomes as spring bean class in spring mvc.

WelcomeController.java
======================
package com.ashokit.controller;

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

@Controller
public class WelcomeController {
	
	
	//adding request Processor Method for welcome Message
	@RequestMapping(value = "welcome")
	public ModelAndView getWelcomeMessage() {
		
		ModelAndView mav = new ModelAndView("welcome"); 
		mav.addObject("wishMessage", "Welcome To AshokIT For Spring MVC WebAplication Development");
		
		return mav;
		
	}
}


Step-6 : Spring Configuration File
==================================
* A DispatcherServlet will automaticaly load/read spring configuration file if the following two conditions
  are satisfied.

  1) File name should be "servlet-name-servlet.xml" (spring configuration file(ashokit-servlet.xml))
  2) The file must be placed in "WEB-INF Folder".

* If the above conditions are failed need to configure the extra configuration in web.xml

Example
=======
<servlet>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/web-application-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

ashokit-servlet.xml
===================
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    
     <!--Enabling component scan feature-->
     <context:component-scan base-package="com.ashokit"/>
     
      <!--configuring the ViewResolve-->
      <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/views/"></property>
        <property name="suffix" value=".jsp"></property>
      </bean>
</beans>

Step-7 : Creating the View
==========================
* Right click on "webapp" folder and add the new folder as  "views".

* After creating the above folder then create a new file with name as "welcome.jsp" as below

welcome.jsp
===========
<%@ page isELIgnored="false" %>
<html>
  <body bgcolor="cyan">
     <div>${wishMessage}</div>
  </body>
</html>

Step-8: pom.xml
===============
<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 
						https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.ashokit</groupId>
	<artifactId>28_SpringMVC_WebApplication</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>17</maven.compiler.source>
		<maven.compiler.target>17</maven.compiler.target>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>6.0.11</version>
		</dependency>
		<dependency>
			<groupId>jakarta.servlet</groupId>
			<artifactId>jakarta.servlet-api</artifactId>
			<version>5.0.0</version>
			<scope>provided</scope>
		</dependency>
	</dependencies>
</project>

Step-9 : Run the Application
============================
* Right click on Project and choose "Run As" option >>> again select  "Run As Server".

* Right click on Added server in server section >>>> Select option as "start" and server will start & deployed successfully.

* Onces server got started open your favourite browser and hit request with below url

     http://localhost:8787/33_SpringMVC_App/welcome

OUTPUT
=====
Welcome To AshokIT For Spring MVC WebAplication Development
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++