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> 

回答

0

IWAS解決這個錯誤這樣的: 我有我的edite角功能:

$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); 
      }); 
    }; 
相關問題