在 JSP scriptlet 中,如何访问从 Spring MVC ModelMap 传递的 java.util.Date 值?
FooController.java:
@RequestMapping(value = "/foo", method = RequestMethod.GET)
public final String foo(HttpServletRequest request, ModelMap model)
{
java.util.Date myDate = new java.util.Date();
model.addAttribute("myDate", myDate);
return "foo";
}
foo.jsp:
<%
java.util.Date myUtilDate = (java.util.Date)request.getParameter("myDate");
org.joda.time.DateTime myJodaDate = new org.joda.time.DateTime(myUtilDate);
%>
<joda:format value="${myJodaDate}" style="LL"/>
为什么 JSP scriptlet 无法获取添加到 FooController
中的 ModelMap
的 myDate
值?
FooController.java:
@RequestMapping(value = "/foo", method = RequestMethod.GET)
public final String foo(HttpServletRequest request, ModelMap model)
{
java.util.Date myDate = new java.util.Date();
model.addAttribute("myDate", myDate);
return "foo";
}
foo.jsp:
<%
java.util.Date myUtilDate = (java.util.Date)request.getParameter("myDate");
org.joda.time.DateTime myJodaDate = new org.joda.time.DateTime(myUtilDate);
%>
<joda:format value="${myJodaDate}" style="LL"/>
Why does the JSP scriptlet fail to obtain the myDate
value that was added to the ModelMap
in the FooController
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该能够使用
${myDate}
访问 ModelMap 参数 - 请参阅类似的问题:如何在 jsp 中访问 ModelMap? 和 JSP 不显示 Spring 中模型中的对象You should be able to just access your ModelMap parameter with
${myDate}
- see similar questions: How do I access ModelMap in a jsp? and JSPs not displaying objects from model in SpringModelMap 中的属性存储为请求(或会话,具体取决于您的声明)属性,而不是参数。
控制器方法完成执行后,Spring 转发到与返回的视图名称关联的 JSP。
因此,在 JSP 中,您必须使用
request.getAttribute("myDate")
,而不是getParameter
。实际上,您应该远离 JSP 中的 Java 代码,但您还应该了解 EL 表达式的作用 - 在您的例子中,
${myDate}
查找名为“myDate”的请求属性。PS:JSTL 中有一个现有标签,用于根据模式格式化
java.util.Date
,
。The attributes in the ModelMap are stored as request (or session, depending on your declarations) attributes, not parameters.
After your controller method finishes execution, Spring forwards to the JSP associated with the returned view name.
So, in your JSP, you must use
request.getAttribute("myDate")
, notgetParameter
.Actually, you should stay away from Java code in JSPs, but you should also understand what EL expressions do - in your case,
${myDate}
finds the request attribute named "myDate".P.S.: There is an existing tag in JSTL for formatting
java.util.Date
s based on patterns,<fmt:formatDate>
.这是一个请求参数,您需要适当地绑定,我上周写了一篇关于此的博客文章:
http://linkedjava.blogspot.com/2011/06/spring-controller-with-date-object.html
That's a request parameter, you need to bind appropriately coming off the wire, I wrote a blog post on this last week:
http://linkedjava.blogspot.com/2011/06/spring-controller-with-date-object.html
Nicolae Albu 的回答是正确的 - 这是请求属性,而不是参数。
属性是您自己在代码中使用 API 与请求关联的东西(在本例中 - Spring MVC 使用模型来实现这一点)。参数是由 Servlet 容器添加的,而不是您添加的,它们代表浏览器发送的 URL/POST 参数。
唯一需要补充的是 ${varName} 相当于 pageContext.findAttribute("varName");
request.getAttribute("varName") 相当于 pageContext.getAttribute("varName", PageContext.REQUEST_SCOPE) (如果您不确定这是什么意思,请查找 Servlet 中的页面、请求、会话和应用程序范围的文档+JSP)。
Answer by Nicolae Albu is right - this is request attribute, not parameter.
Attribute is something you associate with request yourself, in code, using API (in this case - Spring MVC does that using Model). Parameters are added by Servlet Container, not you, and they represent URL/POST parameters sent by browser.
The only thing to add is that ${varName} is equivalent to pageContext.findAttribute("varName");
and request.getAttribute("varName") is equivalent to pageContext.getAttribute("varName", PageContext.REQUEST_SCOPE) (if you're not sure what this is about, look up documentation on page, request, session and application scopes in Servlets+JSPs).