Spring MVC conversion HOW TO

前端 未结 2 2019
长情又很酷
长情又很酷 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

    Thymeleaf compares values (for inclusion of selected="selected" tag in option html) using spring frameworks SelectedValueComparator.isSelected which inherently depends upon java equality first. If that fails, it falls back to String representation of both the values. Following is excerpt from it's documentation


    Utility class for testing whether a candidate value matches a data bound value. Eagerly attempts to prove a comparison through a number of avenues to deal with issues such as instance inequality, logical (String-representation-based) equality and PropertyEditor-based comparison.
    Full support is provided for comparing arrays, Collections and Maps.
    Equality Contract
    For single-valued objects equality is first tested using standard Java equality. As such, user code should endeavour to implement Object.equals to speed up the comparison process. If Object.equals returns false then an attempt is made at an exhaustive comparison with the aim being to prove equality rather than disprove it.
    Next, an attempt is made to compare the String representations of both the candidate and bound values. This may result in true in a number of cases due to the fact both values will be represented as Strings when shown to the user.
    Next, if the candidate value is a String, an attempt is made to compare the bound value to result of applying the corresponding PropertyEditor to the candidate. This comparison may be executed twice, once against the direct String instances, and then against the String representations if the first comparison results in false.


    For your specific case, I'd write down conversion service so that my part object is converted to string as described for VarietyFormatter in http://www.thymeleaf.org/doc/html/Thymeleaf-Spring3.html#configuring-a-conversion-service . Post this I'd use th:value="${part}" and let SelectedValueComparator do it's magic of comparing the objects and add selected="selected" part in the html.

    Also in my design, I always implement equals method based on primary key (usually I do it at my top level abstract entity from which all other entities inherit). That further strengths the natural comparison of domain objects in my system throughout. Are you doing something similar in your design?

    Hope it helps!!

    0 讨论(0)
  • 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<Long> selectedPartsLongSet = selectedParts.stream().map(Part::getId).collect(Collectors.toSet);
    model.addAttribute("selectedPartsLongSet", selectedPartsLongSet);
    

    In ui:

    <select class="form-control" id="parts" name="parts" multiple="multiple" >
                <option th:each="part : ${partsAtribute}" 
                        th:selected="${selectedPartsLongSet.contains(part.id)}"
                        th:value="${part.id}"
                        th:text="${part.name}">Part name and serial No.</option>
                </select>
    
    0 讨论(0)
提交回复
热议问题