在 JSP scriptlet 中,如何访问从 Spring MVC ModelMap 传递的 java.util.Date 值?

发布于 2024-11-16 08:53:17 字数 676 浏览 2 评论 0原文

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 中的 ModelMapmyDate 值?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

倾其所爱 2024-11-23 08:53:17

您应该能够使用 ${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 Spring

一袭水袖舞倾城 2024-11-23 08:53:17

ModelMap 中的属性存储为请求(或会话,具体取决于您的声明)属性,而不是参数。
控制器方法完成执行后,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"), not getParameter.
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.Dates based on patterns, <fmt:formatDate>.

野心澎湃 2024-11-23 08:53:17

这是一个请求参数,您需要适当地绑定,我上周写了一篇关于此的博客文章:

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

十六岁半 2024-11-23 08:53:17

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文