Spring - SessionAttribute problem
I want to implement something like this:
@Controller
@SessionAttributes("promotion")
class PromotionController {
@RequestMapping("showPromo")
void showPromotionInfo(
@RequestParam("promId") String promotionId,
@ModelAttribute Promotion promotion, Errors errors
) {
promotion = Promotions.get(promotionId);
if (promotion == null || promotion.validates() == false) {
errors.reject("promotion.invalid");
}
return "prom";
}
}
The code is invalid, wont work and has some bad errors, but I don't know how to write it better.
When user comes to an URL "showPromo?promId=15", controller should validate if the promotion is valid (outdated/non-existent/etc.). If it is valid - it should show it's information and save the promotion to model and session. If it's not - it should show some error about promotion being invalid.
Problem is, I need to save the promotion in the session (for several requests) and don't want to use direct session management. Is it currently possible with Spring? Or am I doing something wrong?
Could you please provide the optimal solution to my problem using Spring 3?
Thanks in advance开发者_如何学JAVA.
Added:
Ok, I think I'll write down the problematic points:
- When there is no object in session, it can't be bound with @ModelAttribute throwing an error like "no object found in session". And there is no "optional" parameter anywhere.
- I can't have @ModelAttribute in the parameters, as I'm overwriting that promotion object. Proper way should be model.addAttribute("promotion", promotion). But I also need to have a way to provide errors, and I see no way to provide them using model.addAttribute.
In addition to skaffman's answer: you can associate BindingResult
s with manually added model attributes using BindingResult.MODEL_KEY_PREFIX
.
Promotion promotion = ....
modelMap.addAttribute("promotion", promotion);
if (!promotion.validate()) {
BindingResult errors = new BeanPropertyBindingResult(promotion, "promotion");
errors.reject("promotion.invalid");
model.put(BindingResult.MODEL_KEY_PREFIX + "promotion", errors);
}
Note, however, that you can't associate BindingResult
with null
object, therefore you can't use errors.reject
when promotion was not found (if you don't add some kind of empty promotion into model in that case).
promotion = Promotions.get(promotionId);
Above is your line of code. Promotion(s).get(promotionId); // exclude that "s" I placed in ()... That SHOULD help you in one case.
Another thing that COULD help you is when you do your @ModelAttribute("bindingVariable"). You should place one in there which in your case would bind to Promotion promotion,
If you have a CHECK block there.... You should have a view to go to IF it fails (should be handled some how)... AND if it succeeds. You have no handling on this as far as I can see.
精彩评论