我知道这里有很多类似的问题,但没有一个能解决我的问题。
我正在使用Spring 4.0.3和Hibernate Validator 5.1.0。
当我尝试省略path
标记的<form:errors/>
属性时会出现此问题,因此:
<form:errors path="contato.nome" />
的作品
<form:errors path="*" />
的作品
<form:errors />
不起作用
我不知道为什么会这样。 Spring javadocs(org.springframework.web.servlet.tags.form.ErrorsTag)说应该这样工作:
仅限字段 - 设置字段名称(或路径)的路径
仅对象错误 - 省略路径
所有错误 - 设置路径为*
请帮帮我吗?
感兴趣的代码位于'edicao.jsp'和ContatoController.java的方法'confirmarEdicao'中。对不起,如果我的英语不好。
ContatoController.java
@Controller
@RequestMapping("/contatos")
public class ContatoController {
@Autowired
private ContatoService contatoService;
@Autowired
private MessageSource messageSource;
@RequestMapping(value = "/confirmarEdicao", method = RequestMethod.POST)
public String confirmarEdicao(@Valid Contato contato, BindingResult bindingResult) {
if(bindingResult.hasErrors()) {
return "contatos/edicao";
}
contatoService.save(contato);
return "redirect:/contatos";
}
@RequestMapping(method = RequestMethod.GET)
public ModelAndView form(HttpServletRequest request) {
String message = messageSource.getMessage("teste", null, new Locale("pt", "BR"));
System.out.println(message);
return new ModelAndView("contatos/listagem")
.addObject("contatos", contatoService.list());
}
@RequestMapping("/remover/{id}")
public String remover(Contato contato) {
contatoService.delete(contato);
return "redirect:/contatos";
}
@RequestMapping("/editar/{id}")
public ModelAndView formEdicao(Contato contato) {
contato = contatoService.find(contato.getId());
return new ModelAndView("contatos/edicao")
.addObject(contato);
}
@RequestMapping(value = "/cadastrar")
public String formCadastro() {
return "contatos/cadastro";
}
@RequestMapping(value = "/confirmarCadastro", method = RequestMethod.POST)
public String confirmarCadastro(@Valid Contato contato, BindingResult bindingResult,
RedirectAttributes redirectAttributes) {
if (bindingResult.hasFieldErrors()) {
return "contatos/cadastro";
}
contatoService.save(contato);
redirectAttributes.addFlashAttribute("mensagem", "Contato cadastrado com sucesso.");
return "redirect:/contatos";
}
@ResponseBody
@RequestMapping(value = "/pesquisar/{nome}", method = RequestMethod.GET,
produces="application/json")
public List<Contato> pesquisar(@PathVariable String nome) {
return contatoService.findByName(nome);
}
}
edicao.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Editar contato</title>
</head>
<body>
<c:set var="context">${pageContext.request.contextPath}</c:set>
<script type="text/javascript">var context = "${context}";</script>
<script src="${context}/resources/js/jquery-2.1.0.min.js"></script>
<script src="${context}/resources/js/contatos/edicao.js"></script>
<form:form commandName="contato" action="${context}/contatos/confirmarEdicao" method="post">
<form:errors/>
<table>
<form:hidden path="id"/>
<tr>
<td>Nome:</td>
<td><form:input path="nome" /></td>
</tr>
<tr>
<td>Telefone:</td>
<td><form:input path="telefone"/></td>
</tr>
<tr>
<td><input type="button" value="Voltar" id="btn_voltar"/><input type="submit" value="Salvar"/></td>
</tr>
</table>
</form:form>
</body>
</html>
Contato.java
package com.handson.model;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;
public class Contato {
private Long id;
@Size(min = 3, message = "Nome deve ter no mínimo 3 caracteres")
@NotEmpty(message = "O nome deve ser preenchido")
private String nome;
private String telefone;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getTelefone() {
return telefone;
}
public void setTelefone(String telefone) {
this.telefone = telefone;
}
public Contato withId(Long id) {
setId(id);
return this;
}
public Contato withTelefone(String telefone) {
setTelefone(telefone);
return this;
}
public Contato withNome(String nome) {
setNome(nome);
return this;
}
@Override
public String toString() {
return "Contato [id=" + id + ", nome=" + nome + ", telefone="
+ telefone + "]";
}
}
答案 0 :(得分:12)
应该定义一些关键字:
foo
,foo.bar
或foo.bar.baz
)nestedPath
请求属性(新路径相对于此路径)foo
)foo.bar
)标记<form:form commandName="foo">
将嵌套路径定义为nestedPath=foo
。当您编写<form:errors path="bar">
时,它会尝试查找为路径foo.bar
定义的错误。
假设您遇到与foo
(对象错误),foo.bar
和foo.bar.baz
(嵌套字段错误)相关的错误。这意味着什么:
<form:errors>
,则只会显示绑定到foo
路径的错误=&gt; 1条消息<form:errors path="bar">
,则只会显示绑定到foo.bar
路径的错误=&gt; 1条消息<form:errors path="*">
,则会显示绑定到foo
及其子路径的错误=&gt; 3条消息<form:errors path="bar.*">
,则只有foo.bar
的儿童错误显示=&gt; 1条消息<form:errors path="bar*">
,则绑定到foo.bar
的错误及其子路径会被显示=&gt; 2条消息检查Errors
类JavaDoc可能会为您提供额外的见解。