【问题标题】:InitBinder on multiple form objects多个表单对象上的 InitBinder
【发布时间】:2014-03-06 09:26:45
【问题描述】:

你能在同一个表单中的多个数据上使用@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。我该怎么做?

【问题讨论】:

    标签: spring-mvc spring-mvc-initbinders


    【解决方案1】:

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

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

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

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

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

    【讨论】:

    • 啊,谢谢! :) 现在我的编辑器被调用了。但是现在选择了 dropown 中的最后一个元素,如果我查看源代码,下拉列表中的所有元素都有“selected=selected”。是否可以只选择顶部元素?
    • 您的CategoryEditor 有缺陷。你的 getAsText 总是返回 "" 而你可能应该返回 id。
    • 我想我使用了 path=category.ID 因为我使用相同的表单来编辑类型。如果我不使用 path=category.ID,则在我选择编辑类型时不会选择类别。所以现在这行不通了。
    • 您正在编辑category,因此您需要修复它。如果您使用 ID,您可能会遇到一个类别突然获得不同 ID 或一个被另一个覆盖的情况。您当前的问题主要是由于CategoryEditor 有缺陷/错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-01
    • 1970-01-01
    • 2014-10-17
    • 1970-01-01
    • 2017-06-10
    • 1970-01-01
    相关资源
    最近更新 更多