gpt4 book ai didi

validation - 如何在表单验证失败时提供我选择的消息

转载 作者:行者123 更新时间:2023-12-04 02:20:02 24 4
gpt4 key购买 nike

我正在使用 spring mvc 3.0。这是我的 Controller 类:

    @Controller
@RequestMapping("/author")
public class AuthorController {
@Autowired
private IAuthorDao authorDao;

@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
binder.registerCustomEditor(Date.class, new CustomDateEditor(
dateFormat, true));
}

@RequestMapping(method = RequestMethod.GET)
public String get(Model model) {
return "author-list";
}

@RequestMapping(value = "/new", method = RequestMethod.GET)
public String create(Model model) {
model.addAttribute("author", new Author());
return "author-form";
}

@RequestMapping(value = "/new", method = RequestMethod.POST)
public String createPost(@ModelAttribute("author") Author author,
BindingResult result) {
new AuthorValidator().validate(author, result);
if (result.hasErrors()) {
return "author-form";
} else {
authorDao.persist(author);
return "redirect:/author/" + author.getId();
}
}

@RequestMapping(value = "/{authorId}", method = RequestMethod.GET)
public String view(@PathVariable("authorId") int authorId) {
return "author-view";
}
}

我正在尝试验证作者对象。它有 dob attr 类型是 Date。

我正在使用以下类进行验证:

    public class AuthorValidator {
public void validate(Author author, Errors errors) {

if(author.getfName()==null)
errors.rejectValue("fName", "required", "required");
else if (!StringUtils.hasLength(author.getfName())) {
errors.rejectValue("fName", "required", "required");
}

if(author.getfName()==null)
errors.rejectValue("lName", "required", "required");
else if (!StringUtils.hasLength(author.getlName())) {
errors.rejectValue("lName", "required", "required");
}

if(author.getDob()==null)
errors.rejectValue("dob", "required", "required");
else if (!StringUtils.hasLength(author.getDob().toString())) {
errors.rejectValue("dob", "required", "required");
}
}
}

当我没有在表单中输入任何内容时,它会给出所需的消息,这是正确的,但是当我给出不正确的格式时,它会写入 Failed to convert property value of type java.lang.String to required type属性 dob 的 java.util.Date;嵌套异常是 java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "asdads"
需要
作为消息。如何使它像“无效日期”。

谢谢。

最佳答案

如果您使用标准的 Spring 工具,您需要做的就是:

添加消息来源:

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="com.example.messages" />
</bean>

添加属性文件 com/example/messages.properties 包含以下之一:

typeMismatch.author.date = Invalid date
typeMismatch.date = Invalid date
typeMismatch.java.util.Date = Invalid date
typeMismatch = Invalid date

首先让您为特定命令对象(author)中的特定字段(date)配置错误消息,其次 - date 字段任何命令对象,第三个 - java.util.Date 类的字段绑定(bind)错误的消息,最后一个 - 任何转换导致的错误的一般消息。

这是我的 JSP 页面:

<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="sf" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<sf:form commandName="author">
<sf:errors path="date" /><br />
<sf:input path="name"/><br />
<sf:input path="date"/><br />
<input type="submit" value="ok" />
</sf:form>
</body>
</html>

关于validation - 如何在表单验证失败时提供我选择的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8722483/

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