Ramandeep S
Ramandeep S

Reputation: 345

Binding CustomDateEditor spring Mvc

I am working on Spring MVC with annotations. I havent registered any customDateEditory

In My Class i have one Property of type java.lang.Date

What i read was that customDateEditor are not registered by default. But when i submit my form with blank date it throws illegalArgumentException

If i enter date and then submit it doesnt throws exception

THen after that i added BindingResult as argument to controller method now it is accepting blank values

I have not added any initbinder to my Controller

Controller

   @Controller("orderController")
   @RequestMapping(value = "/admin")
    public class OrderController extends BaseController {



    @RequestMapping(value = "/viewOrders.htm", method = RequestMethod.GET)
    public String viewOrders(@ModelAttribute("searchOrder") SearchOrder       searchOrder,BindingResult bindingResult,Model model) {

        List<Order> orders=orderService.getOrders(searchOrder);
        model.addAttribute("orders", orders);

        return "order/searchOrder";
    }
    }

Pojo Containing Date Field

public class SearchOrder {

    private Integer orderId;
    private Long customerId;
    private String customerPo;
    private Date dateCreatedFrom;
    private Date dateCreatedTo;
    private Date dateUpdatedFrom;
    private Date dateUpdatedTo;


    public Integer getOrderId() {
        return orderId;
    }

    public void setOrderId(Integer orderId) {
        this.orderId = orderId;
    }

    public Long getCustomerId() {
        return customerId;
    }

    public void setCustomerId(Long customerId) {
        this.customerId = customerId;
    }

    public String getCustomerPo() {
        return customerPo;
    }

    public void setCustomerPo(String customerPo) {
        this.customerPo = customerPo;
    }

    public Date getDateCreatedFrom() {
        return dateCreatedFrom;
    }

    public void setDateCreatedFrom(Date dateCreatedFrom) {
        this.dateCreatedFrom = dateCreatedFrom;
    }

    public Date getDateCreatedTo() {
        return dateCreatedTo;
    }

  public void setDateCreatedTo(Date dateCreatedTo) {
        this.dateCreatedTo = dateCreatedTo;
    } 

    public Date getDateUpdatedFrom() {
        return dateUpdatedFrom;
    }

    public void setDateUpdatedFrom(Date dateUpdatedFrom) {
        this.dateUpdatedFrom = dateUpdatedFrom;
    }

    public Date getDateUpdatedTo() {
        return dateUpdatedTo;
    }

    public void setDateUpdatedTo(Date dateUpdatedTo) {
        this.dateUpdatedTo = dateUpdatedTo;
    }

@Override
    public String toString() {
        return "SearchOrder [orderId=" + orderId + ", customerId=" + customerId
                + ", customerPo=" + customerPo + ", dateCreatedFrom="
            + dateCreatedFrom + ", dateCreatedTo=" + dateCreatedTo
                + ", dateUpdatedFrom=" + dateUpdatedFrom + ", dateUpdatedTo="
            + dateUpdatedTo + "]";
    } 


 }

Please throw some light

Upvotes: 0

Views: 294

Answers (1)

Ralph
Ralph

Reputation: 120871

When you add a BindingResult than this binding result contains the information about the binding and validation that does not "work". You can use BindingResult.hasErrors() to check if the binding result contains information about the "problem".

If you do not have a BindingResult method parameter, then Spring throw autoamticaly an exception if there is a binding error.

Add

 for(ObjectError objectError : bindingResult.getAllErrors()) {
       Sysout.println("error: " + objectError);
 }

to get some information about your binding error


(Btw: the method parameter order is important: the BindingResult parameter must be the next parameter after the Command object that should be checked with this BindingResult)

Upvotes: 1

Related Questions