如果删除对日期的任何引用,表单会成功提交,但是当我包含日期字段时,我会收到以下错误:
出现意外错误(type = Bad Request,status = 400)。 object ='film'的验证失败。错误计数:1
我的代码如下:
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";
}
}
答案 0 :(得分:0)
请尝试提交表单并检查浏览器为您的日期字段发送的格式。按Ctrl + Shift + i然后转到网络选项卡(如果您使用Chrome)。
格式因您使用的日期组件而异。字段通常具有格式&#34; yyyy-MM-dd&#34;。
如果您的请求中发送的格式为&#34; yyyy-MM-dd&#34;,请使用正确的格式对日期字段进行分配,如下所示:
@DateTimeFormat("yyyy-MM-dd")
Date date;
它会使它发挥作用。
希望这有帮助。