"Welcome To Ashok IT" "Spring Boot and MicroServices" Topic : SpringBootMVC Development Date : 28/11/2024 (Session - 76) _____________________________________________________________________________________________________________________________ * We Will Complete First Spring Boot MVC Application with below concepts 1) Sending Request to Webapplication with out context-path(WebApplicationName) ============================================================================== http://localhost:1234/wishByUser?username=Mahesh http://localhost:1234/wishes 2) Sending Request to Webapplication with context-path ====================================================== 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 #Configuring the context path for spring boot application server.servlet.context-path=/30_SpringBootMVC_App Request the Webpage with below URL =================================== http://localhost:1234/30_SpringBootMVC_App/wishByUser?username=Ashok http://localhost:1234/30_SpringBootMVC_App/wishes 3) Developing the Enquiry Form with 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 4) Developing the Request Processor Method For Handling Form Data. ================================================================== Request Flow ============= (1) (2) (3) (4) Web Browser <----->DispatcherServlet<----------->EnquiryController <------>EnquiryService<------------> EnquiryDao<------>DB Notes ===== (1) WebBrowser will send request through form submission to DispatcherServlet. (2) DispatcherServlet will delegate the request to EnquiryController PostRequestProcessor Method and this method collects the form data into EnquiryDTO object. (3) EnquiryController will communicate with EnquiryService by passing the EnquiryDTO Object as parameter of EnquiryService class method. (4) In the Service Class Method we are doing below things *** Here we are converting the EnquiryDTO Object into Enquiry Entity Object and Viceversa because as Service class will receive the data in DTO Object but we need to pass Enquiry Entity object to DAO layer to persist the Data Contains EnquiryDTO.java <---------------> Form Submitted Data Enquiry.java <---------------> Entity Class used for saving data into DB *** Now Here we need to convert the object from one type(DTO) to another Type(Entity) this is can be achieved with below approaches 1) Manually 2) ModelMapper API *** In our Application we use ModelMapper API to convert the objects from one type to another type in very simple manner with below approach. pom.xml ======= org.modelmapper modelmapper 3.1.1 Application.java ================= * Mark the ModelMapper class as Spring bean with below configuration because we can consider as Singleton and easily can autowire anywhere in application @Bean public ModelMapper getModelMapper() { return new ModelMapper(); } EnquiryServiceImpl.java ======================= @Override public EnquiryDTO createEnquiry(EnquiryDTO enquirydto) { //Converting from EnquiryDTO Object into Entity Class Object Enquiry enquiry = modelMapper.map(enquirydto,Enquiry.class); enquiry.setCreatedDate(new java.util.Date()); //Saving enquiry Information into database Enquiry savedEnquiry = enquiryDao.save(enquiry); //Converting from Entity Class Object into EnquiryDTO class Object EnquiryDTO enqDto = modelMapper.map(savedEnquiry, EnquiryDTO.class); return enqDto; } * We need to following starters for existing Spring Boot MVC Application 1) MySQL Jdbc Driver 2) Spring Data JPA * Develop the following entity,service,dao classes as below com.ashokit.entity | -> Enquiry.java com.ashokit.beans | -> EnquiryDTO.java com.ashokit.dao | -> EnquiryDao.java com.ashokit.service | -> EnquiryService.java | -> EnquiryServiceImpl.java com.ashokit.controller | -> EnquiryController.java Enquiry.java ============ package com.ashokit.Entity; import java.util.Date; import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.Table; import jakarta.persistence.Temporal; import jakarta.persistence.TemporalType; @Table @Entity(name="ashokit_enquires") public class Enquiry { @Id @Column(name="enquiry_id") @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer enquiryId; @Column(name="enquiry_name") private String name; @Column(name="contact_no") private String contactNo; @Column(name="email") private String emailId; @Column(name="course") private String courseName; @Column(name="created_dt") @Temporal(TemporalType.DATE) private Date createdDate; public Enquiry() { } public Enquiry(Integer enquiryId, String name, String contactNo, String emailId, String courseName) { super(); this.enquiryId = enquiryId; this.name = name; this.contactNo = contactNo; this.emailId = emailId; this.courseName = courseName; } public Enquiry(String name, String contactNo, String emailId, String courseName) { super(); this.name = name; this.contactNo = contactNo; this.emailId = emailId; this.courseName = courseName; } public void setEnquiryId(Integer enquiryId) { this.enquiryId = enquiryId; } public Integer getEnquiryId() { return enquiryId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getContactNo() { return contactNo; } public void setContactNo(String contactNo) { this.contactNo = contactNo; } public String getEmailId() { return emailId; } public void setEmailId(String emailId) { this.emailId = emailId; } public String getCourseName() { return courseName; } public void setCourseName(String courseName) { this.courseName = courseName; } public void setCreatedDate(Date createdDate) { this.createdDate = createdDate; } public Date getCreatedDate() { return createdDate; } @Override public String toString() { return "Enquiry [enquiryId=" + enquiryId + ", name=" + name + ", contactNo=" + contactNo + ", emailId=" + emailId + ", courseName=" + courseName + "]"; } } EnquiryService.java =================== package com.ashokit.service; import com.ashokit.beans.EnquiryDTO; public interface EnquiryService { public EnquiryDTO createEnquiry(EnquiryDTO enquiry); } EnquiryServiceImpl.java ======================= package com.ashokit.service; import org.modelmapper.ModelMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.ashokit.Entity.Enquiry; import com.ashokit.beans.EnquiryDTO; import com.ashokit.dao.EnquiryDao; @Service public class EnquiryServiceImpl implements EnquiryService { @Autowired private EnquiryDao enquiryDao; @Autowired private ModelMapper modelMapper; /*@Override public Enquiry createEnquiry(Enquiry enquiry) { //converting from beans(Enquiry) into Entity(Enquiry) com.ashokit.Entity.Enquiry dbEnquiry = modelMapper.map(enquiry, com.ashokit.Entity.Enquiry.class); //saved Enquiry Information com.ashokit.Entity.Enquiry savedEnquiryInfo = enquiryDao.save(dbEnquiry); //converting back from Entity(Enquiry) object to Bean(Enquiry) Enquiry enquiryInfo = modelMapper.map(savedEnquiryInfo, Enquiry.class); return enquiryInfo; }*/ @Override public EnquiryDTO createEnquiry(EnquiryDTO enquirydto) { Enquiry enquiry = modelMapper.map(enquirydto,Enquiry.class); enquiry.setCreatedDate(new java.util.Date()); Enquiry savedEnquiry = enquiryDao.save(enquiry); return modelMapper.map(savedEnquiry, EnquiryDTO.class); } } EnquiryDao.java =============== package com.ashokit.dao; import org.springframework.data.repository.CrudRepository; import com.ashokit.Entity.Enquiry; public interface EnquiryDao extends CrudRepository { } EnquiryDTO.java =============== package com.ashokit.beans; public class EnquiryDTO { private Integer enquiryId; private String name; private String contactNo; private String emailId; private String courseName; public EnquiryDTO() { } public EnquiryDTO(Integer enquiryId, String name, String contactNo, String emailId, String courseName) { super(); this.enquiryId = enquiryId; this.name = name; this.contactNo = contactNo; this.emailId = emailId; this.courseName = courseName; } public EnquiryDTO(String name, String contactNo, String emailId, String courseName) { super(); this.name = name; this.contactNo = contactNo; this.emailId = emailId; this.courseName = courseName; } public void setEnquiryId(Integer enquiryId) { this.enquiryId = enquiryId; } public Integer getEnquiryId() { return enquiryId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getContactNo() { return contactNo; } public void setContactNo(String contactNo) { this.contactNo = contactNo; } public String getEmailId() { return emailId; } public void setEmailId(String emailId) { this.emailId = emailId; } public String getCourseName() { return courseName; } public void setCourseName(String courseName) { this.courseName = courseName; } @Override public String toString() { return "Enquiry [enquiryId=" + enquiryId + ", name=" + name + ", contactNo=" + contactNo + ", emailId=" + emailId + ", courseName=" + courseName + "]"; } } Application.java ================= package com.ashokit; import org.modelmapper.ModelMapper; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Bean public ModelMapper getModelMapper() { return new ModelMapper(); } } EnquiryController.java ====================== package com.ashokit.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.ashokit.beans.EnquiryDTO; import com.ashokit.service.EnquiryService; @Controller @RequestMapping("/enquiry") public class EnquiryController { @Autowired private EnquiryService enquiryService; @GetMapping(value="/") public ModelAndView getHomePage() { ModelAndView mav = new ModelAndView("enquiryDetails"); //Binding the Form with Java Object mav.addObject("enquiryForm", new EnquiryDTO()); return mav; } @PostMapping(value="processEnquiry") public String handleEnquiry(@ModelAttribute("enquiryForm") EnquiryDTO enquirydto, ModelMap map) { //EnquiryDTO contains form data EnquiryDTO enquiry = enquiryService.createEnquiry(enquirydto); map.addAttribute("enquiry", enquiry); return "enquiryInfo"; } } WelcomeController.java ====================== 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; } } 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 #Configuring the context path for spring boot application server.servlet.context-path=/30_SpringBootMVC_App #Datasource Configuration spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/31_springbootms_db spring.datasource.username=root spring.datasource.password=root #Hibernate Configuration spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true enquiryDetails.jsp ================== <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags/form" %> Registration Page

AshokIT Enquiry Form

AngularJS ReactJS Spring Boot Framework Microservices AWS Devops
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}
welcome.jsp =========== <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> Insert title here ${enquiry.name} ${enquiry.contactNo} ${enquiry.emailId} ${enquiry.courseName} pom.xml ======= org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.apache.tomcat tomcat-jasper 10.1.12 jakarta.servlet.jsp.jstl jakarta.servlet.jsp.jstl-api org.glassfish.web jakarta.servlet.jsp.jstl org.springframework.boot spring-boot-starter-data-jpa com.mysql mysql-connector-j runtime org.modelmapper modelmapper 3.1.1 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++