"Welcome To Ashok IT" "Spring Boot and MicroServices" Topic : Spring MVC - Path Variable Date : 23/11/2024 (Session - 73) _____________________________________________________________________________________________________________________________ Yesterday Session ================= * We have seen how to implement Service & DAO Components of Enquires Application in Spring MVC. * We discussed about Double Posting (or) Double Form Submission and Working Post-Redirect-Get Design Pattern. * In order to work with JSTL with respective to Jarkata Implementation with help of below dependencies pom.xml ======= org.glassfish.web jakarta.servlet.jsp.jstl 2.0.0 taglibs standard 1.1.2 Today Session ============= Understanding @PathVariable Annotation ======================================= * Some times in webapplication we need to extract the values from URL. http://localhost:8787/35_SpringMVC_FormTags/employees?employeeId=1254 ------------> Request Parameters To Read the request parameters in Spring MVC we need to use annotation @RequestParam("ParameterName") http://localhost:8787/35_SpringMVC_FormTags/employees/1254 -----------------> Path Variable To Read the employeeId from URL bar in Spring MVC given as annotation @PathVariable * Find the below code in the controller method @RequestMapping(value="editEnquiry/{enquiryId}") public ModelAndView editEnquiryDetails(@PathVariable("enquiryId") Integer enquiryId){ //Implementing the DAO logic for getting enquiry information based on enquiryId; } * In Last Application every Request Processor annotated with @RequestMapping Annotation which is an generic annotation to work with Post/Get Request using method attribute of this annotation. * From Spring 4.X Version onwards we got below annotations to support for HttpRequest Methods Get Request >>>>>>>>>>>>>>>>>> @GetMapping = @RequestMapping(value="urlpattern",method=RequestMethod.GET) Post Request >>>>>>>>>>>>>>>>>> @PostMapping = @RequestMapping(value="urlpattern",method=RequestMethod.POST) Put Request >>>>>>>>>>>>>>>>>> @PutMapping Patch Request >>>>>>>>>>>>>>>>> @PatchMapping Delete Request>>>>>>>>>>>>>>>>> @DeleteMapping * In Web Application Development we always using two kinds of Requests only 1) Get Request >>>>>>>>>> Recommended for getting data from server sometimes we can post data to server with limited data and always data will be visible in browser url bar. 2) Post Request >>>>>>>>> Recommended for posting data to server and data of the form will not visible on browser url bar. * In Distributed Application Development nothing but Rest API Development we can use different Http Requests i.e.,GET,POST, PUT,PATCH,DELETE. Problems with Spring MVC WebApplication Development =================================================== 1) DispatcherServlet need to be configured in web.xml and make sure need to enable to load on start up for DispatcherServlet. 2) If DispatcherServlet need to read the Spring configuration file we need to following below two condition i) The spring configuration file name should be -servlet.xml. ii) The location of Spring configuration file always should be under "WEB-INF" folder. 3) We need to do lot of configuration in spring configuration file i) Enabling the component scan feature in spring to recognize the spring beans automatically from project folder. ii) Datasource configuration for database communication from our webapplication iii) configuring the ViewResolver(I) as spring bean for locating view pages for our webapplication - InternalResourceViewResolver(IC) >>>>>>>>> prefix (Location of views pages in webappication) suffix (Extension of view page to be picked by DispatcherServlet) iv) If our webapplication utilizing the centralized classes from Spring Jdbc (or) Spring ORM we need to configure such classes also to be spring beans. Ex: JdbcTemplate,NamedParameterJdbcTemplate,HibernateTemplate,JpaTemplate etc., v) If your working transaction management in spring we required configuration to enabled the Particular TransactionManager in our application. Transaction Management >>>>>>>>>>>>>> Do Everthing Nothing principal 4) We need to add required dependencies for our webapplication in pom.xml file. 5) Inorder to test the webapplication definetly we need to install atleast one Java based Web Server/Application Server and after completing we need to configure that server into our IDE. We will do deployement of our webapplication into that servers manually effort. 6) By using Spring MVC we can develop the Web Application (or) Distributed Application(We need to add the dependency manually for supporting JSON Response(Jackson-API),XML Response(JaxB-API)) To Overcome all these problems then we need go for "Spring Boot MVC" from Spring Boot Module which is an extension module from Spring MVC. Spring Boot MVC = Spring MVC++. 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.