小菜鸟学习SpringMVC(三) 数据自动封装

本文详细介绍了SpringMVC注解开发中如何实现从表单自动封装数据,包括基本数据类型、POJO类、包装类、数组、集合以及Map集合的封装方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用SpringMVC注解开发从from表单读取数据的自动封装 

注意:方法所传的变量需要和jsp中的from表单字段相同
@Controller
@RequestMapping("my")
public class MyController {
}


1. 读取基本数据类型的封装类型


@RequestMapping("getint")
public String Getint(Integer id){
System.out.println(id);
return "success";
}

<form action="${pageContext.request.contextPath }/my/getint.do"
method="post">
姓名:<input type="text" name="id" value="4" id="id"> <input
type="submit" value="提交">
</form>



2.读取数据封装到一个pojo类中

@RequestMapping("getuser")
public String GetUser(User user){
System.out.println(user);
return "success";
}

<form action="${pageContext.request.contextPath }/my/getuser.do"
method="post">
姓名:<input type="text" name="username" value="username" id="username">
年龄:<input type="text" name="age" value="0" id="age">
生日:<input type="text" name="birthday" value="1995/09/30" id="birthday">
地址:<input type="text" name="address" value="agg" id="address">
测试:<input type="text" name="test" value="test" id="address">
<input type="submit" value="提交">
</form>


3.读取数据封装到一个包装类中


包装类:
public class Usercost {

private User user;   //User对象
private String test;
}

@RequestMapping("getcost")
public void GetUsercost(Usercost user){
System.out.println(user);
//return "success";
}

<form action="${pageContext.request.contextPath }/my/getcost.do"
method="post">
姓名:<input type="text" name="user.username" value="username" id="username">
年龄:<input type="text" name="user.age" value="0" id="age">
生日:<input type="text" name="user.birthday" value="1995/09/30" id="birthday">
地址:<input type="text" name="user.address" value="agg" id="address">
测试:<input type="text" name="test" value="test" id="test">
<input type="submit" value="提交">
</form>

4.读取数据到数组中

@RequestMapping("getarray")
public String GetArray(Integer[] ids){
System.out.println(ids);
for (int i = 0; i < ids.length; i++) {
System.out.println(ids[i]);
}
return "success";
}

<form action="${pageContext.request.contextPath }/my/getarray.do"
method="post">
第一 <input type="checkbox" name="ids" value="1" id="id">
第二 <input type="checkbox" name="ids" value="2" id="id">
第三 <input type="checkbox" name="ids" value="10" id="id">
<input type="submit" value="提交">
</form>

5.读取数据到集合中


包装类:public class Usercost {

private User user;
private List<User> list = new ArrayList<User>();
private String test;
}

@RequestMapping("getlist")
public String GetList( Usercost user){
System.out.println(user.getList());
return "success";
}

<form action="${pageContext.request.contextPath }/my/getlist.do"
method="post">
姓名:<input type="text" name="list[0].username" value="username" id="username">
年龄:<input type="text" name="list[0].age" value="0" id="age">
姓名:<input type="text" name="list[1].username" value="username" id="username">
年龄:<input type="text" name="list[1].age" value="0" id="age">
<input type="submit" value="提交">
</form>

6.读取数据到Map集合中

包装类:
public class Usercost {

private User user;
private List<User> list = new ArrayList<User>();
private String test;
private Map<String, Object> map = new HashMap<String,Object>();
}


@RequestMapping("getmap")
public String GetMap(Usercost user){
System.out.println(user.getMap());
return "success";
}

<form action="${pageContext.request.contextPath }/my/getmap.do"
method="post">
姓名:<input type="text" name="map['username'].username" value="username" id="username">
年龄:<input type="text" name="map['age'].age" value="0" id="age">
<input type="submit" value="提交">
</form>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值