Nothing Important here
V51 Bringing in Maven
here we added the required JAR in the pom.xml, we need spring-webmvc and spring-web
V52 The Dispatcher Servlet
the first thing to do in order to use spring MVC is defining a Dispatcher Servlet.
we will create a Dynamic Web Project in Eclipse, then we will convert it to Maven project by right clicking on the project and selecting Convert To Maven.
now we will add the dispatcher servlet, you can right click and choose add servlet, then check Use an existing Servlet class or jsp
press browse and search for org.springframework.web.servlet.DispatcherServlet this is the DispatcherServlet class.
or you can add it directly to web.xml like this
now, this servlet is actually connected to the beans.xml file, however this file should be named as DISPLAYNAME-servlet.xml, so as the display name is offers --> the beans.xml file should be named offers-servlet.xml
As you can see in the image above, we have added this file inside WEB-INF
V53 Adding a Controller
now we will add the following controller
as you can see we are using @Controller, and @RequestMapping
now, in order for Spring to find this controller we should add the following to the offers-servlet.xml
1- mvc context
2- <mvc:annotation-driven> tag in order to understand the @Controller and @RequestMapping and other tags.
3- and <context:componet-scan> to find all the beans
V54 View Resolver
in the previous tutorial we defined the following Controller
the returned value "home" is actually the name of the view (which is gonna be a JSP page in this example, but it could be VM template or anything else), what we have to do is to define a view resolver to understand that.
There are different types of View Resolvers the one we will use is InternalResourceViewResolver, this resolver is gonna be defined as a bean in offers-servlet.xml
as you can see we define a prefix and suffix, so spring will translate that to PREFIX+RETURNED STRING+SUFFIX
so when we say return "home"; it will be /WEB-INF/jsps/home.jsp
and as you can see we define the home.jsp page in WEB-INF/jsps
so now when you make a request to http://localhost:8084/offers/ (offers is the display name we defined before in web.xml)spring will run the controller and redirect you to home.jsp
V55 Adding Data to Session
now we will talk how to pass information from the controller to the view,
here we will add the data in the session, and read them in the view.
in the controller we write:
as you can see the function now takes a parameter HttpSession, and we add a value to the session by session.setAttribute()
now in the home.jsp
you see that we used session.getAttribute()
V56 Using Spring Data Model
now we will see how to pass the value between the controller and the view correctly,
to do that we use ModelAndView like this
as you can see the function showHome() is returning ModelAndView now not a String, in addition the ModelAndView constructor getting a parameter which is the View name.
and we use model.put to put a value.
another way to do the same thing is
Same function showHome, is returning a String which is the view name , and it takes an input which is Model
now to read the value in the View
as you can see we use request.getAttribute()
V57 Using JSTL
This video is about the java script tagging language
as you can see you add a taglib, then you use the tags, sure you have different taglibs you can search the internet to know more about them
V58 Setting Up a Jndi DataSource
here he talked how you can define a JNDI datasource in Tomcat then use it, he defined a Database connection,
he followed this link https://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html
the steps are:
1- add a Resource to the Tomcat context.xml
2- define a resource-ref in web.xml
3- now you can lookup for this datasource in the code
V59: Bringing In the DAO Code & V60: ContextLoadListner
in the first video he brought Offer & OfferDAO classes,
the idea here that it is better to seperate your beans.xml files, which means it is better to have a beans.xml file just for the database access.
what we do here is
1- creating a package com.spring.dao and add Offer & OfferDAO classes inside
2- create a package com.spring.web.config, inside this package we will put our beans.xml files, we will add dao-context.xml file
4- it is very important to tell spring to check the dao-context.xml, in order to do this you should add ContextLoaderListner in web.xml
V61 Creating a Datasource Bean
in previous video we have created a JNDI datasource to connect to the database, we will add this datasource as bean here
in OfferDAO we have this
this datasource should be the JNDI one, and we will add it to dao-context.xml
<jndi-lookup > to get the jndi
V62 Adding Service Layer
to make the real MVC, we will add a service layer, so by that the controller will call the service layer and the service layer will call the database.
we already have com.spring.web.controller where the controllers stay.
we already have com.spring.web.dao where database objects stay
we will create a new package com.spring.web.service and we will put our services objects here.
inside this package we will create the OfferService class
we define the OfferService with @Service which is exactly similar to @Component, but it is just a new name to specify that this is a service,
also we defined service-context.xml which will have the beans definition for services:
now the controller will use the OffersService,
V63 Adding a New Controller
nothing new here, he added a new controller.
V64 Getting URL Parameters
in order to read the URL parameters, you add @RequestParam as parameter to the RequestMapping method, sure you can add as much as you can