【问题标题】:Date binding in springmvc in yyyy-MM-dd formatyyyy-MM-dd格式的springmvc中的日期绑定
【发布时间】:2015-03-21 14:33:39
【问题描述】:
<tr class="rowsAdded">
        <td><input name="item" class="form-control" type="text" placeholder="Item" /></td>
        <td><input name="amount" class="form-control" type="number" placeholder="Amount" /></td>
        <td><input name="expenseDate" class="form-control" type="date"placeholder="ExpenseDate" /></td>
</tr>

下面是我的控制器和初始化活页夹

@RequestMapping (value = "/saveExpenses", method=RequestMethod.POST)
    public String saveExpenses (@RequestBody ExpenseDetailsListVO expenseDetailsListVO, Model model,BindingResult result) {
        if (result.hasErrors()) {
            System.out.println(result.getFieldError().getField().toString()+" error");
        }
        System.out.println(expenseDetailsListVO);       
        return "success";
    }

@InitBinder
    public void initBinder(WebDataBinder webDataBinder) {
     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
     dateFormat.setLenient(false);
     webDataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
     }

这样我想要的日期格式不起作用,这是我得到的输出 expenseDate=2015 年 3 月 18 日星期三 05:30:00 IST 但我希望它变成像 yyyy-MM-dd 这样的特定格式...建议我这样做的方法。

【问题讨论】:

  • 我想你的 ExpenseDetailsListVO 类有一个 Date 类型的成员。当您在expenseDetailsListVO 上调用println 时,它只会调用该Date 成员的toString() 方法。您的活页夹不以任何方式参与。因此,您将获得 Date 的默认字符串表示形式。您可以更改打印对象的方式,也可以使用扩展 Date 的新类,但使用不同的 toString() 来存储您的费用日期。
  • 是的,你是对的......成员是 Date 类型,因为在后端它也是 date 类型,所以从 VO 转换为 DO 时很容易......所以我现在应该做什么??
  • @Sharique 试试dateFormat.format(yourDateObj)

标签: java spring spring-mvc


【解决方案1】:

这不是更容易吗?

实体或表单支持对象:

class Foo {

  /* handles data-binding (parsing) and display if spring form tld or spring:eval */
  @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
  private Date expenseDate;

  ...
}

形式:

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>

<form:form modelAttribute="modelAttributeName">
  <form:input type="date" path="expenseDate" />
</form:form>

或者只是为了展示:

<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>

<spring:eval expression="modelAttributeName.expenseDate" />

一些迂腐的笔记:

  1. 对布局使用 CSS,而不是表格。请参阅 Bootstrap 的网格系统。
  2. 对表单使用Post-Redirect-Get 模式。
  3. 使用 Spring 的 Form taglib 进行正确的 HTML 转义和 CSRF 保护
  4. 在控制器处理程序方法中使用 @Validated 进行验证
  5. 您在表单中的“占位符”之前缺少一个空格

在此处查看我的帖子以获得最佳实践:Spring MVC: Validation, Post-Redirect-Get, Partial Updates, Optimistic Concurrency, Field Security

【讨论】:

    【解决方案2】:

    我不知道这个答案对你有帮助吗? 我的实体类之一如下...

    @Entity
    @Table(name = "TAIMS_INC_INCIDENT")
    public class Incident implements Serializable{
    
        @DateTimeFormat(pattern = "dd/MM/yyyy") // This is for bind Date with @ModelAttribute
        @Temporal(TemporalType.DATE)
        @Column(name = "inc_date")
        private Date incidentDate;
    
    }
    

    然后像这样输入..

    <input type="text" id="incident-date" name="incidentDate" value="23/08/2017" />
    

    Spring 控制器方法在这里..

        @RequestMapping(value = "/saveIncident.ibbl", method = RequestMethod.POST)
        public ModelAndView saveIncident(
                    @ModelAttribute("incident")
                    Incident incident,
                    BindingResult result){
    
                    System.out.println(incident.getIncidentDate());
                    // Wed Aug 23 00:00:00 BDT 2017
    
        }
    

    这工作正常。 Incident incident 包含 eventDate = Wed Aug 23 00:00:00 BDT 2017

    如果你不想在实体类中使用@DateTimeFormat(pattern = "dd/MM/yyyy"),那么将下面的方法放在控制器类中...

        @InitBinder
        public void initBinder(WebDataBinder binder) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
            dateFormat.setLenient(false);
            binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
        }
    

    【讨论】:

      【解决方案3】:

      我的意思是你的活页夹可能工作得很好。它的作用是从 HTTP 请求的正文中获取格式为 yyyy-MM-dd 的字符串,并将其转换为 Date 对象。但仅此而已。之后你对那个 Date 对象做什么取决于你。如果你想将它作为字符串传递给程序的其他部分,你应该转换它,也许使用 SimpleDateFormatter。

      【讨论】:

      • 嘿谢谢你的建议!! HTTP 请求正文以 yyyy-MM-dd 格式绑定它,但我可以在绑定发生之前更改它的格式,就像这种格式 dd-MM-yyyy
      • 您可以将其类型保留为字符串,而不是在您的活页夹中先将其转换为日期,然后再转换回您实际想要的字符串格式。我应该说这看起来很奇怪,但是由于您没有提供有关您想在该日期做什么的信息,因此我无法提出任何其他建议
      • 我只是在询问知识...我只想将其转换为日期对象,然后将其保存到数据库中...那么我应该在哪里将其转换为对象...我的意思是如何? ?
      • 它已经是一个对象,确切的类是Date。只需将其传递给您的数据库保存方法,就可以了。
      • 但它是 String 类型,而我的后端 pojo 类具有 date 类型...假设我也将它转换为 Date 类型,但在我的数据库中它也是 Date 类型
      猜你喜欢
      • 2016-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-10
      • 2015-07-03
      相关资源
      最近更新 更多