(一)前端传递的参数,在SpringMVC的controller中使用基本数据类型或者String接收参数
1.前端有一个form表单,在controller中使用基本数据类型或者String类型进行接收参数,此时只需要保证请求的参数名称与接受的参数名称一致即可,SpringMVC会帮助我们自动注入赋值。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
${error}
<form action="first" method="get">
用户名:<input type="text" name="username"></br>
密码:<input type="password" name="password"></br>
<input type="submit" value="提交">
</form>
</body>
</html>
@RequestMapping("first")
public ModelAndView loginActive(String username,String password){
//默认保证参数名称和请求中传递的参数名相同即可传递参数
ModelAndView view=new ModelAndView();
System.out.println(username);
System.out.println(password);
if("四维空间".equals(username) && "123".equals(password)) {
view.addObject("username",username);
view.setViewName("success");
}else {
view.addObject("error", "用户名或密码错误!");
view.setViewName("login");
};
return view;
};
即:<input type="text" name="username"></br><input type="password" name="password"></br>与public ModelAndView loginActive(String username,String password){}的名称保持一致
2.前端有一个form表单,在controller中使用类进行接收参数,此时只需要保证请求的参数名称与类中的属性名称一致即可,类类型有get/set方法,满足这两个条件时。SpringMVC会帮助我们自动注入赋值。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="people" method="post">
<input type="text" name="name" >
<input type="text" name="age" >
<input type="submit" value="提交">
</form>
</body>
</html>
@RequestMapping("people")
public ModelAndView demo04(People peo) {
ModelAndView view=new ModelAndView();
view.addObject("name",peo.getName());
view.addObject("age", peo.getAge());
System.out.println(peo.getName()+" "+peo.getAge());
System.out.println(peo);
view.setViewName("peopleWelcome");
return view;
};
public class People {
private String name;
private String age;
public People() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "People [name=" + name + ", age=" + age + "]";
}
}
前端:<input type="text" name="name" ><input type="text" name="age" > 控制器:public ModelAndView demo04(People peo) {}实体类:private String name; private String age;保持一致。
3. 前端传递的参数,在springMVC的controller中使用HttpServletRequest进行接受
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="demo07" method="post">
<input type="text" name="name" >
<input type="text" name="age" >
<input type="submit" value="提交">
</form>
</body>
</html>
@RequestMapping("demo07")
public String demo07(HttpServletRequest req) {
//String name =(String) req.getAttribute("name");
//String age=(String) req.getAttribute("age");
String name=req.getParameter("name");
String age=req.getParameter("age");
System.out.println(name+":"+age);
return "main";
};
4.前端传递的参数,在springMVC的controller中 同时使用上述三中方法进行混合接受参数(不再演示)
(二)使用@RequestParam()注解接受参数
(1) 如果请求参数名和方法参数名不对,使用value属性
@RequestMapping("demo01")
public String demo01(@RequestParam(value="username1")String username,@RequestParam(value="password1")String password) {
//如果请求参数名和方法参数名不对应使用@RequestParam()赋值
System.out.println("执行demo02"+username+" "+password);
return "login";
};
(2)如果接受参数是基本类型,且接受参数类型与null无法进行兼容,此时可以采用包装类型或者采用默认值,使用defaultValue属性赋默认值。
@RequestMapping("demo02")
public String demo02(@RequestParam(defaultValue="河南理工大学")String username,@RequestParam(defaultValue="110")String password) {
//注入默认值
return "login";
};
(3)如果强制要求必须有某个参数,不能为空,使用required属性强制要求赋值,否者报错。
@RequestMapping("demo03")
public String demo03(@RequestParam(required=true)String username,String password) {
//强制要求必须要有参数,否则报错
return "login";
};
(4)请求参数中包含多个同名参数时(复选框传递的参数就是多个同名参数)。我们也称前端向后台传递List类型的参数,这时候使用value属性,因为在前端传递过来的list都会放入一个参数名称中,需要把这个参数名称和一个List类型变量进行绑定。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="hobby" method="get">
游泳<input type="checkbox" name="hobby" value="游泳">
读书<input type="checkbox" name="hobby" value="读书">
编程<input type="checkbox" name="hobby" value="编程">
<input type="submit" value="提交">
</form>
</body>
</html>
@RequestMapping("hobby")
public void demo05(@RequestParam("hobby")List <String> list){
for(String li:list) {
System.out.println(li);
}
};
(三)SpringMVC的restful风格的参数传递
首先请求参数的格式一定的要求,以前的方式是<a href="demo06?name=张三&age=23">跳转</a>,而restful格式是:<a href="demo06/123/abc">跳转</a>在控制器中:@RequestMapping 中一定要和请求格式对应 {名称} 中名称自定义名称
@PathVariable 获取@RequestMapping 中内容,默认按照方法参数名称去寻找。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="demo06/vd/xwf">点击跳转</a>
</body>
</html>
@RequestMapping( value="/demo06/{first}/{second}")
public String demo06(@PathVariable("first")String first,@PathVariable("second")String second) {
System.out.println(first+" :"+second);
return "login";
};