java如何获取前台数据_Springmvc获取前台请求数据过程解析

本文详细介绍了SpringMVC框架中各种类型参数的接收方法,包括基本数据类型、字符串、对象、数组、集合及Map等,并提供了具体示例。

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页面;

用户名:

年龄:

性别:男

地址:

再在controller中测试结果;

@RequestMapping("/saveUser")

public String saveUser(User user){

System.out.println("User:"+user);

return "index.jsp";

}

3)接收数组的情况。在方法中定义数组;

4)对象中的数组和集合可以接收与集合属性同名的多个请求数据;

//通过对象来接收前台的多个同名数据,两种方式,一种是数组,一种是List集合;

//private String[] hobbies;

private List hobbies;

public String[] getHobbies() {

return hobbies;

}

public void setHobbies(String[] hobbies) {

this.hobbies = hobbies;

}

public List getHobbies() {

return hobbies;

}

public void setHobbies(List hobbies) {

this.hobbies = hobbies;

}

jsp页面中添加爱好属性:

爱好: 打篮球

打游戏

敲代码

学习

再在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文件中:

角色:

管理员

超级管理员

测试账号

测试:

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 conditions;

public Map getConditions() {

return conditions;

}

public void setConditions(Map conditions) {

this.conditions = conditions;

}

jsp文件中:

查询条件:

查询条件:

测试:

//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"; }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值