Spring MVC binding issue

2019-08-15 17:49发布

问题:

In my spring mvc application, I have levels that users can create. With these levels, there are various requirements that a level needs in order to be taken (need a car, a phone, etc).

When creating a new level, the user can see a list of all these requirements and move those requirements into the required requirements area (by clicking on them to move them back and forth from one div to another). It would look a little something like this jsp

<div id="allRequirements">
    <c:forEach var="requirement" items="${RequirementList}">
         <div class="requirements">
             <input type="hidden" value="${requirement.id}" name="id"/>
             <h2><c:out value="${requirement.name}"/></h2>
         </div>
    </c:foreach>
</div>
<div id="requiredRequirements"></div>

The RequirementList is just a model attribute that returns a list of requirements

The model for the level and requirement look like this:

public class Level {
    private String name;
    private int id;
    private int points
    private List<Requirement> requirements;
    ....
}

public class Requirement{
    private String name;
    private String id;
    ....
}

and the method for this add functionality in the controller looks like this

@RequestMapping(value = "/level/addNewLevel", method = RequestMethod.POST)
public String addNewLevel(@ModelAttribute("level") Level level, BindingResult result, Model model)
{
    validator.validate(level, result);
    if(result.hasErrors()) {
        //show errors
    }
    else {
        //add level
    }
}

So now onto my problem:

I can get the name, points, id, etc of the level just fine but the Requirements are not coming over at all. I tried to insert <input type='hidden' value='' + id +'' name="requirements"/> in the divs that are in the requiredRequirements when the form is submitted and the do something like this

String[] requiredRequirements = ((String) result.getFieldValue("requirements")).split(",");
level.setRequirements(getRequirementsFromIDs(requiredRequirements));

This works fine until i call the validate method because in the binding result, the requirements is just a list of strings (from the hidden field called requirements) so it throws a type mismatch. I thought about writing a property editor but that seems like a hack to fix a hack.

I was wondering if anyone had any advice on how to fix this problem.

Thanks in advance

回答1:

Writing a propery editor (or better a Spring 3 Converter) for this situation is not a hack. That is what property editors are for. So it is only the solution for the hack, but not the hack for the hack.

But there is a better way the deal with the list problem. Because Spring 3 Converter for single objects like (String(id) to Requirement) can be applied to List of Requirements by spring automaticaly. So you need to write only a Converter that can convert the String representation of an id to an Requirement. Spring will apply this to Lists too.