2

If I remove any reference to the date, the form submits successfully, but when I include the date field I get the following error:

There was an unexpected error (type=Bad Request, status=400). Validation failed for object='film'. Error count: 1

My code is as below:

addfilm.html

<form action="#" th:action="@{/film/addfilm}" th:object="${film}" method="post">
    <p>Title:</p> <input type="text" th:field="*{title}"/>
    <p>Director:</p> <input type="text" th:field="*{director}"/>
    <p>Description:</p> <input type="text" th:field="*{description}"/>
    <p>Release Date:</p> <input type="date" th:field="*{date}"/>
    <p/><input type="submit" value="Add"/>
</form>

film.java

package com.demo.spring.domain;

import java.util.Date;

public class Film {

    String title;
    String director;
    String description;
    Date date;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDirector() {
        return director;
    }

    public void setDirector(String director) {
        this.director = director;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }


}

FilmController

package com.demo.spring.controller;

import com.demo.spring.domain.Film;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/film")
public class FilmController {

    @RequestMapping(value = "/addfilm", method = RequestMethod.GET)
    public String filmView(Model model)
    {
        Film film = new Film();
        model.addAttribute("film", film);
        return "addfilm";
    }

    @RequestMapping(value = "/addfilm", method = RequestMethod.POST)
    @ResponseBody
    public String film(Model model, @ModelAttribute("film") Film film)
    {
        return "This film was added to the database: "+film.getTitle()+" "+film.getDate();
    }
}

HomeController

package com.demo.spring.controller;

import com.demo.spring.domain.Film;
import com.demo.spring.domain.User;
import com.demo.spring.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class HomeController {

    @Autowired
    UserService userService;

    @RequestMapping(value = "/", method = RequestMethod.GET )
 //   @ResponseBody
    public String index(Model model, HttpSession session)
    {
        if(session.getAttribute("login")==null)
        {
            return "redirect:/user/login";
        }

        Film film = new Film();
        model.addAttribute("film", film);

        List<User> users = userService.findAll();
        model.addAttribute("users",users);
        return "index";
    }
}
Dan
  • 951
  • 1
  • 23
  • 46
  • have you used `DateTimeFormat` annotation in your model? if so, what pattern are you using on client and backend? also paste your model and paste request being made with browser – Dmitry Senkovich Feb 01 '18 at 17:37
  • this is all my code regarding film, not sure what else to give you? not sure i understand fully but on backend i expect it to want yyyy,mm,dd, but noticed if I put the input type as date it goes dd,mm,yyyy. Didnt know if that would have any impact so also tried as text field but no joy. – Dan Feb 01 '18 at 17:43
  • when i tried to add date it promted import java.util.Date; and so I did. forgive me if i am misunderstanding here, i am new to spring. I understand DateTimeFormat however but thought this import covered that. – Dan Feb 01 '18 at 17:44
  • 1
    Possibly the accepted answer might help you: https://stackoverflow.com/questions/40259336/date-in-spring-mvc – Boris Feb 01 '18 at 17:55
  • Thank you @Boris this worked a charm. Do you want to put as answer and I will accept. – Dan Feb 01 '18 at 17:58
  • @Dan Boris's link is exactly what I meant:) cool! – Dmitry Senkovich Feb 01 '18 at 17:59
  • @Dan that's fine, I should not duplicate the answer really. Glad that it worked! – Boris Feb 01 '18 at 18:02
  • User's can and will submit whatever data they feel like to your form. Even though your form says it requires a date format, users could use curl/wget/postman to submit requests to your url. I have often seen exceptions being returned to users due to spring code being unable to convert a non-date string to a date. A friendly recommendation is to have all ui model attributes be of type string. Then check the "type" of data and then have methods on your model like 'getDateAsDate()' etc... It is more work, but I have found it to be more user friendly. – hooknc Feb 01 '18 at 18:27

1 Answers1

0

Please try to submit your form and inspect the format that is sent by the browser for your date field. Ctrl+Shift+i and then go to Network tab (if you use Chrome).

The format varies according to the date component you're using. fields generally have the format "yyyy-MM-dd".

If the format sent in your request is "yyyy-MM-dd", please anotate your date field with the correct format, like the following:

@DateTimeFormat("yyyy-MM-dd")
Date date;

It will make it work.

Hope this helps.