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, does not work, and has some bad errors, but I don’t know how to write it better.

When the user arrives at the URL "showPromo? PromId = 15", the controller must check if the stock is valid (obsolete / does not exist / etc.). If it is valid, it should show information and keep moving along the model and session. If this is not so, it should be incorrectly indicated that the promotion is invalid.

The problem is that I need to keep the promotion in the session (for multiple requests) and do not want to use direct session management. Is this possible with Spring? Or am I doing something wrong?

Could you suggest an optimal solution to my problem using Spring 3?

.

:

, , :

  • , @ModelAttribute, , "no object found in session". "" .
  • @ModelAttribute , . model.addAttribute( "", ). , model.addAttribute.
+3
2

skaffman: BindingResult 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);
}

, , BindingResult null, errors.reject, ( - ).

+1

promotion = Promotions.get(promotionId);

. ().get(promotionId);//, "s" ()... .

, , - @ModelAttribute ( "bindingVariable" ). , ,

CHECK... , IF, ( - )... . .

0

Source: https://habr.com/ru/post/1750063/


All Articles