简体   繁体   English

黑白传递模型和在jsp / gsp上使用请求对象的区别

[英]Difference b/w passing model and using request object at jsp/gsp

I have been working both on spring and grails (grails is built on spring). 我一直在从事spring和grails的工作(grails是基于spring的)。

We can pass our data from controller to view (jsp/gsp) via request object or via model. 我们可以通过请求对象或模型将数据从控制器传递到视图(jsp / gsp)。

Passing data via model is preferred because its easy. 首选通过模型传递数据,因为它很容易。

Comparison of code : 代码比较:

Spring :
    controller  request.setAttribute("name","india");
    view        ${pageContext.request.getAttribute('name')}

    controller  ModelAndView("viewname",modelObject)
Grails
    controller  request.setAttribute("name","india")
    view        ${request.getAttribute("name")}

    controller  render(view:"viewName",model:[name:"india"])

I think request object does many functionalities other than this. 我认为请求对象除此以外还有许多功能。

So my question is : If passing data from controller to view can be done with request object itself why java technology required to make a concept like model ? 所以我的问题是:如果可以使用请求对象本身来完成从控制器到视图的数据传递,那么为什么Java技术需要创建像模型这样的概念?

necessity is mother of invention 必然是发明之母

I mean to say that there must be a task that can be implemented only via a model. 我的意思是说,必须存在只能通过模型​​执行的任务。

A few things come to mind, but they are speculation as to why. 我想到了几件事,但是他们在猜测原因。

Mainly, separation of concerns. 主要是关注点分离。 Modifying the request , which is input, to hold output seems to be abusing this. 修改输入的请求以保留输出似乎在滥用这一点。 The model is clearly ouput, and only output. 该模型显然是输出,并且仅输出。

By clearly defining what constitutes output, using a model, frameworks such as Spring and Grails can then apply other processes to only the model. 通过使用模型明确定义构成输出的内容,诸如Spring和Grails之类的框架可以将其他过程仅应用于模型。 Grails filters come to mind, as an example. 以Grails过滤器为例。

That's my quick opinion. 那是我的见解。

That data which a controller gathers up and needs to make available to the view should be put in the model. 控制器收集并需要对视图可用的数据应放在模型中。 That is kind of the definition of what the model is in this context. 这就是这种情况下模型的定义。 Never use request attributes for this. 切勿为此使用请求属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM