
- 27 students
- 25 lessons
- 0 quizzes
- 10 week duration
-
Java OOPs
- Java OOPs
- If Else in Java
- Threading in Java
- Joining of Threads
- Synchronized Methods
- Using Add, Notify in Java
- Some Java Classes
- Searching and Sorting
- Introducing Java
- Inheritance
- Exception Handling
- Calling of Constructors and Methods in Inheritance
- Beginning Object Oriented Programming
- A Small Project
- Inner Classes— Instance and Static
- Introduction to OOPS
- Methods
- Packages
- Serialization
- The This Reference Variable
- Installing and running a simple Java Program on Netbeans.
- Making Custom Tags for a JSP Page
- Beginning Spring Web MVC
- Setting up Hibernate?
- Java IO Questions.
Beginning Spring Web MVC
Spring’s web MVC framework
is a web MVC framework, it is request-driven, and depends on a central servlet that dispatches requests to controllers and offers other functionality that facilitates the development of web applications.
It is one of the most in-demand software skills in the job market today.
Spring starts by developing a servlet called the Dispatcher Servlet and setting an URL pattern such that all requests are handled here.
This is done by having a mapping in the web.xml file.
The DispatcherServlet implements the “Front Controller” design where every request must pass through the Dispatcher Servlet.
Our web.xml file.
<web-app> <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/root-context.xml</param-value> </context-param> <!-- Creates the Spring Container shared by all Servlets and Filters --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Processes application requests --> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
These are the relevant lines.
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Step 1: First request will be received by DispatcherServlet.Step 2: DispatcherServlet will take the help of HandlerMapping and get to know the Controller class name associated with the given request.Step 3: So request transfer to the Controller, and then controller will process the request by executing appropriate methods and returns ModelAndView object (contains Model data and View name) back to the DispatcherServlet.Step 4: Now DispatcherServlet send the model object to the ViewResolver to get the actual view page.Step 5: Finally DispatcherServlet will pass the Model object to the View page to display the result.A Spring Web MVC Project in Eclipse
<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd 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"> <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> <!-- Enables the Spring MVC @Controller programming model --> <annotation-driven /> <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> <resources mapping="/resources/**" location="/resources/" /> <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <beans:property name="prefix" value="/WEB-INF/views/" /> <beans:property name="suffix" value=".jsp" /> </beans:bean> <context:component-scan base-package="com.champak.springmvc" /> </beans:beans>
The Views folder.
Controller and Request Mapping.
HomeController
package com.champak.springmvc; import java.text.DateFormat; import java.util.Date; import java.util.Locale; import javax.servlet.http.HttpServletRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; /** * Handles requests for the application home page. */ @Controller public class HomeController { private static final Logger logger = LoggerFactory.getLogger(HomeController.class); /** * Simply selects the home view to render by returning its name. */ @RequestMapping(value = "/", method = RequestMethod.GET) public String home(Locale locale, Model model) { logger.info("Welcome home! The client locale is {}.", locale); Date date = new Date(); DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale); String formattedDate = dateFormat.format(date); model.addAttribute("serverTime", formattedDate ); return "home"; } @RequestMapping(value = "/patakh?", method = RequestMethod.GET) public String f(HttpServletRequest request,Model model) { model.addAttribute("path", request.getRequestURI()); return "path"; } @RequestMapping(value = "/test", method = RequestMethod.GET) public String posttest() { return "test"; } @RequestMapping(value = "/test", method = RequestMethod.POST) public String test(HttpServletRequest request,Model model) { String n=request.getParameter("n"); model.addAttribute("n",n); return "test"; } @RequestMapping(value="/roll?",method=RequestMethod.GET) public String P(HttpServletRequest request,Model model) { model.addAttribute("roll1",request.getRequestURI()); return "roll1"; } @RequestMapping(value="/newp*",method=RequestMethod.GET) public String R(HttpServletRequest request,Model model) { model.addAttribute("roll1",request.getRequestURI()); return "roll1"; } @RequestMapping(value="/formp*",method=RequestMethod.GET) public String B(HttpServletRequest request,Model model) { model.addAttribute("form",request.getRequestURI()); return "form"; } @RequestMapping(value="/formp*",method=RequestMethod.POST) public String C(HttpServletRequest request,Model model) { String n1=request.getParameter("n1"); String n2=request.getParameter("n2"); String n3=request.getParameter("n3"); model.addAttribute("n1",n1); model.addAttribute("n2",n2); model.addAttribute("n3",n3); model.addAttribute("form",request.getRequestURI()); return "form"; } }
ChampakController
package com.champak.springmvc; import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class ChampakController { @RequestMapping(value = "/champakwa/{id}", method = RequestMethod.GET) public String display(@PathVariable String id,Model model) { model.addAttribute("id",id); return "champak"; } @RequestMapping(value = "/books", method = RequestMethod.GET) public String multiplebook(Model model) { List<Book> books=new ArrayList<Book>(); Book book =new Book(); book.setBookname("Basic C"); book.setPrice(100); book.setSubject("C"); Book book1 =new Book(); book1.setBookname("Basic C++"); book1.setPrice(100); book1.setSubject("C++"); books.add(book); books.add(book1); model.addAttribute("books",books); return "multiplebookview"; } @RequestMapping(value = "/book", method = RequestMethod.GET) public String singlebook(Model model) { Book book =new Book(); book.setBookname("Basic C"); book.setPrice(100); book.setSubject("C"); model.addAttribute("book",book); return "singlebookview"; } }
end