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";
}
将页面数据传递到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";
}
相关文章推荐
- SpringMVC中,前台jsp封装参数,绑定参数,传递参数到后台controller的过程详解
- springMVC教程(四)在controller中获取web元素
- Spring MVC 3学习笔记+教程(二)在controller和视图之间传递参数
- 前台传递参数使用变量@RequestParam springmvc
- 二 :springMVC:modelandview,model,controller,参数传递
- springMVC:modelandview,model,controller,参数传递
- SpringMVC:ModelAndView,model,controller,参数传递
- springMVC controller间跳转 重定向 传递参数的方法
- SpringMVC之后台接收参数与前台传递数据
- SpringMVC Controller接收前台ajax的GET或POST请求返回各种参数
- springmvc在controller和视图之间传递参数
- Spring MVC 3学习笔记+教程 在controller和视图之间传递参数
- springmvc项目jsp页面传递参数至controller中文乱码解决方案
- springMVC参数的传递方式(1.通过@PathVariabl获取路径参数,2.@ModelAttribute获取数据,3.HttpServletRequest取参,4@RequestParam)
- ASP.NET关于前台脚本传递中文参数,后台获取乱码问题(乱码为方框,黑色方框等。。)
- Spring MVC 3学习笔记+教程(二)在controller和视图之间传递参数
- springMVC获取传递的参数
- Spring MVC 3学习笔记+教程(二)在controller和视图之间传递参数
- 使用 aop拦截 springMVC的controller并获取请求参数及返回结果
- springMVC中前台向后台传递参数的方式