@InitBinder使用

  1. @InitBinder  
  2. public void initBinder(WebDataBinder binder) {  
  3.   
  4.   
  5.     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
  6.     dateFormat.setLenient(false);  
  7.     binder.registerCustomEditor(Date.classnew CustomDateEditor(dateFormat, true));  
  8.   
  9.   
  10.     binder.registerCustomEditor(SystemInfo.classnew PropertyEditorSupport() {  
  11.   
  12.         @Override  
  13.         public void setAsText(String text) throws IllegalArgumentException {  
  14.             if (!StringUtils.hasText(text)) {  
  15.                 return;  
  16.             }  
  17.             {  
  18.                 Long systemInfoId = Long.valueOf(text);  
  19.                 SystemInfo systemInfo = systemInfoService.findById(systemInfoId);  
  20.                 setValue(systemInfo);  
  21.             }  
  22.         }  
  23.     });  
  24.   
  25.     binder.registerCustomEditor(Category.classnew PropertyEditorSupport() {  
  26.   
  27.         @Override  
  28.         public void setAsText(String text) throws IllegalArgumentException {  
  29.             if (!StringUtils.hasText(text)) {  
  30.                 return;  
  31.             } else {  
  32.                 Long categoryId = Long.valueOf(text);  
  33.                 Category category = categoryService.findById(categoryId);  
  34.                 setValue(category);  
  35.             }  
  36.         }  
  37.     });  
  38. }  

 

 

Html代码    收藏代码
  1.             <form:form modelAttribute="categoryEditForm" id="categoryForm" method="post" action="saveOrUpdate.do">  
  2.               
  3.             <form:hidden path="category.objectId" />  
  4.             <input type="hidden" name="category.parent" value="${categoryEditForm.category.parent.objectId}"/>  
  5.             <input type="hidden" name="category.systemInfo" value="${categoryEditForm.category.systemInfo.objectId }"/>  
  6.               
  7.             <div class="area">  
  8.                 <div class="areaTitle">  
  9.                     <div class="inner">  
  10.                         <label>Category Information Form</label>  
  11.                         <div class="clear"></div>  
  12.                     </div>  
  13.                 </div>  
  14.             </div>  
  15.               
  16.             <div class="areaBody">  
  17.     <table class="formTable">  
  18.                     <tbody>  
  19.                         <tr>  
  20.                 <td colspan="4">  
  21.                     <span class="button"><span><a href="javascript:sumbit();" class="btnSave">Submit</a></span></span>  
  22.                 </td>  
  23.             </tr>   
  24.             <tr>  
  25.                 <td colspan="4">&nbsp;</td>  
  26.             </tr>  
  27.             <tr>  
  28.                 <td align="right">Parent Category Name:</td>  
  29.                 <td colspan="3"><form:input path="category.parent.name.fullName" readonly="true" id="parentCategory" cssClass="input readOnly" /></td>                                          
  30.             </tr>  
  31.             <tr>  
  32.                 <td align="right">Current Category Name:</td>  
  33.                 <td><form:input path="category.name.fullName" id="categoryName" cssClass="input"/></td>  
  34.                 <td align="right">description:</td>  
  35.                 <td><form:input path="category.description" id="description" cssClass="input"/></td>  
  36.             </tr>  
  37.                     </tbody>  
  38.                         </table>  
  39.             </div>  
  40.               
  41. </form:form>  

你可能感兴趣的:(InitBinder)