Ciao a tutti.
Sono un novizio di Spring.
Sto cercando di validare un form che ha un campo data e vorrei che tale campo data abbia il pattern dd/MM/yyyy.
Sembra che non riesca a far partire il formatter.

questo è il mio xml di configurazione.

codice:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">


    <context:component-scan base-package="it/filippo/agendaservletspring/controller"/>
    <context:component-scan base-package="it/filippo/agendaservletspring/service"/>
    <context:component-scan base-package="it/filippo/agendaservletspring/formatter"/>
    
    <mvc:annotation-driven conversion-service="conversionService"/>
    
    <mvc:default-servlet-handler />
    <mvc:resources mapping="/css/**" location="/css/"/>
    <mvc:resources mapping="/*.html" location="/"/>
    
    
 
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>
    
    <bean id="messageSource" 
          class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:validator/messages" />
    </bean>
    
    <bean id="taskValidator" 
          class="it.filippo.agendaservletspring.validator.TaskValidator" />


    <bean id="conversionService"
         class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
         <property name="formatters">
             <set>
                 <bean class="it.filippo.agendaservletspring.formatter.FormatterData">
                     <constructor-arg type="java.lang.String" value="dd/MM/yyyy" />
                 </bean>
             </set>
         </property>
    </bean>
    
    
</beans>
la validazione funziona, perchè se lascio i campi vuoti e faccio submit, mi viene restituito il form con i relativi messaggi di campo vuoto,

Se invece metto una data diversa dal formato consentito, oppure metto una stringa nella data, mi viene fuori la pagina The request sent by the client was syntactically incorrect.
Suppongo che non venga proprio avviato il formatter perchè pare non prenda l'eccezione.


il mio formatter è

codice:
/* * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.filippo.agendaservletspring.formatter;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import org.springframework.format.Formatter;


/**
 *
 * @author filippo
 */
public class FormatterData implements Formatter <Date> {
    private String datePattern;
    private SimpleDateFormat dateFormat;


    public FormatterData(String datePattern) {
        this.datePattern = datePattern;
        dateFormat = new SimpleDateFormat(datePattern);
        dateFormat.setLenient(false);
    }


    @Override
    public String print(Date date, Locale locale) {
        return dateFormat.format(date);
    }


    @Override
    public Date parse(String s, Locale locale) throws ParseException {
    	
    	if(s.length()!=10){
    		throw new IllegalArgumentException(
                    "Data non valida. Inserire con il pattern\""
                            + datePattern + "\"");
    	}
        try {
            return dateFormat.parse(s);
        } catch (ParseException e) {
            // the error message will be displayed when using <form:errors>
            throw new IllegalArgumentException(
            		"Data non valida. Inserire con il pattern\""
                            + datePattern + "\"");
        }
    }
}


e questo è il mio controller.

codice:
/* * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.filippo.agendaservletspring.controller;


import it.filippo.agendaservletspring.beans.Categoria;
import it.filippo.agendaservletspring.beans.Task;
import it.filippo.agendaservletspring.formatter.FormatterData;
import it.filippo.agendaservletspring.service.TaskService;
import it.filippo.agendaservletspring.validator.TaskValidator;
import java.util.ArrayList;
import java.util.List;
import javax.validation.Valid;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
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.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;


/**
 *
 * @author filippo
 */
@Controller
@RequestMapping("/task")
@SessionAttributes("tasks")
public class AgendaController {


    @RequestMapping(value = {"", "lista"})
    public String listaTask() {
        return "task/lista";
    }


    @Autowired
    private TaskService taskService;


    @Autowired
    private TaskValidator taskValidator;


    private static final Log logger = LogFactory.getLog(AgendaController.class);


    @RequestMapping(value = "crea")
    public String inputTask(Model model) {
        List<Categoria> categorie = taskService.prendiCategorie();
        model.addAttribute("categorie", categorie);
        model.addAttribute("taskBean", new Task());
        return "task/creaTask";
    }


    @RequestMapping(value = "save", method = RequestMethod.POST)
    public String saveTask(@Valid @ModelAttribute("taskBean") Task task, Model model, RedirectAttributes redirectAttributes, BindingResult bindingResult) {
        System.out.println("task: " + task);
        List tasks;
        if (!model.containsAttribute("tasks")) {
            tasks = new ArrayList<>();
            model.addAttribute("tasks", tasks);
        } else {
            tasks = (List) model.asMap().get("tasks");
        }
        logger.info("save");
        
        taskValidator.validate(task, bindingResult);


        if (bindingResult.hasErrors()) {
            logger.info("save");
            Categoria categoria = taskService.getCategory(task.getCategoria().getId());
            List<Categoria> categorie = taskService.prendiCategorie();
            task.setCategoria(categoria);
            model.addAttribute("categorie", categorie);
            model.addAttribute("message", "Problemi nell'inserimento");
            return "task/creaTask";
        } else {
            Categoria categoria = taskService.getCategory(task.getCategoria().getId());
            task.setCategoria(categoria);
            taskService.save(task, tasks);
            System.out.println(task);
            redirectAttributes.addFlashAttribute("message", "Prodotto aggiunto con successo");
            return "redirect:lista";
        }


    }


    @RequestMapping(value = "task_modifica")
    public String editTask(Model model, @RequestParam("id") int id) {
        List<Categoria> categorie = taskService.prendiCategorie();
        model.addAttribute("categorie", categorie);
        List tasks;
        tasks = (List) model.asMap().get("tasks");
        Task task = taskService.get(id, tasks);
        model.addAttribute("taskBean", task);
        return "task/task_edit_form";
    }


    @RequestMapping(value = "task_aggiorna", method = RequestMethod.POST)
    public String updateTask(@ModelAttribute("taskBean") Task task, RedirectAttributes redirectAttributes, Model model) {
        Categoria categoria = taskService.getCategory(task.getCategoria().getId());
        task.setCategoria(categoria);
        System.out.println("task: " + task);
        List<Task> tasks = (List) model.asMap().get("tasks");


        taskService.update(task, tasks);
        redirectAttributes.addFlashAttribute("message", "Modifica Effettuata con successo");
        return "redirect:lista";
    }


}


Grazie in anticipo.