开发者

How can I set a custom PropertyEditorSupport.setAsText(String) for spring's form select list?

I have a spring form. The form contains a select list that is bound to an enum type. I have also added a "choose" option to the select list that isn't represented in the enum.

I have seen some threads online where the people suggest using a PropertyEditor to avoid having to modify the enum and avoid spring from throwing conversion errors between the "choose" option and the enum (the user can leave the option as choose - it's optional in some cases)

I have created a PropertyEditor and have tried it in two scenarios. If I try using it with a select list (as outlined below) the form will not load and spring will throw errors: No enum const class com.mytest.domain.Box.Choose. If I don't include my custom editor, the form loads fine and the only problem is when the form is submitted, the errors object will contain errors complaining about a failed conversion from Java.lang.String to BoxType. If I switch the form to form:input instead of form:select, then the form will load, and will only throw errors if I submit after typing in a value that is not one of the Enum values. If I put in a valid value, I can see that my BoxEditor.setAsText() is not being called. BoxEditor.getAsText() is being called when the form loads (monkey brains is the value in the input field). I'm at a loss why the get method is called, but not the set. Any help would be greatly appreciated.

I have defined an Enum:

public enum BoxType {
    SMALL,MEDIUM,LARGE
}

And a Domain object:

public class BoxRequest {
    private BoxType box;

    public BoxType getBox() {
        return box;
    }

    public void setBox(BoxType b) {
        this.box = b;
    }
}

I have created a jsp using jstl and spring's form tagilb

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>

<html>
<head>
<title>Box Form</title>
</head>
<body> 
<form:form id="boxrequest" method="post" modelAttribute="boxrequest">
    <form:label path="box">Choose a box size*</form:label>

       <form:input path="box"/>

        <!-- really want the form to use this once I get it working
    <form:select multiple="single" path="box">
        <form:option value="Choose"/>
        <form:options/>
    </form:select>
        -->

</form:form>
</body>
</html>

My PropertyEditor

public class BoxTypeEditor extends PropertyEditorSupport {

    @Override
    public void setAsText(String s) {
        System.out.println("This output is never printed :( ");
        if(StringUtils.hasText(s))
        开发者_运维问答    setValue(Enum.valueOf(BoxType.class, s));
        else
            setValue(null);
    }

    @Override
    public String getAsText() {
        System.out.println("this output shows when the form loads");
        if(getValue() == null) {        
            return "monkeybrains";
        }

        BoxType b = (BoxType) getValue();
        return b.toString();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜