Announcement Announcement Module
Collapse
No announcement yet.
@InitBinder doesn't call. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @InitBinder doesn't call.

    Hi All,

    I'm using Spring MVC with Annotations.

    The code is a MultiController and I need to bind a Date.

    @Controller
    public class GroupController {

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

    @RequestMapping("/addGroup.do")
    public ModelMap addGroup(@RequestParam("name") String name, @RequestParam("startDate") Date startDate) {
    ....
    }
    }

    - I'm not using Web Flow and View, coz return only json values.
    - I'm using java.util.Date (not sql).

    This solutions seems equals to the Documentation but never call @InitBinding so never bind the startDate.

    Any sugestion?
    thx in advance.
    icon77.-

  • #2
    InitBinder will only be called in combination with the @ModelAttribute.

    Comment


    • #3
      Now the @InitBinder is called, but...

      mdeinum,

      Thks for your response, I tried using @ModelAttribute and worked fine, but I have a strange problem, the returned date seems to be alway the date of today.

      Code:
          @InitBinder
          public void initBinder(WebDataBinder binder) {
              SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yy");
              dateFormat.setLenient(false);
              binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
          } 
      
          @RequestMapping(value = "/addUser.do", method = RequestMethod.POST)
          public ModelAndView addUser(
                  @RequestParam(value = "name") String name, 
                  @RequestParam(value = "password") String password, 
                  @ModelAttribute(value = "startDate") Date startDate, 
                  @RequestParam(value = "active") Boolean active) {
                      
              log.info("Se esta guardando: " + name + " con fecha: " + startDate);
              
              User user = new User();
              user.setName(name);
              user.setPassword(password);
              user.setStartDate(startDate);
              user.setActive(active);
      For example we receive this 12/12/07 date from web, and then when passes the @InitBinder, the startDate is 01/05/08 (today).

      Any idea?

      Comment

      Working...
      X