spring mvc - InitBinder on multiple form objects -


can have @initbinder on several data in same form?

i have spring form contains select dropdown of object , 2 datafields, have initbinder on dates otherwise error on submit. need bind dropdown object.

i have type has 2 dates , category, , category need bind because can not empty on save. think me validate form to. can have in type controller?

@initbinder     public void initbinder(webdatabinder binder) {         simpledateformat dateformat = new simpledateformat("yyyy-mm-dd");         dateformat.setlenient(false);         binder.registercustomeditor(date.class, new customdateeditor(dateformat, true));         binder.registercustomeditor(category.class, "category", new categoryeditor(categoryservice)); } 

and editor:

public class categoryeditor extends propertyeditorsupport {      private categoryservice categoryservice;      public categoryeditor(categoryservice categoryservice) {         this.categoryservice = categoryservice;     }      @override     public void setastext(string text) throws illegalargumentexception {         if (text.equals("0")) {             this.setvalue(null);         } else {             category sc = categoryservice.getcategory(integer.parseint(text));             this.setvalue(sc);         }     }      @override     public string getastext() {         category parent = new category();         if (this.getvalue() != null) {             parent = (category) this.getvalue();         }         return "";     } } 

and jsp-page

<s:url value="/mvc/type/save" var="actionurl" /> <sf:form method="post" modelattribute="type" action="${actionurl}">     <fieldset>         <legend><s:message code="${heading}" /></legend>         <table>             <tr>                 <th><label for="category"><s:message code="category" />:</label></th>                 <td><sf:select path="category.id" id="category">                         <sf:option value="0">&nbsp;</sf:option>                         <sf:options items="${listofcategories}" itemlabel="name" itemvalue="id" />                     </sf:select></td>             </tr>             <tr>                 <th><label for="name"><s:message code="name" />:</label></th>                 <td><sf:input path="name" id="name" />                     <sf:hidden path="id" />                     <sf:hidden path="version" /></td>             </tr>             <tr>                 <th><label for="marketing"><s:message code="marketing" />:</label></th>                 <td><sf:input path="marketingfunction" id="marketing" /></td>             </tr>             <tr>                 <th><label for="status"><s:message code="status" />:</label></th>                 <td><sf:select path="lifecyclestatus">                         <sf:option value="0">&nbsp;</sf:option>                         <sf:options items="${listofenums}" />                     </sf:select></td>             </tr>             <tr>                 <th><label for="validfrom"><s:message code="validfrom" />:</label></th>                 <td><sf:input path="validfrom" id="validfrom" /></td>             </tr>             <tr>                 <th><label for="validuntil"><s:message code="validuntil" />:</label></th>                 <td><sf:input path="validuntil" d="validuntil" /></td>             </tr>             <tr>                 <td colspan="2">                     <input id="savebutton" class="right" type="submit" title="<s:message code="save" />" value=" [ <s:message code="save" /> ] " />                 </td>             </tr>         </table>     </fieldset> </sf:form> 

so question: can have multiple binder in same initbinder in controller? seems can't because never enter categoryeditor. how do this?

there aren't multiple binders, there multiple propertyeditors.

your custom 1 never called because binding wrong path.

<sf:select path="category.id" id="category"> 

you must bind category , not category.id

<sf:select path="category" id="category"> 

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -