在spring里,model和form的对应不需要写xml文件,只需要在页面里用form这个taglib就可以了:
这里的commandName就是和Controller里相互映射的model的名字(实例名)
path=email指定了对应对象的属性。
Controller里可以这样写:
protected ModelAndView sendInvite(@ModelAttribute(“invite”) Invite invite,
BindingResult result, SessionStatus status, ModelMap model) {}
其中,参数的第一个invite会自动从form里生成一个Invite的实例并设置值,Controller里直接使用这个对象就可以。
而加上@SessionAttributes的话,可以在form里一直显示上次输入的form内容,直到自己调用status.setComplete()为止,这个操作的意思就是一个form相关联的操作已经结束,从session里清空就可以了
固定链接: http://liubin.nanshapo.com/2010/07/08/modelattribute-under-spring/ | 其实我是一个程序员
最新评论