【发布时间】:2015-06-08 09:00:23
【问题描述】:
我在两个实体TypeSite 和Site 之间有one-to-many 关系。在前端,我有一个表单(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 = '创建';
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>
【问题讨论】:
标签: json angularjs spring-mvc data-binding restful-url