java.lang.IllegalStateException:无法将[java.lang.String]类型的值转换为所需类型

时间:2015-06-11 14:14:35

标签: angularjs spring-mvc angularjs-scope angularjs-ng-repeat ng-repeat

当我尝试提交表单时,我收到以下验证错误。使用来自Sites.java的值填充的下拉框会出现此错误:

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>

1 个答案:

答案 0 :(得分:0)

我解决了这个错误: 我编辑了我的角度函数:

$scope.createObject = function (newObjectForm) {
        if (!newObjectForm.$valid) {
            $scope.displayValidationError = true;
            return;
        }
        $scope.lastAction = 'create';
        var url = $scope.url;
        console.debug(url);
        var config = {headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}};
        $scope.addSearchParametersIfNeeded(config, false);
        $scope.startDialogAjaxRequest();
        $scope.sites.siteesTypeSite =JSON.parse($scope.sites.siteesTypeSite);
        $http.post(url, $.param($scope.sites), config)
            .success(function (data) {
                console.debug(data);
                $scope.finishAjaxCallOnSuccess(data, "#addObjectsModal", false);
            })
            .error(function(data, status, headers, config) {
                $scope.handleErrorInDialogs(status);
            });
    };