springMVC获取传递的参数

页面定义如下:

<html>
<body>
<form action="spring/cpf.do">
	<input type="text" name="username">
	<input type="text" name="password">
	<input type="submit" value="submit">
</form>
</body>
</html>

1.通过HttpServletRequest

@Controller
@RequestMapping("/spring")
public class HelloWorldController 
{
	//表示对应的路径是/cpf.do
	@RequestMapping(value="/cpf.do")
	public String say(HttpServletRequest request, HttpServletResponse response)
	{
		String name = request.getParameter("username");
		String password = request.getParameter("password");
		System.out.println(name);
		System.out.println(password);
		
		return "cpf";
	}
}
            这种方式和Servlet完全一样,从Request中直接获得即可

2.通过和参数名相同的入参

@RequestMapping(value="/cpf.do")
public String say( String username,
String password, Model model)
{
<span style="white-space:pre">	</span>System.out.println(name);
	System.out.println(password);

	return "cpf";
}

3.通过别名,并指定默认值

@RequestMapping(value="/cpf.do")
public String say(@RequestParam(value="username",defaultValue="tom") String name,
@RequestParam(value="password",defaultValue="123")String password, Model model)
{
<span style="white-space:pre">	</span>System.out.println(name);
	System.out.println(password);
	model.addAttribute("hello", name);
		
	return "cpf";
}

4.通过对象( 最常用

          页面定义如下:

<html>
<body>
<h2>${hello}</h2>
<form action="cpf.do">
	<input type="text" name="username"><br/>
	<input type="text" name="password"><br/>
	<input type="checkbox" name="hobby" value="dota"> dota
	<input type="checkbox" name="hobby" value="travel"> travle<br/>
	<input type="submit" value="submit">
</form>
</body>
</html>

          定义对应的对象:

public class User {
	private String username;
	private String password;
	private String[] hobby;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String[] getHobby() {
		return hobby;
	}
	public void setHobby(String[] hobby) {
		this.hobby = hobby;
	}	
}
          Controller:

@Controller
public class HelloWorldController 
{
	@RequestMapping(value="/cpf.do")
	public String say(User user)
	{
		System.out.println("username:"+user.getUsername());
		System.out.println("password:"+user.getPassword());
		System.out.print("hobby contains:");
		for (String s : user.getHobby()) {
			System.out.print(s+" ");
		}
		
		return "cpf";
	}
}
         结果为:

          

           注意:复选框使用String[]来存储所有的值

5.参数类型转换

          有时我们需要进行类型转换。如页面需要传入生日,但是传入的是字符串形式,而我们的Controller需要接收Date类型的数据,这个时候,就可以使用类型转化器了

@Controller
public class HelloWorldController 
{
	@InitBinder
	public void initBinder(ServletRequestDataBinder bin)
	{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		CustomDateEditor dateEditor = new CustomDateEditor(sdf, true);
		
		bin.registerCustomEditor(Date.class, dateEditor);
	}
	
	@RequestMapping(value="/cpf.do")
	public String say(Date birthday)
	{
		System.out.println("birthday:"+birthday);
		
		return "cpf";
	}
}
         页面为:

<html>
<body>
<h2>${hello}</h2>
<form action="cpf.do">
	<input type="text" name="birthday"><br/>
	<input type="submit" value="submit">
</form>
</body>
</html>
       输入:

结果为:




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值