How to avoid 400 error in spring mvc when binding as java Bean -


i'm using spring mvc. in controller, want parameter java bean order. order bean has several parameters, 1 of them duedate (java.util.date).

@requestmapping("/toaddorder") public modelandview addorder(order order, bindingresult bindingresult){     return new modelandview("redirect:tovieworder"); }  @initbinder protected void initbinder(         webdatabinder binder) throws servletexception {    binder.registercustomeditor(byte[].class,             new bytearraymultipartfileeditor());      simpledateformat dateformat = new simpledateformat("yyyy-mm-dd");             dateformat.setlenient(false);             binder.registercustomeditor(date.class, new customdateeditor(dateformat, false)); } 

problem if not set value duedate before submiting form controller. meet error. bad_request, because duedate null or "".

so, want know, how avoid problem? solutions can found follows. 1. js check before submit form. 2. not binding order, parameters httpservletrequest request

extra questions found last 2 solutions. if order bean has many parameters, need write long code , set value, may add load me in future.

i have solved problem. because i'm new spring mvc. tried several methods. think follow methods may if meet same problems.

add in controller

@initbinder protected void initbinder( webdatabinder binder) throws servletexception {             simpledateformat dateformat = new simpledateformat("yyyy-mm-dd");             dateformat.setlenient(false);             binder.registercustomeditor(date.class, new customdateeditor(dateformat, false)); } 

but meet new questions.

if submit form in value "". error.code typemismatch.

i configed in message.properties file

#bean level error message typemismatch.order.duedate = order.duedate format not match "yyyy-mm-dd"  #global type not match message typemismatch = data type not match 

but when print error message in head of page, code typemismatch not typemismatch.order.duedate.

<#if error?has_content>     <#list error.allerrors item>        <@spring.message "${item.code?if_exists}"/><br/>     </#list> </#if> 

anyone knows how solve questions?


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -