k.elkourchi
k.elkourchi

Reputation: 131

spring - difference between request.setAttribute and model.addAttribute?

can any one tell me the difference between request.setAttribute and model.addAttribute in spring web app?

Upvotes: 9

Views: 6581

Answers (2)

Parin Patel
Parin Patel

Reputation: 41

Request V/s Model

where request could get attributes through getAttribute("") method. normally it is used for getting information from defined attributes and used inside the method for performing the different operations. So Basically Request used for input.

Just like Request, the model provides addAttribute("","") method, Through this model, we could make the object and storing data inside model object and deploying it on result Server Page.Basically it used in storing input data which is provide by us and storing for some time.

Upvotes: -1

rompetroll
rompetroll

Reputation: 4799

The difference is, that Model is an abstraction. You could use Spring with servlets, portlets or other frontend technologies and Model attributes will always be available in your respective views.

HttpServletRequest on the other hand is an object specific for Servlets. Spring will also make request attributes available in your views, just like model attributes, so from a user perspective there is not much difference.

Another aspect is that models are more lightweight and more convenient to work with (e.g iterating over all attributes in a model map is easier than in a request).

Upvotes: 12

Related Questions