0

你能@Initbinder在同一个表格中有几个数据吗?

我有一个弹簧表单,其中包含一个对象的选择下拉列表和两个数据字段,我在日期上有一个 Initbinder,否则我在提交时会出错。但我还需要将下拉列表绑定到一个对象。

我有一个类型,它有两个日期和一个Category,它是Category我需要绑定的,因为它在保存时不能为空。我认为这将帮助我验证表格。那么我可以在我的类型控制器中使用它吗?

@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));
}

这是编辑器:

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 "";
    }
}

还有我的jsp页面

<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>

所以我的问题是:initBinder我的控制器中可以有多个活页夹吗?好像我不能,因为我从来没有进入CategoryEditor. 我该怎么做呢?

4

1 回答 1

1

没有多个活页夹,有多个PropertyEditors.

您的自定义路径永远不会被调用,因为您绑定了错误的路径。

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

你必须绑定到category而不是category.ID

<sf:select path="category" id="category">
于 2014-03-06T09:32:11.870 回答