Spring MVC conversion HOW TO

前端 未结 2 2040
长情又很酷
长情又很酷 2021-01-17 06:29

I have vehicle service that, among other has list of parts. Adding new service is not a problem, viewing of service is not a problem, but when I try to implement edit, it do

2条回答
  •  渐次进展
    2021-01-17 07:00

    I was searching about another thing and just came across to this post, thought to share a practical and much simpler solution to this issue.

    Sometimes being drowned in technologies don't let us think out of the box.

    For this one, instead going through all the definitions of converters or formaters, we can simply convert objects set (In this case parts) to string or primitives set inside action method and then add it to the model.

    Then inside template just simply check if the set contains any option value:

    //In edit action:
    Set selectedPartsLongSet = selectedParts.stream().map(Part::getId).collect(Collectors.toSet);
    model.addAttribute("selectedPartsLongSet", selectedPartsLongSet);
    

    In ui:

    
    

提交回复
热议问题