In this session, let us learn how to handle session in a spring MVC application. When you want to carry data across multiple pages for a particular session, you need to handle it properly. That you are a model attribute you want to carry the model attribute across multiple pages for a particular person means you you need to add it to a session. When you are working in a day application session can be achieved our session can be handled with the help of HTTP session interface. In a spring MVC application, if you want to maintain the conversational state, you can do it in two ways. One day, if you think are direct session attributes, this is an annotation.
You are just going to specify the model attributes that has to be carried across multiple pages. So I've been a session attribute is used for handling facial Are you can use your good old HTTP session interface, the HTTP session object should be injected to the controller. That is you're just going to also HTTP session with elfa HTTP session you can carry that model attributes across multiple pages. Now, let us see how to handle session in a spring MVC application. Using a simple example. I will go back to eclipse I am an eclipse I have created a spring starter project with Weber's dependency.
I have done the initial setup. Let us first go through the flow and we will identify in which scenario we need to start with session or in which scenario we need to use this at the rate session attribute. Let me explain the flow. As usual. I have got the application class here. The model classes product, then I have got product controller, then three JSP pages whole success and add product form.
So first from the product controller, I go to Home dot JSP from home dot JSP. And looking in for ad for ads Prop, the method annotated with that form is calling our product form. Here also instead of request mapping what we can use aggregate mapping. Let me import this. Okay, now I need to go into our product from in our product form. As usual I'm having a form with form fields.
When I click Submit, it has to go to this add product action. I'll go to this product controller in the product controller I have got value add product method, request method dot post, in snap that what we can do now we can go in for post mapping Let me import this also I don't need this like and what is the model attribute name product only I have given the same name and I am returning success. What am I printing in success Let me see that in success. I am trying to print the product details point it is working fine. After that I want to edit the product details. When I want to edit the product details I need to check whether the value is getting populated in the form that's what we are going to see now let's move forward on this application select spring MVC session right click run as run on server.
Click Finish. The application has started I am in home dot JSP let me click Add product. Now I have got add product from the JSP. Now let me enter the details. TV console. 900 quantities 900 I'm not doing any validation I have got a four I need to filter values it will take me to success dot JSP let me click OK Here I have given it a sad customer I'll just go and change it add product form instead of add customer it has to be add product this is the problem of copy pasting right.
So here's Let me refresh our product there Got it? Can you see TV 990 Let me click Add products. Now I have got success dot JSP wherein I'm having the product name cost and quantity. Let me go to the success that JC see I am trying to print the products we have overridden to string so it is printing complete product details then I am trying to retrieve the product name using dollar product dot name. Now I want to edit the form. So here I have given a link href equal to edit for view and edit.
I need to see the form with the module so that I can modify it. So when I click Edit from where it has to go, it has to go to product controller here, I need to have one other method to call edit product from. So here it says Edit form, show edit form. And here is a product from Actually, I'm trying to keep the URL similar to the operation that I'm performing. So it is going to show me this edit product form. This is going to be similar to our product for Monday.
So I'll just copy this Ctrl V. Now I need to give it an edit product form. Click Ok. I will open the Edit product form now in the Edit product form here instead of add, let me change it to edit and what about the action? It has to be added product. Right now I will go back to products or troller similar to add product I need to have edit product also. So, I will add one more method this product and now I need to go into final dot JSP wherein I want to print the details after the product details are edited that page is similar to success dot JSP file just copy it and I will paste it here itself.
I will give the name of final dot JSP and Open final dot JSP I don't need this line right Ctrl D okay we are do navitus editors product details. Now, I have done a flow from success dot JSP I have a link when this link is clicked it has to take me to edit product form wherein I want the values to be populated when they are now the decision for the user to modify or change the product details. Once the product details such as when they click submit it has to be To this next page it till now whatever we are seeing we are not writing any code in the product controller here. Basically within this you are supposed to call the back end layer service layer service talking to do come back to the result we are not doing anything we are directly printing it once again we are working with hibernate you will see how actually the complete flow is from the controller it goes to the service it is coming back to the result and then going to the final dot JSP our success dot JSP like that.
Okay, now, let me run this application we'll see the flaw first. What is the problem? The application has started I am in home the JC let me click this link. I have bought that product from let me add the product details. Product button. Now I am in success dot JSP when it Click this link it will take me to edit product from one aidid now, I am in Edit product form dot JSP here what actually I'm expecting I am expecting the values to be populated in the form so, that it is easy for me to change the values or modify the value values, but in this case I am not getting it, how to get it using session attributes.
Now, let me just go back to this product controller I will explain the flow Now, let us see the flow in the product controller first I am an add products in our product I am trying I am using accurate model attribute products. So, what actually is happening spring is creating a product object setting all the values that are in the form fields to the instance variable that is it is calling the setter methods in the model class. Then it is our training it to add the rate model attribute it is something like model dot r attribute this product comma product. So, the dummy name over here is product you will think that dummy name you are retrieving it in success dot JSP that is why it is printing it properly what happens to the values the values are getting seized over here itself because using model you can carry it only to the immediate page.
Now, you are given a date from again it is going to the product controller there is no session set. So, from here you are trying to call edit product for similarly let me add it for cost and quantity. Okay I have added the details. Let me rerun the application. The application has started let me click this link. I have bought that product crop and enter the details.
Let me click Add products. When I click Add product, what actually is happening spring framework is creating a product objects taking All these values and setting it to the product objects and then it is giving a dummy name for the product object that product we are given at the rate model attribute product only No. So, that name only is given to us the dummy name, I will click Add product. Now, I am able to retrieve it in success dot JSP this value is available only till success dot JSP it will not be carried to any other page because it is not in this session I will click View one is it Am I getting any value already we are at $1 product dot name product data whatever but it is not shown over here why the reason is it is not set in a session. So, I will go back to the product controller.
Now what I have to do once the product is populated with the values it has to be set in a session so that it can be retrieved in all the pages. I want to retrieve it even in the Edit product form. Okay, so I am just go scroll up. And here I'm just going to give session attributes. Let us see what is happening now. within which I need to give it a name that I want to carry across multiple pages.
If you are having multiple model attributes that has to be carried across in that scenario, you need to give it in the form of a string array within curly braces you have to give the dummy name. Let me save this I will rerun the application I have brought the first page let me click the link. I will add the product details. Click Add product. I am getting an error. Let us see what is the error in the console.
I'll maximize console. I'll scroll up HTTP session required exception expected session attribute products. This is the message let us see what Actually is happening and minimize this, I will go to this product controller where exactly we are getting the error, Are we getting the error in this page? No, are we getting the error in this page No, our ad product form dot JSP is coming perfectly fine. Now, in this particular point we are getting an error exception basically an exception why in a fixed version is occurring, you are given our threat model attribute product. It means when you are using arsenate session attribute product and when you are trying to use this annotation this method is trying to retrieve a product from the session.
I hope you can understand because I've got a direct session attribute and I'm using our direct model attribute spring framework is trying to retrieve an object from this product and assign it to this variable. It is time to call get attribute method. What I am telling spring framework You set attribute create a product object and assign it to this product attributes, but what spring assigned to do it assigned to retrieve it from this session define to retrieve a product object from the session and assign it to this product with variable which is not possible why when you are using accurate product this object must be available, we have not created it, spring will not create it because you are using session attributes. If you don't have session attribute if you are trying to give a product object it will create it in this case it will not create So, explicitly you need to create a product object where even before going into the form you create the product object and keep it ready.
So, in this ad for what I will do, I will create a product object product product equal to new product. I am creating a product object and I am setting I am giving a dummy name for the for the product object as product how already no God model in parameter. So, using Model Model dot add attribute, the first name is the dominie or the attribute may product comma product. Now, I will tell you the flow what actually is happening when you are trying to call the fall yet a new product object is created by you and you have given the dummy name as product. This is stored in session. So, when this method is called that time it says the product object is created and it is stored in C sharp.
Now, when you're calling this add product method, spring will retrieve the object which is already present and start populating that object with the values I hope you can understand. Now, in this case, when you're trying to call accurate model attributes, your new object is not getting created. Spring is retrieving that object which is already created for you and then it is starting to populate that object with a new values is it clear because you have created a project sorry product, the product object is available spring retrieve that and fetch the value from the form. Now, I will save this I will rerun this application. So, when you are using session attributes the objects should be available already the attributes should be available already started and add the details let me click Add product. Now see it is working perfectly fine because spring is retrieving the already created objects and calling all the setter methods to set the form field values.
Now, I have got a product object which is populated with values and the dummy name is dollar product. Now, again you see what is happening Click View unaided. Now can you see it is coming with the calculated values how in product controller when you are going to call get it for it is coming to the stage Sameer you're calling edit product form in Edit product from the GSP you are trying to retrieve dollar products isn't available Of course it is available why because you have set it up session. So in all the methods after this method, this product object is available in this pay in this method it is an object with the values. In this method it is an object with values populated with values. From here you're going to success from success you're coming to this page.
Here again the product object is available with values okay because thing is already populated. That is why when you call edit product for the it is populated with values properly. Okay and now again when you come to this edit product what will happen you are having a product object with value. So, needless entering into this method. Now, the user has changed the quantity and the price. So, what spring will do spring is going to take the original object and take the modified values call the setter method, so, that the original object is edited properly.
If you try to print here you will be getting that modified object Okay, now, I will just go to the method here I'm here now, I will just change it to 670 and this also will change let me click Edit tronic. Now, can you see the values that we are changing is deflecting in final dot JSP Okay, now, I will go back to the product controller in the storage controller I will try printing the values Add product method also let me try to print the product defect details inside the show it for if you want to print the product details, again you need to use this model attribute at the rate model attribute product product if you give and if you try to print it you will be getting a product objects populated with the values from the Add product from okay. So, here let me just rerun the application understand this using average session attributes we are trying to maintain session for model attributes across multiple pages create multiple controllers actually, you need to go in for controllers for every specific model.
If it is product for product how one controller for you should have one controller, adding, deleting, updating, then editing everything has to be done in a single controller. So this is the right way of coding in spring. Let me click this link. The application has started and enter the details. If we click Add product and go to this console also because in the console, I have got the details. Can you see it coming with populated values, I'll click view and edit.
Now, I will change it to mobile and I will change the price to 1000 this 200 click Edit product. Now can you see this spring is looking for an existing object many are using at the rate session attributes so that it can populate it with values and what will happen if you try to print the product object is too overdone to string you will not get any value, but basically only after the side product for the value is getting populated. I hope it is clear. This is how you handle sessions in a spring MVC application. Whatever we have learnt till now everything is separated Like we have learned how to do validation how to work with forms, how to handle session going forward when we are working with Spring and Hibernate, I will just show you how to use all the concepts together. Thank you