java 在jsp中从ModelAndView中检索模型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1185083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieving a model from ModelAndView in jsp
提问by smauel
I am currently trying to return a model from onSubmit() in my controller. I then am trying to retrieve this in my jsp.
我目前正在尝试从控制器中的 onSubmit() 返回模型。然后我试图在我的 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.
但是,这不会显示任何内容。它转到正确的重定向视图并且不会发出任何错误,但不会显示文本。
Any help will be much appreciated.
任何帮助都感激不尽。
thanks
谢谢
回答by serg
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");

