Submit date parameter from html form to controller in thymeleaf

I ran into the issue below passing the date to the controller if I remove the date and then it works correctly.

My html code

<div class="form-group" id="all">
    <label class="col-lg-2 control-label" for="focusedInput3">Start date</label>
    <div class="col-lg-10">
        <input type="date" id="startDate" name="startDate" th:value="*{startDate}" />
    </div>
</div>
<div class="form-group" id="all">
    <label class="col-lg-2 control-label" for="focusedInput4">End date</label>
    <div class="col-lg-10">
        <input type="date" id="endDate" name="endDate" th:value="*{endDate}" />
    </div>
</div>

      

and my Entity class

@Column(name = "name")
private String courseName;

@Column(name = "semister")
private String semister;

@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat (pattern="dd-MMM-YYYY")
@Column(name = "startDate")
private Date startDate;

@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat (pattern="dd-MMM-YYYY")
@Column(name = "endDate")
private Date endDate;

      

after clicking the submit button, the following error appears:

enter image description here

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing  this as a fallback.

Fri Apr 24 14:05:33 IST 2015
There was an unexpected error (type=Not Found, status=404).
No message available

      

Here is my controller

@Controller
@RequestMapping("/admin")
public class CourseScheduleController {

@Autowired
private CourseScheduleService courseScheduleService; 

@RequestMapping("/createCourse")
public ModelAndView getAllativities() {
    Map<String, Object> model = new HashMap<String, Object>();
    model.put("courseName", "");
    model.put("semister", "");
    model.put("startDate",null);
    model.put("endDate",null);
    return new ModelAndView("createCourse",model);
}

@RequestMapping("/saveCourse")
public String saveCourseSchedule(@ModelAttribute CourseBE courseBE){
    courseScheduleService.saveCourseSchedule(courseBE);
    return "redirect:/admin/createCourse";
}

      

Please help me to solve this problem.

+3


source to share


3 answers


Perhaps you should include a binder in the controller:



@InitBinder
    public void initBinder(WebDataBinder dataBinder) {
        dataBinder.setDisallowedFields("id");

        dataBinder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String value) {
                try {
                    setValue(new SimpleDateFormat("dd/MM/yyyy").parse(value));
                } catch (ParseException e) {
                    setValue(null);
                }
            }
        });

    }

      

0


source


Adding a binder to your controller will allow you to bind your form to your model.

 @InitBinder
 public void initDateBinder(final WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-mm-dd"), true));
 }

      

Should do the trick and convert your input string from your form to a date.

If you are using input type="date"

I guess you will have no choice in pattern date . If you want more flexibility in your template, just change the input type to text and adapt the format template to whatever you like.



You have to use th field in your html markup to bind to mork like:

<input th:field="*{startDate}"

      

and make sure your form has something like <form th:object="${courseBE}"

to define using a bean to bind your form.

0


source


Add the following:

logging.level.org.springframework.web=DEBUG

      

To the properties file. This will help with verbose logging of HTTP errors.

0


source







All Articles