错误400输入类型=日期客户端发送的请求在语法上不正确

时间:2014-11-23 22:28:40

标签: java jsp spring-mvc

每次我在表单中插入输入类型=日期时,返回错误400,但如果我从表单中删除输入,则代码运行正常。我尝试过不同类型的约会,但任何人都有效。

我的控制器

package br.com.starcode.agenda.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import br.com.starcode.agenda.domain.Usuario;
import br.com.starcode.agenda.service.UsuarioService;

@Controller
public class UsuarioController {

@Autowired
UsuarioService UsuarioService; 

@RequestMapping(value="/usuario", params="new")
ModelAndView novoUsuario() {
    Usuario usuario = new Usuario();
    usuario.setDataNascimento(new Date());
    return new ModelAndView("cadastrar-usuario")
            .addObject("usuario", usuario);
}

@RequestMapping(value="/usuario", method = RequestMethod.POST)
ModelAndView confirmarNovo(
        Usuario novoUsuario,
        @RequestParam(value="dataNascimento", required=false) @DateTimeFormat(pattern="yyyy-MM-dd") Date dtNasc,
        RedirectAttributes redirectAttributes) {
    try {
        novoUsuario.setDataNascimento(dtNasc);
        //insert 
        UsuarioService.insert(novoUsuario);
         //success
        redirectAttributes.addFlashAttribute("msg", "Registro '" + novoUsuario.getIdUsuario() + "' inserido com sucesso!");
        return new ModelAndView("redirect:/");
     } catch (Exception e) {
        return new ModelAndView("cadastrar-usuario")
                .addObject("erro", e.getMessage())
                .addObject("usuario", novoUsuario);
     } 
 }

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

 }

我的jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
 <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

 <!DOCTYPE html>
 <html lang="pt-br">
 <head>
 <meta charset="utf-8">
 <meta http-equiv="X-UA-Compatible" content="IE=edge">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Coleções - Cadastrar Usuário</title>

 <!-- Bootstrap -->
 <link href="<c:url value="/bootstrap/css/bootstrap.min.css" />" rel="stylesheet">
 <link href="<c:url value="/bootstrap/css/bootstrap-responsive.css" />" rel="stylesheet">


 </head>
 <body>

 <div class="container" style="background-color:#ffffff" >

        <h1>Cadastrar Usuário</h1>
        <p>Faça seu cadastro abaixo e monte já suas coleções!</p>

                <form id="form-cadastrar-usuario" class="form-horizontal" role="form" method="post" action="<c:url value="/usuario" />" >


                    <div class="input-group input-group-lg">
                        <span class="input-group-addon">Nome:</span>
                        <input type="text" class="form-control" placeholder="Digite seu nome" name="nomeUsuario" id="nomeUsuario" value="${usuario.nomeUsuario}" >
                    </div><p/>

                    <div class="input-group input-group-lg">
                        <span class="input-group-addon">E-mail:</span>
                        <input type="text" class="form-control" placeholder="Digite seu e-mail" name="emailUsuario" id="emailUsuario" value="${usuario.emailUsuario}" >
                    </div><p/>

                    <div class="input-group input-group-lg">
                        <span class="input-group-addon">Sobrenome:</span>
                        <input type="text" class="form-control" placeholder="Digite seu sobrenome" name="sobrenomeUsuario" id="sobrenomeUsuario" value="${usuario.sobrenomeUsuario}" >
                    </div><p/>

                    <div class="input-group input-group-lg">
                        <span class="input-group-addon">Senha:</span>
                        <input type="password" class="form-control" placeholder="Digite uma senha" name="senhaUsuario" id="senhaUsuario" value="${usuario.senhaUsuario}" >
                    </div><p/>

                    <div class="input-group input-group-lg">
                        <span class="input-group-addon">Data de Nascimento:</span>
                        <input type="date" class="form-control" placeholder="Digite a data do seu nascimento" name="dataNascimento" id="dataNascimento" value="<fmt:formatDate pattern="yyyy-MM-dd" value="${usuario.dataNascimento}" />" />
                    </div><p/>

                    <br>

                    <button type="submit" class="btn btn-large btn-primary" >Enviar</button>
                    <button type="reset" class="btn btn-large btn-primary" >Limpar</button>
                    <a href="<c:url value="/" />" class="btn btn-large btn-danger" >Cancelar</a>

                </form>             

</div>

    <!-- MENSAGENS -->
 <div class="container-fluid">

   <c:if test="${not empty param.erro or not empty erro}">
    <div class="alert alert-danger fade in text-center" role="alert">
     <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Fechar</span></button>
     <strong>${param.erro}${erro}</strong>
    </div>
   </c:if>
   <c:if test="${not empty param.msg or not empty msg}">
   <div class="alert alert-success fade in text-center" role="alert">
     <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span>   <span class="sr-only">Fechar</span></button>
    <strong>${param.msg}${msg}</strong>
  </div>
  </c:if>

</div>

<jsp:include page="template-footer.jsp" /> 

0 个答案:

没有答案