Spring - adding BindingResult to newly created model attribute
Posted
by Max
on Stack Overflow
See other posts from Stack Overflow
or by Max
Published on 2010-06-16T11:18:30Z
Indexed on
2010/06/16
11:22 UTC
Read the original article
Hit count: 841
My task is - to create a model attribute by given request parameters, to validate it (in same method) and to give it whole to the View.
I was given this code:
//Create the model attribute by request parameters
Promotion promotion = Promotions.get(someRequestParam);
//Add the attribute to the model
modelMap.addAttribute("promotion", promotion);
if (!promotion.validate()) {
BindingResult errors = new BeanPropertyBindingResult(promotion, "promotion");
errors.reject("promotion.invalid");
//TODO: This is the part I don't like
model.put(BindingResult.MODEL_KEY_PREFIX + "promotion", errors);
}
This thing sure works, but that part with creating key with MODEL_KEY_PREFIX and attribute name looks very hackish and not a Spring style to me. Is there a way to make the same thing prettier?
© Stack Overflow or respective owner