How do I bind collection attributes to a form in Spring MVC

后端 未结 6 977
忘了有多久
忘了有多久 2021-02-04 05:17

I\'m trying to bind one of my model objects to the fields of a form, using Spring-MVC. Everything works fine, except that one of the attributes of the model object is an unorder

6条回答
  •  梦谈多话
    2021-02-04 06:05

    You can use a semi-colon-delimited list if you're using numeric references to the IDs of objects, and an appropriate Converter implementation registered.

    POST data leaderboards=1,2

    Converter implementation (ignore the JSON stuff)

        public final class LeaderboardConverter extends JsonDeserializer implements Converter
        {
            public Leaderboard convert(String source) throws IllegalArgumentException
            {
                Leaderboard activity = new Leaderboard();
                activity.setId(new Integer(source));
                return activity;
            }
    
    
            public Leaderboard deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException
            {
                return convert(jp.getText());
            }
    
    }
    

提交回复
热议问题