Spring @ModelAttribute, http post parameter conversion fail
I'm getting the below error when trying to submit html option selection form, the selected option value is not converted to the respected enum type.
Failed to convert property value of type 'java.lang.String' to required type 'net.mis.lookup.model.PortalLayoutLocation' for property 'portalLayoutLocation'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [net.mis.lookup.model.PortalLayoutLocation] for property 'portalLayoutLocation': no matching editors or conversion strategy found
<select name="portalLayoutLocation" id="portalLa开发者_如何转开发youtLocation">
<option value="">Choose a PortalLayoutLocation</option>
<option value="1">PortalLayoutLocation.HEADER_MAIN_LOGO</option>
<option value="2">PortalLayoutLocation.HOMEPAGE_TECHNICAL_TIP_OF_THE_DAY</option
<option value="3">PortalLayoutLocation.HOMEPAGE_CARTOON_OF_THE_DAY</option>
<option value="4">PortalLayoutLocation.FOOTER_LOGO</option>
<option value="5">PortalLayoutLocation.PORTAL_HEADER_MAIN_ADVERTISEMENT</option>
<option value="6">PortalLayoutLocation.PORTALHOMELAYOUT_HOMECONTENT_ADVERTISEMENT</option>
<option value="7">PortalLayoutLocation.PORTALHOMELAYOUT_WIDESIDEBAR_ADVERTISEMENT</option>
<option value="8">PortalLayoutLocation.PORTALLAYOUT_NARROWSIDEBAR_ADVERTISEMENT</option>
</select>
public enum PortalLayoutLocation {
HEADER_MAIN_LOGO(1, "PortalLayoutLocation.HEADER_MAIN_LOGO"),
HOMEPAGE_TECHNICAL_TIP_OF_THE_DAY(2, "PortalLayoutLocation.HOMEPAGE_TECHNICAL_TIP_OF_THE_DAY"),
HOMEPAGE_CARTOON_OF_THE_DAY(3, "PortalLayoutLocation.HOMEPAGE_CARTOON_OF_THE_DAY"),
FOOTER_LOGO(4, "PortalLayoutLocation.FOOTER_LOGO"),
PORTAL_HEADER_MAIN_ADVERTISEMENT(5, "PortalLayoutLocation.PORTAL_HEADER_MAIN_ADVERTISEMENT"),
PORTALHOMELAYOUT_HOMECONTENT_ADVERTISEMENT(6, "PortalLayoutLocation.PORTALHOMELAYOUT_HOMECONTENT_ADVERTISEMENT"),
PORTALHOMELAYOUT_WIDESIDEBAR_ADVERTISEMENT(7, "PortalLayoutLocation.PORTALHOMELAYOUT_WIDESIDEBAR_ADVERTISEMENT"),
PORTALLAYOUT_NARROWSIDEBAR_ADVERTISEMENT(8, "PortalLayoutLocation.PORTALLAYOUT_NARROWSIDEBAR_ADVERTISEMENT");
private int typeCode;
private String messageKey;
private PortalLayoutLocation(int typeCode, String messageKey) {
this.typeCode = typeCode;
this.messageKey = messageKey;
}
public int toInt() {
return typeCode;
}
public static PortalLayoutLocation fromInt(int value) {
switch (value) {
case 0:
return null;
case 1:
return HEADER_MAIN_LOGO;
case 2:
return HOMEPAGE_TECHNICAL_TIP_OF_THE_DAY;
case 3:
return HOMEPAGE_CARTOON_OF_THE_DAY;
case 4:
return FOOTER_LOGO;
case 5:
return PORTAL_HEADER_MAIN_ADVERTISEMENT;
case 6:
return PORTALHOMELAYOUT_HOMECONTENT_ADVERTISEMENT;
case 7:
return PORTALHOMELAYOUT_WIDESIDEBAR_ADVERTISEMENT;
case 8:
return PORTALLAYOUT_NARROWSIDEBAR_ADVERTISEMENT;
default:
throw new UnsupportedOperationException("Unexpected enum value : " + value);
}
}
public int getTypeCode() {
return typeCode;
}
public String getMessageKey() {
return messageKey;
}
public String getLocalozedMessage() {
return StaticMessageSource.getMessage(messageKey);
}
@Override
public String toString() {
return getLocalozedMessage();
}
}
Use HEADER_MAIN_LOGO
, HOMEPAGE_TECHNICAL_TIP_OF_THE_DAY
, ... for the values, instead of 1, 2, ...
精彩评论