提问者:小点点

Thymeleaf不使用使用data-th字段的输入格式化程序


我在Spring Boot应用程序中使用Thymeleaf时发现了一个问题。

版本:

  • Spring Boot 1.3.4和1.3.3

我的实体:

@Entity
public class MyEntity {

  @Id
  @GeneratedValue
  private Long id;

  @Version
  private int version;

  @DateTimeFormat(pattern="dd/MM/yyyy")
  private Calendar calendar;

  @DateTimeFormat(pattern="dd/MM/yy")
  private Date date;

  @NumberFormat(pattern="#0.00000")
  private Double aDouble;

}

我的控制器:

@RequestMapping(value = "/{myEntity}/edit-form", 
     method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
public String editForm(@PathVariable MyEntity myEntity, Model model)  {
    return "myEntity/edit";
}

我的myEntity/dit. html模板:

    <form class="form-horizontal" method="POST"
      data-th-object="${myEntity}"
      data-th-action="@{/myEntity/{id}(id=*{id})}">
      <input type="hidden" name="_method" value="PUT" />

      <div class="form-group"
        data-th-classappend="${#fields.hasErrors('calendar')}? 'has-error has-feedback'">
        <label for="calendar" class="col-md-3 control-label">Calendar</label>
        <div class="col-md-3">
          <input type="text" class="form-control"
            data-th-field="*{calendar}"/>
          <span data-th-text="*{{calendar}}"></span>
        </div>
      </div>

      <div class="form-group"
        data-th-classappend="${#fields.hasErrors('date')}? 'has-error has-feedback'">
        <label for="date" class="col-md-3 control-label">Date</label>
        <div class="col-md-3">
          <input type="text" class="form-control"
            data-th-field="*{date}"/>
          <span data-th-text="*{{date}}"></span>
        </div>
      </div>

      <div class="form-group"
        data-th-classappend="${#fields.hasErrors('aDouble')}? 'has-error has-feedback'">
        <label for="date" class="col-md-3 control-label">aDouble</label>
        <div class="col-md-3">
          <input type="text" class="form-control"
            data-th-field="*{{aDouble}}"/>
          <span data-th-text="*{{aDouble}}"></span>
        </div>
      </div>

      <div class="form-group">
        <div class="col-md-12">
          <div class="pull-right">
            <button type="submit" class="btn btn-primary">Update</button>
          </div>
        </div>
      </div>
    </form>

当我尝试显示此页面时,我得到:

<body>

<form class="form-horizontal" method="POST" action="/myEntity/1">
      <input type="hidden" name="_method" value="PUT" />

      <div class="form-group">
        <label for="calendar" class="col-md-3 control-label">Calendar</label>
        <div class="col-md-3">
         <input type="text" class="form-control" id="calendar" name="calendar"
            value="java.util.GregorianCalendar[time=1451602800000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id=&quot;Europe/Madrid&quot;,offset=3600000,dstSavings=3600000,useDaylight=true,transitions=165,lastRule=java.util.SimpleTimeZone[id=Europe/Madrid,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2016,MONTH=0,WEEK_OF_YEAR=53,WEEK_OF_MONTH=0,DAY_OF_MONTH=1,DAY_OF_YEAR=1,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=3600000,DST_OFFSET=0]" /> 
          <span>01/01/2016</span>
        </div>
      </div>

      <div class="form-group">
       <label for="date" class="col-md-3 control-label">Date</label>
       <div class="col-md-3">
         <input type="text" class="form-control" id="date" name="date" 
            value="2016-02-01 00:00:00.0" />
         <span>01/02/16</span>
       </div>
      </div>


     <div class="form-group">
       <label for="date" class="col-md-3 control-label">aDouble</label>
       <div class="col-md-3">
          <input type="text" class="form-control" id="aDouble" name="aDouble" 
             value="0.1" /> 
          <span>0.10000</span>
        </div>
      </div>


      <div class="form-group">
        <div class="col-md-12">
          <div class="pull-right">
            <button type="submit" class="btn btn-primary">Update</button>
          </div>
         </div>
       </div>
     </form>
  
  </body>

如您所见,对所有值使用toString()方法对所有字段的值进行格式化并不符合预期(参见span,它通过th-text属性使用相同的值)。

有人能帮我吗?提前谢谢。

编辑1:我已经创建了一个关于它的新问题


共2个答案

匿名用户

已解决。问题出在request estMaps定义中:

@RequestMapping(value = "/{myEntity}/edit-form", method = RequestMethod.GET, 
               produces = MediaType.TEXT_HTML_VALUE)
public String editForm(@PathVariable MyEntity myEntity, Model model) {

    }

它需要请求上下文中的BindingResult来获取属性编辑器,它可以将对象值转换为String。因此,在请求映射定义中包含BindingResult都按预期工作:

@RequestMapping(value = "/{myEntity}/edit-form", method = RequestMethod.GET, 
               produces = MediaType.TEXT_HTML_VALUE)
public String editForm(@ModelAttribute MyEntity myEntity, BindingResult result, 
               Model model) {

    }

请注意,对象必须使用@Model属性进行注释,并且BindingResult必须在@Model属性之后声明。

感谢所有试图帮助我的人。

匿名用户

对于我要使用的日期:

${#dates.format(date, 'dd/MMM/yyyy HH:mm')}

和数字:

${#numbers.formatDecimal(num,3,2,'COMMA')}

在留档中,它被解释为数字和日期。