0

I have a form in JSP which is to be submitted to a controller. Now one of the attributes of the form is a enum, but I cannot get this to work.

public class UploadFormBean {
    private Type type;
    public enum Type {
        A ("abc"),

        B ("xyz"),

        C ("pqr");

        private final String str;

        private Type(final String str) {
            this.str = str;
        }

        @Override
        public String toString() {
            return str;
        }
    }

    public Type getType() {
        return type;
    }

    public void setType(final String type) {
        for (Type s: Type.values()) {
            if (s.toString().equalsIgnoreCase(type)) {
                this.type = s;
            }
        }
    }
}

Controller:

public ModelAndView execute(final HttpServletRequest request, @ModelAttribute final UploadFormBean uploadFormBean) {
    //some code.
    Type t = uploadFormBean.getType();  //t is null.
    //some more code.
}

JSP:

<input type="hidden" name="type" value="abc">

What am I missing. Let me know if any information is needed. Thanks for all help.

3
  • post your UploadFormBean class code?
    – dReAmEr
    Feb 6, 2015 at 20:43
  • Where is the enum? Where is it used in the JSP? Where is it used in the controller? When you say "cannot get it to work", what do you mean? What do you expect to happen? What actually happens? And if any of the "some code" parts are changing or setting up anything relevant to the question, they should be posted. Feb 6, 2015 at 20:50
  • @RE350 added relevant code. Feb 7, 2015 at 6:04

2 Answers 2

2

The problem is that the default Spring MVC property editor cannot determine how to map abc submitted in the HTTP request to Type.A. Its default behaviour with respect to enums is to translate the string value into an enumeration. Since it cannot find an enumeration value called abc, it bails out with a null.

You can overcome this by writing your own PropertyEditor.

public class TypeEditor extends java.beans.PropertyEditorSupport {
  @Override
  public void setAsText(String text) throws IllegalArgumentException {
    if(text != null) {
      text = text.trim();
      if(text.equalsIgnoreCase("abc")) {
        setValue(Type.A);
      }
      else if(text.equalsIgnoreCase("xyz")) {
        setValue(Type.B);
      }
      else if(text.equalsIgnoreCase("pqr")) {
        setValue(Type.C);
      }
    }
  }
}

It may be better to create a method in the enum class Type to get a Type value from a java.lang.String. The property editor code will get simplified after that and any changes to enumeration values will be localized to the Type class.

Then, add this editor to the web binder in your controller class.

@Controller
public class MyController {
  @InitBinder
  public void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(Type.class, new TypeEditor());
  }
}
0

You can not map Enum directly, Change your form bean type to

private String type;

it will work

1
  • Please see the edit and see if anything changes. Let me know why this is not possible as well. Any docs would help. Feb 7, 2015 at 6:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.