1

The following input field for a date object works, apparently, nicely. But when it reaches the controller, the value for executionDate is null.

<form role="form" action="#" th:object="${pojo}" th:action="@{/scheduler/create}" method="post">
<div class="col-lg-5 col-sm-5 col-xs-10" >
    <div class="well with-header">
        <div class="header">
            <label th:for="datepicker0">Execution Date: </label>
        </div>
        <div class="form-group input-group">
            <input id="datepicker0" type="text" name="executionDate" th:field="*{executionDate}" class="form-control"></input>
            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>                               
        </div>
    </div>
</div>
// rest of the page
</form>

Relevant part of controller is:

@RequestMapping(value = "/scheduler/create", method = RequestMethod.POST)
public String createSchedulerPost(@Valid  @ModelAttribute("pojo") SchedulerPojo pojo, BindingResult result, ModelMap model) {

    System.out.println(pojo.getDescription());
    System.out.println(pojo.isRecurrent());
    System.out.println(pojo.getExecutionDate());
    System.out.println(pojo.getStartDate());
    System.out.println(pojo.getTerminationDate());
    System.out.println(pojo.getFailStrategy());
    (...) // I'm just verifying whether the SchedulerPojo pojo object has values for now...
}

The SchedulerPojo DTO is:

public class SchedulerPojo {

    private String id;

    private String description;

    private Date executionDate;

    private boolean recurrent;

    private Date startDate; 

    private Date terminationDate;

    private SchedulerFailStrategy failStrategy;

    // other attributes, getters and setters
}

Other, fields as the description String and recurrent boolean checkbox inputs return the given value on the HTML.

What am I missing here?

7
  • 1
    Can you please paste your controller code? Of course in your html you use th:object in the same <form>? And if you create datepicker object with inline js you escape code like in this example? Commented Apr 19, 2016 at 20:11
  • Yes, the th:object="${pojo}" reference is atop the <form>. Editing the question now. Commented Apr 19, 2016 at 20:14
  • 1
    Check if any value is sent to your controller (using for example firebug). Commented Apr 19, 2016 at 20:54
  • Some values, namely String description and the booleans, are printed out correctly by the controller... Commented Apr 19, 2016 at 20:58
  • 1
    Yes, but maybe there's a value sent to controller, but then java.util.Date constructor cannot handle this value? For now I only guess that problem is not in controller, but 1) the field is always empty so form sends null 2) there's a js problem with datepicker lib you use 3) some bizzare value for this field is sent to controller Commented Apr 19, 2016 at 21:12

1 Answer 1

1

According to Thymeleaf+Spring tutorial th:field generates code the same as you set id and name tags:

<input type="text" th:field="*{datePlanted}" />

Equivalent code :

<input type="text" id="datePlanted" name="datePlanted" th:value="*{datePlanted}" />

Maybe you need to remove id=datepicker0 to executionDate and bind the class on datepicker?

<input class="date" type="text" th:field="*{executionDate}" class="form-control"></input>
...
<script>
    $(document).ready(function () {
        $('.date').datepicker({dateFormat: 'dd.mm.yy'});
    });
</script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.