繁体   English   中英

域中的日期字段和g:textField标记在Grails中不起作用

[英]Date field in domain and g:textField tag don't work in Grails

我有一个用户域从SecUser扩展的应用程序(Spring Secrity Core中的安全用户类)。 在该域(用户)中,我有服务器字段,其中之一是: Date birthDate 脚手架会创建g:datePicker标记,但我不喜欢这种样式。

我想实现例如:Boostp的Datepicker( http://www.eyecon.ro/bootstrap-datepicker/ ),但是该库使用输入type =“ text”字段。

在我的视图(.gsp )中,如果我输入文本,则保存和更新操作将不起作用,因为在我的控制器中,当我获得userInstance.birthDate时,该值为null。

如果我放置了g:datePicker标记,我的控制器将正确获取该值。

我怎么解决这个问题? 谢谢。

编辑

我认为Datepicker引导代码:

<div class="input-group date date-picker" data-date-format="dd-MM-yyyy" data-date-end-date="+0d">
       <input type="text" id="birthDate" name="birthDate" value="${formatDate(format:'dd-MM-yyyy',date: userInstance?.birthDate)}" class="form-control form-shadow"/>
       <span class="input-group-btn">
             <button class="btn default" type="button">
                  <i class="fa fa-calendar"></i>
             </button>
       </span>
</div>
<span class="help-block">
  <h5>
       <g:message code="custom.date.picker.description" default="Select a date"/>
  </h5>
</span>

现在,我可以使用params.birthDate在控制器中获取值,但出现下一个错误:

Class java.text.ParseException
Message Unparseable date: "01-04-2016"

解决了:

解决方案是在Datepicker中正确解析Bootstrap库。 该库使用其他解析器。 例如:Grails中的“ 01-04-2016 ”为“ dd-MM-yyyy ”,但在此库中为“ dd-mm-yyyy ”。

在此链接中,格式显示为: https : //bootstrap-datepicker.readthedocs.org/en/latest/options.html#format

此外,有必要在Config.groovy中添加以下行:

grails.databinding.dateFormats = [ 'dd-MM-yyyy', 'yyyy-MM-dd', 'yyyy-MM-dd HH:mm:ss.S' ]

直接解决方案是在控制器操作中映射日期字段:

def save( User user ) {
  ....
  user.birthDate = new SimpleDateFormat( 'dd-MM-yyyy' ).parse params.birthDate
  ...
}

暂无
暂无

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

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