Spring InitBinder,嵌套Bean列表的绑定日期

时间:2015-07-23 05:53:41

标签: spring spring-mvc spring-mvc-initbinders

我有Bean DealSheetForm,其中有ListOrderDto个bean作为其属性。 OrderDtos有4个日期字段。我使用@InitBinder将日期格式绑定为控制器上的MM/dd/yyyy。现在我需要将其中一个日期字段格式修改为MM/dd/yyyy hh:mm:ss

我尝试在CustomDateEditors中创建2 @InitBinder但在dateformat上没有任何更改。如果有人有可能的解决方案,请帮助我。

以下是包含OrdersDto Bean的DealSheetBean:

public class DealSheetForm {

    private CustomerDto customer=new CustomerDto();
    private AutoPopulatingList<OrdersDto> orders =new                            
               AutoPopulatingList<OrdersDto>(OrdersDto.class);      

    public AutoPopulatingList<OrdersDto> getOrders() {
      return orders;
    }

    public void setOrders(AutoPopulatingList<OrdersDto> orders) {
      this.orders = orders;
    }

    public CustomerDto getCustomer() {
        return customer;
    }

    public void setCustomer(CustomerDto customer) {
        this.customer = customer;
    }
}

这是我的控制器

@Controller
public class DealSheetController{

  @InitBinder
 public void initBinder(WebDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
    SimpleDateFormat dateTimeFormat = new SimpleDateFormat("MM/dd/yyyy  
       HH:mm:ss");
     dateFormat.setLenient(false);
     dateTimeFormat.setLenient(false);

    binder.registerCustomEditor(Date.class,new CustomDateEditor(dateFormat, 
     true));
    binder.registerCustomEditor(Date.class,"orderDate",new  
    CustomDateEditor(dateTimeFormat, true));     
   }


   @RequestMapping(value="/agent/dealsheet.do",method=RequestMethod.POST)
   @ResponseBody
    public String saveDealSheets(@ModelAttribute("dealSheet") DealSheetForm  
     dealSheet,Map<String,Object> map,HttpServletRequest  
      request,HttpServletResponse response){
        try{                
            log.info("inside saveDealSheeeeeets()");
            Authentication 
            auth=SecurityContextHolder.getContext().getAuthentication();                
            String user=auth.getName();     
            HoveyUserDto userDto=this.userService.getUserByUsername(user);
            String agentNumber=userDto.getAgentNumber();
            this.dealSheetService.saveDealSheetToDB(dealSheet,agentNumber);
            String message="Orders Saved Successfully";
            map.put("message", message);
            return "success";
        }           
        catch(Exception e){
            log.error(e.getStackTrace().toString());                
            return "error";
        }       
    }   }

0 个答案:

没有答案