springmvc请求参数获取的几种方法
本文内容纲要:
1、直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交。
/**
*1.直接把表单的参数写在Controller相应的方法的形参中
*@paramusername
*@parampassword
*@return
*/
@RequestMapping("/addUser1")
publicStringaddUser1(Stringusername,Stringpassword){
System.out.println("usernameis:"+username);
System.out.println("passwordis:"+password);
return"demo/index";
}
url形式:http://localhost/SSMDemo/demo/addUser1?username=lixiaoxi&password=111111提交的参数需要和Controller方法中的入参名称一致。
2、通过HttpServletRequest接收,post方式和get方式都可以。
/**
*2、通过HttpServletRequest接收
*@paramrequest
*@return
*/
@RequestMapping("/addUser2")
publicStringaddUser2(HttpServletRequestrequest){
Stringusername=request.getParameter("username");
Stringpassword=request.getParameter("password");
System.out.println("usernameis:"+username);
System.out.println("passwordis:"+password);
return"demo/index";
}
3、通过一个bean来接收,post方式和get方式都可以。
(1)建立一个和表单中参数对应的bean
packagedemo.model;
publicclassUserModel{
privateStringusername;
privateStringpassword;
publicStringgetUsername(){
returnusername;
}
publicvoidsetUsername(Stringusername){
this.username=username;
}
publicStringgetPassword(){
returnpassword;
}
publicvoidsetPassword(Stringpassword){
this.password=password;
}
}
(2)用这个bean来封装接收的参数
/**
*3、通过一个bean来接收
*@paramuser
*@return
*/
@RequestMapping("/addUser3")
publicStringaddUser3(UserModeluser){
System.out.println("usernameis:"+user.getUsername());
System.out.println("passwordis:"+user.getPassword());
return"demo/index";
}
4、通过@PathVariable获取路径中的参数
/**
*4、通过@PathVariable获取路径中的参数
*@paramusername
*@parampassword
*@return
*/
@RequestMapping(value="/addUser4/{username}/{password}",method=RequestMethod.GET)
publicStringaddUser4(@PathVariableStringusername,@PathVariableStringpassword){
System.out.println("usernameis:"+username);
System.out.println("passwordis:"+password);
return"demo/index";
}
例如,访问http://localhost/SSMDemo/demo/addUser4/lixiaoxi/111111路径时,则自动将URL中模板变量{username}和{password}绑定到通过@PathVariable注解的同名参数上,即入参后username=lixiaoxi、password=111111。
5、使用@ModelAttribute注解获取POST请求的FORM表单数据
Jsp表单如下:
<formaction="<%=request.getContextPath()%>/demo/addUser5"method="post">
用户名: <inputtype="text"name="username"/><br/>
密 码: <inputtype="password"name="password"/><br/>
<inputtype="submit"value="提交"/>
<inputtype="reset"value="重置"/>
</form>
JavaController如下:
/**
*5、使用@ModelAttribute注解获取POST请求的FORM表单数据
*@paramuser
*@return
*/
@RequestMapping(value="/addUser5",method=RequestMethod.POST)
publicStringaddUser5(@ModelAttribute("user")UserModeluser){
System.out.println("usernameis:"+user.getUsername());
System.out.println("passwordis:"+user.getPassword());
return"demo/index";
}
6、用注解@RequestParam绑定请求参数到方法入参
当请求参数username不存在时会有异常发生,可以通过设置属性required=false解决,例如:@RequestParam(value="username",required=false)
/**
*6、用注解@RequestParam绑定请求参数到方法入参
*@paramusername
*@parampassword
*@return
*/
@RequestMapping(value="/addUser6",method=RequestMethod.GET)
publicStringaddUser6(@RequestParam("username")Stringusername,@RequestParam("password")Stringpassword){
System.out.println("usernameis:"+username);
System.out.println("passwordis:"+password);
return"demo/index";
}
本文内容总结:
原文链接:https://www.cnblogs.com/xiaoxi/p/5695783.html