Thymeleaf不使用格式化程序来使用数据字段

时间:2016-05-07 18:34:03

标签: spring-mvc spring-boot thymeleaf

我在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 / edit.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>

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

&#13;
&#13;
<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>
&#13;
&#13;
&#13;

正如您所看到的,所有字段的值未按预期格式(请参阅span使用相同的值通过th-text属性){ {1}}所有值的方法。

任何人都可以帮助我吗?提前谢谢。

编辑1:我创建了new issue about it

2 个答案:

答案 0 :(得分:0)

解决。问题出在 requestMapping 定义中:

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

    }

需要请求上下文中的BindingResult 才能获得PropertyEditor,它可以将对象值转换为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) {

    }

请注意,对象必须使用@ModelAttribute 进行注释, BindingResult必须在@ModelAttribute 之后声明。

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

答案 1 :(得分:-1)

约会时我会使用:

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

和数字:

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

在文档中对numbersdates进行了解释。