【问题标题】:SpringMVC: how to specify the date format in jsp pagesSpringMVC:如何在jsp页面中指定日期格式
【发布时间】:2015-03-12 14:39:48
【问题描述】:

我有一个模型定义如下:

public class TM
{
    @DateTimeFormat(pattern = "yyyy.MM.dd HH:mm:ss")
    Date date;

    public Date getDate()
    {
        return date;
    }

    public void setDate(Date date)
    {
        this.date = date;
    }
}

我有一个控制器定义如下:

@RequestMapping(value = "test")
public ModelAndView func(ModelAndView mav)
{
    TM tm = new TM();
    tm.setDate(new Date());
    mav.addObject("obj", tm);
    mav.setViewName("test/view.jsp");
    return mav;
}

文件view.jsp

${obj.date}

它输出Wed Jan 14 20:00:46 CST 2015,而不是我期望的“yyyy.MM.dd HH:mm:ss”格式

SpringMVC 的注解 @DateTimeFormat 对我不起作用。

我知道我可以在 jsp 页面中使用 <fmt:formatDate... 来实现相同的目的,但这不是我喜欢的方式,因为我必须在必须打印 Date 对象或 TM 的任何地方添加 <fmt:formatDate...对象。

我只是想知道如何使用 sping 的@DateTimeFormat 来实现这一点?

【问题讨论】:

  • 先使用JodaDate,Date会好很多。在那里,您可以设置所需的精度级别和格式。如果你愿意,我可以为你创建一个答案,但你可以用简单的谷歌找到它
  • @WeareBorg 是否可以使用 Java 内置的 Date 类?
  • 我已经创建了一个答案,检查一下,如果有任何疑问,请在 cmets 中告诉我。
  • 它确实有效,但只有当你正确使用它时,你才没有正确使用它。日期格式仅在您将其与弹簧表单标签一起使用时才有效。你只是用EL来打印字段的值,基本上就是Date.toString()

标签: java spring jsp servlets model-view-controller


【解决方案1】:

你可以使用 JSTL:

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
...
<fmt:formatDate value="${obj.date}" pattern="yyyy-MM-dd HH:mm:ss" />

【讨论】:

  • 感谢您的快速回复,但我刚刚更新了我的问题,我知道我可以使用 jstl 实现这一目标,但我想知道如何使用 springmvc 实现这一目标
  • 好吧,你为什么不想使用 JSTL?您在 JSP 中使用什么?假设您正在使用 JSP。
  • 你是否使用任何模板引擎,例如velocity?
【解决方案2】:

把这个:

<mvc:annotation-driven/>

到 dispatcher-servlet.xml。

并把这个xml开头:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:device="http://www.springframework.org/schema/mobile/device"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mobile/device http://www.springframework.org/schema/mobile/device/spring-mobile-device-1.0.xsd">

【讨论】:

    【解决方案3】:

    给你:

    您可以使用 SimpleDateFormat 格式化您的日期。检查 ou JavaDocs,只是一些示例。

    **String oldstring = "2011-01-18 00:00:00.0";
    Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(oldstring);**
    
    String newstring = new SimpleDateFormat("yyyy-MM-dd").format(date);
    System.out.println(newstring); // 2011-01-18
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-26
      • 2021-07-14
      • 1970-01-01
      • 1970-01-01
      • 2016-03-08
      • 2012-12-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多