您的位置:首页 > 编程语言 > Java开发

springMVC教程(三)在controller中获取前台传递的参数

2014-06-20 09:16 477 查看
在controller中获取前台传递的参数

将页面数据传递到controller
页面表单:
<form
action="user.do"method="post">
     
用户名:<input
type="text"name="name"/><br/>
     
年龄:<input
type="text"name="age"/><br/>
     
生日:<input
type="text"name="birth"/><br/>
      <input
type="submit"
value="添加"/>
</form>
Controller为:
/**
    * 1、直接使用形参获取前台传递的参数数据
    * 要注意的是形参的名字必须和页面参数的名字一致
    * @param model
    * @param name
    * @param age
    * @param birth
    * @return
    */
   @RequestMapping(method=RequestMethod.POST)
   publicString addUser(Model model,String name,Integer age,Date birth){
      model.addAttribute("message","添加了一个用户");
      System.out.println("name:"+name+ "\tage:"+age+"\tbirht:"+birth);
      System.out.println("UserController.addUser()");
      return"/WEB-INF/jsp/addsuc.jsp";
   }
 
/**
    * 2、使用对象接受前台传递的参数,
    * 要注意的是前台传递的参数的名称必须和对象的属性名称一直,如果不一致则可以使用@ModelAttribute("u")String
uname指定
    */
   @RequestMapping(method=RequestMethod.POST)
   publicString addUser(Model model,User user){
      model.addAttribute("message","添加了一个用户");
      System.out.println("name:"+user.getName()+ "\tage:"+user.getAge()+"\tbirht:"+user.getBirth());
      System.out.println("UserController.addUser()");
      return"/WEB-INF/jsp/addsuc.jsp";
   }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mvc spring springmvc web 框架