gpt4 book ai didi

java - 带有日期字段的对象的 Spring 验证失败

转载 作者:太空宇宙 更新时间:2023-11-04 10:44:26 24 4
gpt4 key购买 nike

如果我删除对日期的任何引用,表单会成功提交,但是当我包含日期字段时,会收到以下错误:

There was an unexpected error (type=Bad Request, status=400). Validation failed for object='film'. Error count: 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>

电影.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;
}


}

电影 Controller

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();
}
}

家庭 Controller

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";
}
}

最佳答案

请尝试提交您的表单并检查浏览器为您的日期字段发送的格式。 Ctrl+Shift+i,然后转到“网络”选项卡(如果您使用 Chrome)。

格式根据您使用的日期组件而有所不同。字段通常采用“yyyy-MM-dd”格式。

如果您的请求中发送的格式是“yyyy-MM-dd”,请使用正确的格式注释您的日期字段,如下所示:

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

它会让它发挥作用。

希望这有帮助。

关于java - 带有日期字段的对象的 Spring 验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48568578/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com