简体   繁体   English

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

[英]In a JSP scriptlet, how do you access a java.util.Date value passed from a Spring MVC ModelMap?

FooController.java: 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: 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 ? 为什么JSP scriptlet无法获取添加到ModelMap中的FooControllermyDate值?

You should be able to just access your ModelMap parameter with ${myDate} - see similar questions: How do I access ModelMap in a jsp? 您应该能够使用${myDate}访问ModelMap参数 - 请参阅类似的问题: 如何在jsp中访问ModelMap? and JSPs not displaying objects from model in Spring JSP不在Spring中显示模型中的对象

The attributes in the ModelMap are stored as request (or session, depending on your declarations) attributes , not parameters. ModelMap中的属性存储为请求(或会话,取决于您的声明) 属性 ,而不是参数。 After your controller method finishes execution, Spring forwards to the JSP associated with the returned view name. 在控制器方法完成执行后,Spring将转发到与返回的视图名称关联的JSP。

So, in your JSP, you must use request.getAttribute("myDate") , not getParameter . 因此,在JSP中,必须使用request.getAttribute("myDate") ,而不是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". 实际上,你应该远离JSP中的Java代码,但是你也应该理解EL表达式的作用 - 在你的情况下, ${myDate}找到名为“myDate”的请求属性。

PS: There is an existing tag in JSTL for formatting java.util.Date s based on patterns, <fmt:formatDate> . PS:JSTL中有一个现有的标签,用于根据模式<fmt:formatDate>格式化java.util.Date

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 http://linkedjava.blogspot.com/2011/06/spring-controller-with-date-object.html

Answer by Nicolae Albu is right - this is request attribute , not parameter . Nicolae Albu的回答是对的 - 这是请求属性 ,而不是参数 Attribute is something you associate with request yourself, in code, using API (in this case - Spring MVC does that using Model). 属性是您自己与请求关联的东西,在代码中,使用API​​(在这种情况下 - Spring MVC使用Model来实现)。 Parameters are added by Servlet Container, not you, and they represent URL/POST parameters sent by browser. 参数由Servlet Container添加,而不是您,它们代表浏览器发送的URL / POST参数。

The only thing to add is that ${varName} is equivalent to pageContext.findAttribute("varName"); 唯一要添加的是$ {varName}等同于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). 和request.getAttribute(“varName”)相当于pageContext.getAttribute(“varName”,PageContext.REQUEST_SCOPE)(如果您不确定这是什么,请查看Servlet中的文档,请求,会话和应用程序范围) + JSP页面)。

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

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