如何在MVC中进行模型通信的视图

时间:2022-01-29 11:29:20

I have been reading about MVC from a book. The confusion is about the fact how the view to model communication happens. I want to understand how this happens in a simple java web application which uses JSP and servlets.

我一直在读一本关于MVC的书。混淆是关于如何建立通信模型的事实。我想了解这是如何在一个使用JSP和servlet的简单java Web应用程序中发生的。

the view usually gets the state and data it needs to display, directly from the model

视图通常直接从模型中获取需要显示的状态和数据

It is the controller which changes the state of a model object. Once it is changed, the controller passes that specific object to a JSP page via the request object.

它是控制器,它改变模型对象的状态。一旦更改,控制器就会通过请求对象将该特定对象传递给JSP页面。

in controller servlet

在控制器servlet中

rd = request.getRequestDispatcher("/success.jsp");
User userObj = new User(username, password);
request.setAttribute("user", userObj); 
rd.forward(request, response);

in success.jsp

<%  
User bean=(User)request.getAttribute("user");  
out.print("Welcome, "+bean.getName());  
%>  

The request dispatcher forwarded the request and response objects to the jsp page. the jsp page used the request object to get access to the modified model object (userObj). is this what is meant by the view talks to the model? is this the fundamental way of communication between the view and the model? (I mean through request.getAttribute()?)

请求调度程序将请求和响应对象转发到jsp页面。 jsp页面使用请求对象来访问修改后的模型对象(userObj)。这是与模型的视图对话的意思吗?这是视图和模型之间沟通的基本方式吗? (我的意思是通过request.getAttribute()?)

1 个解决方案

#1


In a MVC architecture Controller receives the data from the request object, manipulates the model & then forwards the flow to the jsp. Jsp fetches from the scope in which the attribute is set & displays.

在MVC架构中,Controller从请求对象接收数据,操纵模型然后将流转发到jsp。 Jsp从设置和显示属性的范围中提取。

There are several scopes wherein you can set the attribute.

您可以在几个范围内设置属性。

Request, Session, ServletContext

each scope has getAttribute() & setAttribute()

每个范围都有getAttribute()和setAttribute()

${requestScope.yourValue} : request scope which is also the default
${sessionScope.yourValue} : retrieving from the session scope
${applicationScope.yourValue} : retrieving from the context scope

When the JSP is fetching the value from the respective scopes, this is the example of view talking to the model. Besides it's not a mandate that you stick to this flow, you can always customize the flow of the app as per your requirements.

当JSP从相应的作用域中获取值时,这是视图与模型交谈的示例。除此之外,您不必遵守此流程,您可以随时根据自己的要求自定义应用程序的流程。

#1


In a MVC architecture Controller receives the data from the request object, manipulates the model & then forwards the flow to the jsp. Jsp fetches from the scope in which the attribute is set & displays.

在MVC架构中,Controller从请求对象接收数据,操纵模型然后将流转发到jsp。 Jsp从设置和显示属性的范围中提取。

There are several scopes wherein you can set the attribute.

您可以在几个范围内设置属性。

Request, Session, ServletContext

each scope has getAttribute() & setAttribute()

每个范围都有getAttribute()和setAttribute()

${requestScope.yourValue} : request scope which is also the default
${sessionScope.yourValue} : retrieving from the session scope
${applicationScope.yourValue} : retrieving from the context scope

When the JSP is fetching the value from the respective scopes, this is the example of view talking to the model. Besides it's not a mandate that you stick to this flow, you can always customize the flow of the app as per your requirements.

当JSP从相应的作用域中获取值时,这是视图与模型交谈的示例。除此之外,您不必遵守此流程,您可以随时根据自己的要求自定义应用程序的流程。