Spring MVC - Durgesh
> Servlet : Used to generate dynamic content to a web page.
M - Data related
V - Present data to user
C - Interface b/w Model and view
Steps:
All set to implement now.
Just right click server and click Start
> After creating project add tomcat dependency to it.
1 Step : Configuring dispatcher servlet.
we can give any servlet name but it should be same for name in <servlet-mapping>
servlet class we will enter fully classified name.
and url pattern / means all the url's (Ex : /home , /about ... and so on)
2 Step: Create Spring configuration file.
It should be at same level of web.xml
Naming convention for spring config file : <servlet-name> - servlet.xml
here : spring-servlet.xml
here in spring configuration file we will configure view resolver.
class name will be fully classified name
prefix: value here is the path with respect to webapp folder
here we will have our all view frontend files inside views folder .
here we will have our all view frontend files inside views folder .
suffix: is the extension for webapp file .
4 Step : Creating Controller
here the class has been made controller class by annotation @Controller
@RequestMapping here is for URI whenever it is fired by browser then controller will return index
now the "index" will be send to View resolver which is in views folder.
So here at same level of Spring Config we'll create index.jsp page that will be returned from view resolver as response will be index.jsp.
5 Step : Create View to show page
So this was the response (index.jsp) for /home request in controller.
So
Similarly we can create any number of .jsp files in view folder and controller for it in controller class.
********************************************************************************
Sending Data from CONTROLLER to View
> Using Model
for adding attribute in Controller class
Model is imported from springframework.ui class.
Object that we are adding can be of any type : primitive, class (Employee,person..),collection , date ..
> Using ModelAndView
here in Controller class we will create method with return type of ModelAndView ,this object will have both view name and attribute/Object value with it.
So here we have set viewName as help
and added Object name and roll number.
Object that we are adding can be of any type : primitive, class (Employee,person..),collection , date ..
here in View page we are getting Object using getAttribute and now we can use it in HTML page as follows.
for it we will have to give isELIgnored value false , by default it takes value as true ,So Expression Language values are ignored.
now by this we can access values for controller's Model Object by this expression.
We don't need to first create the variable using <% %> tag and then again using it for using the variable.
saved lots of line of code and time.
> for iterating through list we will use JSTL library from servlets in View.
i.e by this library we can use java functionalities just by using tags.
but in backend of these tags java code runs.
we have lot n lot of tags but here we will just look forech loop tag for our implementation example.
Below giving some example for set, out and if tags.
- <c:set var="income" scope="session" value="${4000*4}"/>
- <c:if test="${income > 8000}">
- <p>My income is: <c:out value="${income}"/><p>
- </c:if>
> Now implementing it in our jsp page
here we are having marks list in Controller class now if we directly use {$ marks} so it will give output as List because its toString method will return it in same way but here we will iterated through each elements saperately.
for using JSTL we will have to add its dependency in pom.xml file first
now as dependency is added we will need to add this tag.<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
*******************************************************************************
Sending data from VIEW to CONTROLLER
here in View we can have multiple fields which gets stored in HttpServletRequest Object as we Submit our form .
The controllers URL is given in action tag of form.
While here in controller we can use getParameter() method to access data from HttpServletRequest object but here in MVC we have annotations :
@ModelAttribute : here it implements Model class that will have all data in single object.
@RequestParam : here we will have to separately fetch data for each field.
> Firstly we'll need to know more about @RequestMapping
So we can use @RequestMapping on class level as well as method level.
here the @RequestMapping at class level is having parameter /first ,So whenever we need to access home we will have to add prefix /first : < URL >/first/home.
Also in @RequestMapping at method level we have multiple parameters :
path : used for URI
method : is used for CRUD operations which we need to apply in our page.
[Here the method used CRUD wrt View page not Controller ( front-end ) ]
i.e here our front-end is getting/reading data from controller So we used RequestMethod.GET
> Getting data from front-end ( View ) to Controller using form :
here is the controller class 1 method will be fired when URI is /contact.
second RequestMapping method is defined for fetching data from front end using @RequestParam for each fields of form.
here the parameter of attribute is name of field in form ( front-end )
and value next to it is the variable in which we are storing that value.
here we have one more parameter of Model type so it is for sending data from controller to View page
At last the method is returning success so this will be the page that will be shown after signup is clicked.
success.jsp
view page : here we will have same name for the fields as in our model class( here User Class)
now we will use this user class to access all the data fields to controller.
here in handleForm method we are accessing User type object "user" so now we will get the data coming from View in "user" .
So, we will directly use it from "user" here in controller class we will not need to specify RequestParam for each field separately.
Also we have another use of @ModelAttribute is we can use it above any method so it will run before the object that we have fire of that class.
Ex : here in browser when we will fire /contact uri then commonDataForModel method will get executed ,And also when we submit the form then the handleForm method is called So before calling it also commonDataForModel method will get executed.
So it will be helpful if we have any common data to be displayed on all the pages of this class so we can use it.
Also check weather we are able to access "user" in view page i.e success.jsp here without giving Model as parameter.???
> Now we will store the contact.jsp form data in our Database:
we can directly call DB from controller class but this is not the proper way of doing so.
So here we will follow the above layering for storing data in our database.
firstly we will have to import these dependency in our project.
> 14th video of playList will have to stop here for creating hibernate database notes since its being used after this
Comments
Post a Comment