SpringMVC获取参数的几种方式
1个回答
2018-07-05 · 国内最优秀java资源共享平台
关注
展开全部
获取页面参数的几种方式
1、直接把表单的参游尘数写在Controller相应的方法的形参中
案例:
/**
* 1.直接把表单的参数写在Controller相应的方法的形唤亩参中
*/
@RequestMapping("/add")
public String add(String username,String password) {
System.out.println("username is:"+username);
System.out.println("password is:"+password);
return "index";
}
2、通过HttpServletRequest接收
案例:
/**
* 2、和磨森通过HttpServletRequest接收
*/
@RequestMapping("/add")
public String add(HttpServletRequest request) {
String username=request.getParameter("username");
String password=request.getParameter("password");
System.out.println("username is:"+username);
System.out.println("password is:"+password);
return "index";
}
3、通过一个bean来接收
案例:
public class User {
private Long id;
private String username;
private String password;
public User(){}
public Long getId() {
return id;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public void setId(Long id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
}
/**
* 3、通过一个bean来接收
*/
@RequestMapping("/add")
public String add(User user) {
System.out.println("username is:"+user.getUsername());
System.out.println("password is:"+user.getPassword());
return "index";
}
4、通过@PathVariable获取路径中的参数
案例:
/**
* 4、通过@PathVariable获取路径中的参数
* @param username
* @param password
* @return
*/
@RequestMapping(value="/add/{username}/{password}",method=RequestMethod.GET)
public String addUser4(@PathVariable String username,@PathVariable String password) {
System.out.println("username is:"+username);
System.out.println("password is:"+password);
return "index";
}
1、直接把表单的参游尘数写在Controller相应的方法的形参中
案例:
/**
* 1.直接把表单的参数写在Controller相应的方法的形唤亩参中
*/
@RequestMapping("/add")
public String add(String username,String password) {
System.out.println("username is:"+username);
System.out.println("password is:"+password);
return "index";
}
2、通过HttpServletRequest接收
案例:
/**
* 2、和磨森通过HttpServletRequest接收
*/
@RequestMapping("/add")
public String add(HttpServletRequest request) {
String username=request.getParameter("username");
String password=request.getParameter("password");
System.out.println("username is:"+username);
System.out.println("password is:"+password);
return "index";
}
3、通过一个bean来接收
案例:
public class User {
private Long id;
private String username;
private String password;
public User(){}
public Long getId() {
return id;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public void setId(Long id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
}
/**
* 3、通过一个bean来接收
*/
@RequestMapping("/add")
public String add(User user) {
System.out.println("username is:"+user.getUsername());
System.out.println("password is:"+user.getPassword());
return "index";
}
4、通过@PathVariable获取路径中的参数
案例:
/**
* 4、通过@PathVariable获取路径中的参数
* @param username
* @param password
* @return
*/
@RequestMapping(value="/add/{username}/{password}",method=RequestMethod.GET)
public String addUser4(@PathVariable String username,@PathVariable String password) {
System.out.println("username is:"+username);
System.out.println("password is:"+password);
return "index";
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询