【问题标题】:submit dropdown angularjs spring MVC提交下拉angularjs spring MVC
【发布时间】:2015-06-08 09:00:23
【问题描述】:

我在两个实体TypeSiteSite 之间有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


    【解决方案1】:

    堆栈跟踪中解释了您的错误: 无法将类型“java.lang.String”的属性值转换为属性“siteesTypeSite”所需的类型“.model.TypeSites”;

    保管箱中的值是一个字符串,您正在尝试匹配一个 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 并像我一样更新您的保管箱。然后使用该函数获取链接的TypeSites。

    【讨论】:

    • No TypeSites 是我在 Sites 和 TypeSites 之间有 Many-to-one 的类,并且我有一个对象 private TypeSites siteesTypeSite;
    • 好吧,想法还是一样的:在你的列表中,你不能返回 TypeSites,但你可以返回字符串、整数……你的 TypeSites 应该在数据库中有一个 id,然后执行类似 typeSiteName 并使用函数通过其 id 获取 TypeSite
    • 现在我可以使用此ng-options="typesites as typesites.typeSite for typesites in page.source " 返回 TypeSites,但我收到此错误now Invalid property 'siteesTypeSite[idTypeSite]' of bean class [model.Sites]:Property referenced in indexed property path 'siteesTypeSite[idTypeSite]' is neither an array nor a List nor a Map; returned value was [8]。我已经编辑了我的帖子,我将图片放在了我的问题的最后,
    • 真的解决不了这个问题你可以再看看这个问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 1970-01-01
    • 1970-01-01
    • 2015-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多