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