springmvc 获取前台请求数据方式
1)基本数据类型或String,在方法参数中定义参数,参数名与请求传递数据名一致即可自动封装;
// RequestMapping:指定方法对应的请求地址 //return:页面地址,表示方法执行完成之后跳转到对应的页面(转发) //springmvc:接收请求参数,直接在方法的参数中定义名称与传递参数名一致的形参即可 //name:会自动接收请求传递的name值 @RequestMapping("/hello") public String hello(String name,Integer age){ System.out.println("name:"+name+",age:"+age); return "index.jsp"; }
启动tomcat,在浏览器地址栏输入
http://localhost:8086/hello?name=tom&age=18
即可查看结果;
2)对象类型的,在方法参数中定义对象,与对象属性名一致的数据,会自动封装进对象;
public class User {
private Integer id;
private String name;
private Integer age;
private String sex;
private String addr;
}
并对其get和set,再写一个form表单提交信息的jsp页面;
<form action="/saveUser" method="post"> 用户名:<input type="text" name="name"/><br/> 年龄:<input type="text" name="age"/><br/> 性别:<input type="radio" name="sex" value="男"/>男 <input type="radio" name="sex" value="女">女<br/> 地址:<input type="text" name="addr"><br/> <input type="submit" value="注册"/> </form>
再在controller中测试结果;
@RequestMapping("/saveUser") public String saveUser(User user){ System.out.println("User:"+user);
return "index.jsp";
}
3)接收数组的情况。在方法中定义数组;
4)对象中的数组和集合可以接收与集合属性同名的多个请求数据;
//通过对象来接收前台的多个同名数据,两种方式,一种是数组,一种是List集合; //private String[] hobbies; private List<String> hobbies;
public String[] getHobbies() {
return hobbies;
}
public void setHobbies(String[] hobbies) {
this.hobbies = hobbies;
}
public List<String> getHobbies() {
return hobbies;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
jsp页面中添加爱好属性:
爱好: <input type="checkbox" name="hobbies" value="打篮球"/>打篮球 <input type="checkbox" name="hobbies" value="打游戏"/>打游戏 <input type="checkbox" name="hobbies" value="敲代码"/>敲代码 <input type="checkbox" name="hobbies" value="学习"/>学习<br/>
再在controller中测试结果;
@RequestMapping("/saveUser") public String saveAdmin(User user, String[] hobbies){ System.out.println("user:"+user); System.out.println(Arrays.toString(hobbies)); return "index.jsp"; }
5)对象中的对象。 前台指定name 时,属性.属性;
要测试对象中的对象,那我们要另外再建立一个对象,然后把这个对象当成属性添加到User.java中去;
public class Role { private String name; private Integer id; @Override public String toString() { return "Role{" + "name='" + name + '\'' + "id='" + id + '\'' + '}'; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } }
User.java中: //用来接收前台的数据 private Role role; public Role getRole() { return role; } public void setRole(Role role) { this.role = role; }
jsp文件中: <%-- 将属性封装到对象的对象属性中 属性名.属性--%> 角色:<select name="role.name" id="role"> <option value="管理员">管理员</option> <option value="超级管理员">超级管理员</option> <option value="测试账号">测试账号</option> </select><br/> <input type="hidden" name="role.id" value="100"/><br/>
测试:
public String saveUser(User user, String[] hobbies){ System.out.println("User:"+user);
System.out.println(Arrays.toString(hobbies));
System.out.println("role:"+user.getRole());
return "index.jsp";
}
6)将数据封装到map中。在方法中定义HashMap参数,并在前面添加@RequestParam注解;
7)封装到对象中的map,前台指定name为 map属性名[key值];
User.java中: //通过map接收前台的值 private Map<String,String> conditions; public Map<String, String> getConditions() { return conditions; } public void setConditions(Map<String, String> conditions) { this.conditions = conditions; }
jsp文件中: <%--将数据封装到对象中的map,map属性名[key]--%> 查询条件:<input type="text" name="conditions[age]"/><br/> 查询条件:<input type="text" name="conditions[sex]"/><br/> <input type="submit" value="注册"/>
测试:
//map :将所有请求数据封装到map中 // @RequestParam("name"):指定向请求中获取数据 ==>request.getParameter //required = false:是否必传 defaultValue:默认值 @RequestMapping("/saveUser") public String saveUser(User user, String[] hobbies, @RequestParam(name="name",required = false,defaultValue = "username") String username, @RequestParam HashMap map){ System.out.println("User:"+user); System.out.println("role:"+user.getRole()); System.out.println(Arrays.toString(hobbies)); System.out.println("map:"+map); System.out.println("username:"+username);
System.out.println("conditions:"+user.getConditions());
return "index.jsp";
}