@Controller
@RequestMapping(value="/")
public class TestModelAttributeController{
@RequestMapping(value="result")
public String login(Model model) {
model.containsAttribute("username");
return "result";
}
/*
* 返回的是username的值
*相当于model.addAttribute("username", username);
*/
@ModelAttribute(value="username")
public String userModel(@RequestParam("username") String username) {
return username;
}
}
使用方式三:@ModelAttribute注释返回具体类的方法
@ModelAttribute
public User userModel(@RequestParam("username") String username,@RequestParam("pwd") String pwd){
User user = new User(username,pwd);
return user;
}
public String login(Model model){
System.out.println(model.containsAttribute("username"));
System.out.println(model.containsAttribute("pwd"));
return modelAttribute/result";
}