2

My controller is annotated as

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

The UploadFormBean is defined as:

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

Why is @mMdelAttribute not able to set type variable (t is null in execute function)?

What am I missing? Also, please explain how does @ModelAttribute binds the data members from http request to a java bean.

Note: this works fine in case when type is a String and setType is defined as:

void setType(final String type) {
    this.type = type;
}

JSP:

<input type="hidden" name="type" value="abc">
<input type="hidden" name="type" value="xyz">
Aman Deep Gautam
  • 8,091
  • 21
  • 74
  • 130
  • see if [this](http://stackoverflow.com/questions/4617099/spring-3-0-mvc-binding-enums-case-sensitive) helps – Bond - Java Bond Feb 06 '15 at 06:25
  • how looks your jsp/html? I think it's `null` because spring can not bind it because spring doesn't know this property. – Manu Zi Feb 06 '15 at 06:51
  • @ManuZi I am also guessing that spring is not able to bind it, but then since it was private member spring must be setting it with help of setter. I have kept the setter same in both cases(when type is enum and when it is string). Not sure if spring uses reflection. – Aman Deep Gautam Feb 06 '15 at 08:38
  • i image that you need something like this in your jsp/html `type.str` – Manu Zi Feb 06 '15 at 08:54
  • @ManuZi caould you elaborate more on this. – Aman Deep Gautam Feb 06 '15 at 09:01
  • before i can do that i need to see your jsp/html. you must have something like that to 'connect' your controller model with the client: `` (that's thymeleaf syntax) – Manu Zi Feb 06 '15 at 09:04
  • @ManuZi added JSP component as well. – Aman Deep Gautam Feb 06 '15 at 10:17
  • i'm wondering that 'submitType' would be worked. i think your setType method is wrong, have you try to remove this method? it would be better we use the so chat option – Manu Zi Feb 06 '15 at 12:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/70419/discussion-between-aman-deep-gautam-and-manu-zi). – Aman Deep Gautam Feb 06 '15 at 19:42

1 Answers1

0

1)Why is @mMdelAttribute not able to set type variable (t is null in execute function)?

Because for binding it will expect a setter like like one below.

public void setType(final Type type) {
  this.type = type) ;
}

But this wont work as path value in spring form,because enum will not have public constructor.

So better way to achieve this is to re factor your bean like one below

public class UploadFormBean {

    private String 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 String getType() {
        return type;
    }

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

    }
}
Renganathan V
  • 413
  • 1
  • 9
  • 27