1
当我尝试提交表单时,出现以下验证错误。下拉框填充Sites.java中的值会导致此错误:java.lang.IllegalStateException:无法将[java.lang.String]类型的值转换为所需的类型
org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'model.TypeSites'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [model.TypeSites]: no matching editors or conversion strategy found
at org.springframework.beans.SimpleTypeConverter.convertIfNecessary(SimpleTypeConverter.java:58)
我的映射有问题吗?
Sites.java映射
public class Sites implements java.io.Serializable {
private int id;
@ManyToOne
@JoinColumn(name = "idTypeSite")
private TypeSites siteesTypeSite;
}
TypeSites.java映射:
public class TypeSites implements java.io.Serializable {
private int idTypeSite;
private String typeSite;
@OneToMany(mappedBy = "siteesTypeSite",fetch = FetchType.LAZY)
private Set<Sites> sitees= new HashSet<Sites>(0);
}
控制器类:
@Controller
@RequestMapping(value = "/protected/sites")
public class SitesController {
------
@RequestMapping(method = RequestMethod.POST, produces = "application/json")
public ResponseEntity<?> create(@ModelAttribute("site") Sites site,
@RequestParam(required = false) String searchFor,
@RequestParam(required = false,
defaultValue = DEFAULT_PAGE_DISPLAYED_TO_USER) int page,
Locale locale) {
siteService.save(site);
}
Angularjs代码:
$scope.createObject = function (newObjectForm) {
if (!newObjectForm.$valid) {
$scope.displayValidationError = true;
return;
}
$scope.lastAction = 'create';
var url = $scope.url;
var config = {headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}};
$scope.addSearchParametersIfNeeded(config, false);
$scope.startDialogAjaxRequest();
$http.post(url, $.param($scope.sites), config)
.success(function (data) {
$scope.finishAjaxCallOnSuccess(data, "#addObjectsModal", false);
})
.error(function(data, status, headers, config) {
$scope.handleErrorInDialogs(status);
});
};
JSP:
<select required
ng-model="sites.siteesTypeSite"
name="siteesTypeSite"
ng-change="getSelectedValue()"
value="{{sites.siteesTypeSite}}"
>
<option ng-repeat="typesites in page.source" value="{{typesites.idTypeSite}}" >{{typesites.typeSite}}</option>
</select>