bez*_*max 18 java code-formatting spring-mvc
我的任务是 - 通过给定的请求参数创建模型属性,以验证它(以相同的方法)并将其整体提供给View.
我得到了这个示例代码:
@Controller
class PromotionController {
@RequestMapping("promo")
public String showPromotion(@RequestParam String someRequestParam, Model model) {
//Create the model attribute by request parameters
Promotion promotion = Promotions.get(someRequestParam);
//Add the attribute to the model
model.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);
}
return
}
}
Run Code Online (Sandbox Code Playgroud)
这件事确实有效,但是使用MODEL_KEY_PREFIX和属性名称创建键的那部分看起来非常hackish而不是Spring风格.有没有办法让同样的东西漂亮?
斯卡夫曼回答了问题但消失了,所以我来替他回答。
绑定验证用于绑定和验证参数,而不是任意业务对象。
这意味着,如果我需要对用户未提交的一些常规数据进行一些自定义验证- 我需要添加一些自定义变量来保存该状态而不是使用 BindingResult。
这回答了我对 BindingResult 的所有问题,因为我认为它必须用作任何类型错误的容器。
再次感谢@Skaffman。
归档时间: |
|
查看次数: |
12977 次 |
最近记录: |