Spring MVC 类型转换 @InitBinder使用

 

    @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(SystemInfo.class, new PropertyEditorSupport() {

            @Override
            public void setAsText(String text) throws IllegalArgumentException {
                if (!StringUtils.hasText(text)) {
                    return;
                }
                {
                    Long systemInfoId = Long.valueOf(text);
                    SystemInfo systemInfo = systemInfoService.findById(systemInfoId);
                    setValue(systemInfo);
                }
            }
        });

        binder.registerCustomEditor(Category.class, new PropertyEditorSupport() {

            @Override
            public void setAsText(String text) throws IllegalArgumentException {
                if (!StringUtils.hasText(text)) {
                    return;
                } else {
                    Long categoryId = Long.valueOf(text);
                    Category category = categoryService.findById(categoryId);
                    setValue(category);
                }
            }
        });
    }

 

		            	<form:form modelAttribute="categoryEditForm" id="categoryForm" method="post" action="saveOrUpdate.do">
		            	
		            	<form:hidden path="category.objectId" />
		            	<input type="hidden" name="category.parent" value="${categoryEditForm.category.parent.objectId}"/>
		            	<input type="hidden" name="category.systemInfo" value="${categoryEditForm.category.systemInfo.objectId }"/>
		            	
		                <div class="area">
		                    <div class="areaTitle">
		                      	<div class="inner">
		                        	<label>Category Information Form</label>
		                        	<div class="clear"></div>
		                    	</div>
		                	</div>
		                </div>
		                
		                <div class="areaBody">
							<table class="formTable">
	                        	<tbody>
	                        		<tr>
										<td colspan="4">
											<span class="button"><span><a href="javascript:sumbit();" class="btnSave">Submit</a></span></span>
										</td>
									</tr>	
									<tr>
										<td colspan="4">&nbsp;</td>
									</tr>
									<tr>
										<td align="right">Parent Category Name:</td>
										<td colspan="3"><form:input path="category.parent.name.fullName" readonly="true" id="parentCategory" cssClass="input readOnly" /></td>										
									</tr>
									<tr>
										<td align="right">Current Category Name:</td>
										<td><form:input path="category.name.fullName" id="categoryName" cssClass="input"/></td>
										<td align="right">description:</td>
										<td><form:input path="category.description" id="description" cssClass="input"/></td>
									</tr>
	                        	</tbody>
                      		</table>
		                </div>
		                
						</form:form>
 

你可能感兴趣的:(spring mvc)