* 当用get方式访问add页面,调用该方法
*/
@RequestMapping(value="/add",method=RequestMethod.GET)
public String adduser(Model model){
//开modelDriven
model.addAttribute(new user());
return "user/add";
}
/*
* 用post方法访问add页面
*/
@RequestMapping(value="/add",method=RequestMethod.POST)
public String adduser(@Validated user use,BindingResult br){
if(br.hasErrors())
return "user/add";
users.put("5", use);
return "redirect:users";
}
2.在返回的过程中,默认为forword:返回页面 客户端的跳转得加上redirect:
public String adduser(@Validated user use,BindingResult br){
if(br.hasErrors())
return "user/add";
users.put("5", use);
return "redirect:users";
}
3. 使用spring mvc form标签得加上<%@taglib prefix="sf" uri="http://www.springframework.org/tags/form" %>
然后用<sf:xxxxx>来使用
eg.
<!-- springMVC的from标签 modelAttribute数据绑定,在from里面相当于user.username-->
<sf:form method="post" modelAttribute="user">
<!-- path 相当于绑定类的属性名称-->
username:<sf:input path="username"/><sf:errors path="username"></sf:errors><br>
userpassword:<sf:password path="userpassword"/><br>
email:<sf:input path="email"/><sf:errors path="email"></sf:errors><br>
age:<sf:input path="age"/><sf:errors path="age"></sf:errors><br>
<input type="submit" value="提交">
</sf:form>