Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

data-th-field doesn't use formatters in Spring Boot application #495

Closed
jmvivo opened this issue May 8, 2016 · 4 comments
Closed

data-th-field doesn't use formatters in Spring Boot application #495

jmvivo opened this issue May 8, 2016 · 4 comments

Comments

@jmvivo
Copy link

jmvivo commented May 8, 2016

According to #223 for this entity:

@Entity
public class MyEntity {
    ...
    ...
@DateTimeFormat(pattern="dd/MM/yyyy")
private Calendar calendar;

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

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

and this template:

      <form data-th-object="${myEntity}">
        <input type="text"
            data-th-field="*{{calendar}}"/> 
        <span data-th-text="*{{calendar}}"></span>

        <input type="text"
            data-th-field="*{{date}}"/>
        <span data-th-text="*{{date}}"></span>
        <input type="text"
            data-th-field="*{{aDouble}}"/>
        <span data-th-text="*{{aDouble}}"></span>
     </from>

should use Spring Formatters to transform values but I get:

<form>
        <input type="text" id="calendar" name="calendar"
   value="java.util.GregorianCalendar[time=1454108400000,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=4,WEEK_OF_MONTH=4,DAY_OF_MONTH=30,DAY_OF_YEAR=30,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=5,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=3600000,DST_OFFSET=0]">
   <span>30/01/2016</span>

    <input type="text" id="date" name="date" value="2016-03-31 00:00:00.0">
    <span>31/03/16</span>

    <input type="text" class="form-control" id="aDouble" name="aDouble" value="12.3">
    <span>12.30000</span>

    </form>

As you can see, span with th-text formats values as expected, but input with th-field doesn't.
Is this a regression? Is there any configuration missing?

Thank you in advance.

Note: I try it with Spring boot 1.3.4 and 1.3.3

Note 2: There is a post in StackOverflow about it.

@danielfernandez
Copy link
Member

I am unable to reproduce this, using the same annotation and the same data type. Check this (non-Spring Boot) sandbox app:

Entity: https://github.com/thymeleaf/thymeleafsandbox-springjsp/blob/master/src/main/java/thymeleafsandbox/springjsp/business/entities/User.java#L34-L35
and page: https://github.com/thymeleaf/thymeleafsandbox-springjsp/blob/master/src/main/webapp/WEB-INF/templates/thymeleaf/form.html#L46-L48

Could you please give me more details: what specific version of Spring Framework is being used in this Spring Boot app? Thymeleaf… I suppose we are talking about 2.1.4, right?

Is the conversion service being explicitly configured for looking at annotations? For example in XML configuration you'd need something like:

<mvc:annotation-driven conversion-service="conversionService"/>

BTW: in a th:field the double-braces are not needed. th:field value rendering is actually delegated to Spring itself, so there is actually no way to show a value coming from a form-backing bean in a Spring-enabled form without Spring applying all its conversion infrastructure to it.

@jmvivo
Copy link
Author

jmvivo commented May 23, 2016

Thank you @danielfernandez:

This is a simple proof created using Spring Initializr tool, including web, validation and Thymeleaft dependencies. This generates a project with:

  • org.springframework.boot:spring-boot-starter-thymeleaf:jar:1.3.4.RELEASE
  • org.thymeleaf:thymeleaf-spring4:jar:2.1.4.RELEASE
  • nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:jar:1.3.3
  • org.springframework:spring-core:jar:4.2.6.RELEASE

I suppose that conversionService is correctly configured as dat-th-text works as expected in <span> tags. See:

<input type="text" ... name="aDouble" value="12.3">
<span>12.30000</span>

I just upload the proof in https://github.com/DISID/disid-proofs/tree/master/spring-boot-thymeleaf-field-problem so you can check it.

Thank you very much!!!

@jmvivo
Copy link
Author

jmvivo commented May 24, 2016

I've got it:

  • The myEnity object is loaded on model through a @PathVariable by a Formatter, instead use a @ModelAttribute.
  • This makes that no BindingResult is available when Thymeleaf builds a BindStatus for field value
  • So, no PropertyEditor is found to transform the value to a String.

So, to solve it I must change this request mapping definition:

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

        }

by this one:

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

        }

Thank you.

Regards,
Chema.

@jmvivo jmvivo closed this as completed May 24, 2016
@danielfernandez
Copy link
Member

Correct, that's what I was just seeing too, but I wanted to make sure that it was not in the Thymeleaf side, basically that Spring is not expecting the View to additionally create BindingResult objects on the variables coming from the @PathVariable mechanism (as opposed to the model).

So I made the equivalent to this using JSP and Spring's taglib and I can confirm behaviour is the same: Spring does not create BindingResult objects for @PathVariables, and they should therefore be manually added to the model in controller methods (for example, by switching the annotation to @ModelAttribute as you did).

@danielfernandez danielfernandez removed this from the Thymeleaf 2.1 milestone May 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants