简体   繁体   English

在jsp中从ModelAndView检索模型

[英]Retrieving a model from ModelAndView in jsp

I am currently trying to return a model from onSubmit() in my controller. 我目前正在尝试从控制器中的onSubmit()返回模型。 I then am trying to retrieve this in my jsp. 然后,我试图在我的jsp中检索它。

for example 例如

Map model = new HashMap();
model.put("errors", "example error");
return new ModelAndView(new RedirectView("login.htm"), "model", model);

and then retrieving it with 然后用

<c:out value="${model.errors}"/>

However this does not display anything. 但是,这不会显示任何内容。 it goes to the correct redirectview and does not issue any errors, but the text is not displayed. 它会转到正确的redirectview,并且不会发出任何错误,但不会显示该文本。

Any help will be much appreciated. 任何帮助都感激不尽。

thanks 谢谢

What RedirectView does is sending a redirect header to the browser so browser does a complete reload of the page, as a result model does not get carried over there (as it is handled now by login controller with its own model). RedirectView所做的是向浏览器发送重定向标头,以便浏览器完成页面的完全重载,因为结果模型不会被带到那里(因为它现在由登录控制器使用其自己的模型进行处理)。

What you can do is pass errors through request attributes: 您可以做的是通过请求属性传递错误:

In your views.properties: 在您的views.properties中:

loginController.(class)=org.springframework.web.servlet.view.InternalResourceView
loginController.url=/login.htm

Then instead of RedirectView return: 然后,而不是RedirectView返回:

request.setAttribute("errors", "example errors");
return new ModelAndView("loginController");

And in your login controller check for this attribute and add it to the model. 然后在您的登录控制器中检查此属性并将其添加到模型中。

Update: Without using views.properties: 更新:不使用views.properties:

request.setAttribute("errors", "example errors");
return new ModelAndView(new InternalResourceView("/login.htm"));

OR you can add (another) internal view resolver to your App-servlet.xml (Note from API: When chaining ViewResolvers, an InternalResourceViewResolver always needs to be last, as it will attempt to resolve any view name, no matter whether the underlying resource actually exists.): 或者,您也可以在App-servlet.xml中添加(另一个)内部视图解析器(来自API的注释:链接ViewResolvers时,InternalResourceViewResolver总是需要最后一个,因为它将尝试解析任何视图名称,无论基础资源是否确实存在。):

<bean id="viewResolver2"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
</bean>

And then just use: 然后只需使用:

request.setAttribute("errors", "example errors");
return new ModelAndView("/login.htm");

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

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