我在两个实体one-to-many
和TypeSite
之间建立Site
关系。在前端我有一个表单(angularJS)用于创建一个新的站点,其中包含一堆字段和下拉列表,用户可以在其中选择此站点的类型。表单提交时没有Dropdown
,但是当我尝试使用组合框提交表单时,我收到以下错误:
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'site' on field 'siteesTypeSite': rejected value [Etatique]; codes [typeMismatch.site.siteesTypeSite,typeMismatch.siteesTypeSite,typeMismatch.model.TypeSites,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [site.siteesTypeSite,siteesTypeSite]; arguments []; default message [siteesTypeSite]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'model.TypeSites' for property 'siteesTypeSite'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [model.TypeSites] for property 'siteesTypeSite': no matching editors or conversion strategy found]
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:110)
型号:
public class Sites implements java.io.Serializable {
private int id;
private TypeSites siteesTypeSite;
@JsonBackReference("site-typeSite")
@ManyToOne
@JoinColumn(name = "idTypeSite")
public TypeSites getSiteesTypeSite() {
return siteesTypeSite;
}
public class TypeSites implements java.io.Serializable {
private int idTypeSite;
private Set<Sites> sitees= new HashSet<Sites>(0);
@JsonManagedReference("site-typeSite")
@OneToMany(mappedBy = "siteesTypeSite",fetch = FetchType.LAZY)
public Set<Sites> getSitees() {
return sitees;
}
存储库:
public interface SitesRepository extends PagingAndSortingRepository<Sites, Integer> {
Page<Sites> findBycodeGSMLike(Pageable pageable, String codeGSM);
}
服务类:
@Service
@Transactional
public class SitesService {
@Autowired
private SitesRepository siteRepository;
@Transactional(readOnly = true)
public void save(Sites site) {
siteRepository.save(site);
}
}
控制器类:
@Controller
@RequestMapping(value = "/protected/sites")
public class SitesController {
private static final String DEFAULT_PAGE_DISPLAYED_TO_USER = "0";
@RequestMapping(method = RequestMethod.GET)
public ModelAndView welcome() {
return new ModelAndView("sitesList");
}
@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);
if (isSearchActivated(searchFor)) {
return search(searchFor, page, locale, "message.create.success");
}
return createListAllResponse(page, locale, "message.create.success");
}
}
Angularjs代码: $ scope.createObject = function(newObjectForm){ if(!newObjectForm。$ valid){ $ scope.displayValidationError = true; 返回; } $ 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 ng-model="sites.siteesTypeSite"
name="siteesTypeSite"
ng-options="typesites as typesites.typeSite for typesites in page.source "
value="{{sites.siteesTypeSite}}" >
<option value="">-- Select Type site --</option>
<input type="hidden" name="siteesTypeSite" value="{{sites.siteesTypeSite}}" />
</select><br>
答案 0 :(得分:0)
您的错误在stacktrace中解释: 无法转换类型&#39; java.lang.String&#39;的属性值要求的类型&#39; .model.TypeSites&#39; for property&#39; siteesTypeSite&#39;;
Dropbox中的值是一个字符串,您尝试匹配TypeSites。
解决方案:
我认为你的TypeSites是一个枚举。如果是这种情况,只需创建一个函数,根据下拉列表返回的字符串查找匹配的值。
<select ng-model="sites.siteesTypeSite"
name="siteesTypeSiteName"
ng-options="typesites.typeSite as typesites.typeSite for typesites in page.source" >
<option value="">-- Select Type site --</option>
</select>
创建一个字段siteesTypeSiteName并像我一样更新您的Dropbox。然后使用该函数获取链接的TypeSites。