1. 基本数据类型(以int为例,其他类似):
controller代码:
| 
 
								1
 
								2
 
								3
						  | 
@requestmapping("saysth.do")
public void test(int count) {
}
 | 
表单代码:
| 
 
								1
 
								2
 
								3
 
								4
						  | 
<form action="saysth.do" method="post">
<input name="count" value="10" type="text"/>
......
</form>
 | 
表单中input的name值和controller的参数变量名保持一致,就能完成数据绑定,如果不一致可以使用@requestparam注解。需要注意的是,如果controller方法参数中定义的是基本数据类型,但是从页面提交过来的数据为null或者”"的话,会出现数据转换的异常。
也就是必须保证表单传递过来的数据不能为null或”",所以,在开发过程中,对可能为空的数据,最好将参数数据类型定义成包装类型,具体参见下面的例子。
2. 包装类型(以integer为例,其他类似):
controller代码:
| 
 
								1
 
								2
 
								3
						  | 
@requestmapping("saysth.do")
public void test(integer count) {
}
 | 
表单代码:
| 
 
								1
 
								2
 
								3
 
								4
						  | 
<form action="saysth.do" method="post">
<input name="count" value="10" type="text"/>
......
</form>
 | 
和基本数据类型基本一样,不同之处在于,表单传递过来的数据可以为null或”",以上面代码为例,如果表单中num为”"或者表单中无num这个input,那么,controller方法参数中的num值则为null。
3. 自定义对象类型:
model代码:
| 
 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
						  | 
public class user {
private string firstname;
private string lastname;
省略set,get 方法
}
 | 
controller代码:
| 
 
								1
 
								2
 
								3
						  | 
@requestmapping("saysth.do")
public void test(user user) {
}
 | 
表单代码:
| 
 
								1
 
								2
 
								3
 
								4
 
								5
						  | 
<form action="saysth.do" method="post">
<input name="firstname" value="张" type="text"/>
<input name="lastname" value="三" type="text"/>
......
</form>
 | 
非常简单,只需将对象的属性名和input的name值一一匹配即可。
4. 自定义复合对象类型:
model代码:
| 
 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
 
								8
 
								9
 
								10
 
								11
 
								12
 
								13
 
								14
 
								15
 
								16
						  | 
public class contactinfo {
private string tel;
private string address;
省略set ,get 
}
public class user {
private string firstname;
private string lastname;
private contactinfo contactinfo;
省略set ,get 
}
 | 
controller代码:
| 
 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
						  | 
@requestmapping("saysth.do")
public void test(user user) {
system.out.println(user.getfirstname());
system.out.println(user.getlastname());
system.out.println(user.getcontactinfo().gettel());
system.out.println(user.getcontactinfo().getaddress());
}
 | 
表单代码:
| 
 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
						  | 
<form action="saysth.do" method="post">
<input name="firstname" value="张" /><br>
<input name="lastname" value="三" /><br>
<input name="contactinfo.tel" value="13809908909" /><br>
<input name="contactinfo.address" value="北京海淀" /><br>
<input type="submit" value="save" />
</form>
 | 
user对象中有contactinfo属性,controller中的代码和第3点说的一致,但是,在表单代码中,需要使用“属性名(对象类型的属性).属性名”来命名input的name。
5. list绑定:
list需要绑定在对象上,而不能直接写在controller方法的参数中。
model代码:
| 
 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
 
								8
 
								9
 
								10
 
								11
 
								12
 
								13
 
								14
 
								15
 
								16
 
								17
 
								18
 
								19
						  | 
public class user {
private string firstname;
private string lastname;
省略set ,get 
}
public class userlistform {
private list<user> users;
public list<user> getusers() {
return users;
}
public void setusers(list<user> users) {
this.users = users;
}
}
 | 
controller代码:
| 
 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
						  | 
@requestmapping("saysth.do")
public void test(userlistform userform) {
for (user user : userform.getusers()) {
system.out.println(user.getfirstname() + " - " + user.getlastname());
}
}
 | 
表单代码:
| 
 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
 
								8
 
								9
 
								10
 
								11
 
								12
 
								13
 
								14
 
								15
 
								16
 
								17
 
								18
 
								19
						  | 
<form action="saysth.do" method="post">
<input type="submit" value="save" /></td>
</tr>
</tfoot>
<tbody>
<tr>
<td><input name="users[0].firstname" value="aaa" /></td>
<td><input name="users[0].lastname" value="bbb" /></td>
</tr>
<tr>
<td><input name="users[1].firstname" value="ccc" /></td>
<td><input name="users[1].lastname" value="ddd" /></td>
</tr>
<tr>
<td><input name="users[2].firstname" value="eee" /></td>
<td><input name="users[2].lastname" value="fff" /></td>
</tr>
</form>
 | 
其实,这和第4点user对象中的contantinfo数据的绑定有点类似,但是这里的userlistform对象里面的属性被定义成list,而不是普通自定义对象。
所以,在表单中需要指定list的下标。值得一提的是,spring会创建一个以最大下标值为size的list对象,所以,如果表单中有动态添加行、删除行的情况,就需要特别注意,譬如一个表格,用户在使用过程中经过多次删除行、增加行的操作之后,下标值就会与实际大小不一致,
这时候,list中的对象,只有在表单中对应有下标的那些才会有值,否则会为null,看个例子:
表单代码:
| 
 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
 
								8
 
								9
 
								10
 
								11
 
								12
 
								13
 
								14
 
								15
 
								16
 
								17
 
								18
 
								19
						  | 
<form action="saysth.do" method="post">
<td colspan="2"><input type="submit" value="save" /></td>
</tr>
</tfoot>
<tbody>
<tr>
<td><input name="users[0].firstname" value="aaa" /></td>
<td><input name="users[0].lastname" value="bbb" /></td>
</tr>
<tr>
<td><input name="users[1].firstname" value="ccc" /></td>
<td><input name="users[1].lastname" value="ddd" /></td>
</tr>
<tr>
<td><input name="users[20].firstname" value="eee" /></td>
<td><input name="users[20].lastname" value="fff" /></td>
</form>
 | 
这个时候,controller中的userform.getusers()获取到list的size为21,而且这21个user对象都不会为null,但是,第2到第19的user对象中的firstname和lastname都为null。打印结果:
| 
 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
						  | 
aaa - bbb
ccc - ddd
null - null
null - null
.....
null - null
eee - fff
 | 
6. set绑定:
set和list类似,也需要绑定在对象上,而不能直接写在controller方法的参数中。但是,绑定set数据时,必须先在set对象中add相应的数量的模型对象。
model代码:
| 
 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
 
								8
 
								9
 
								10
 
								11
 
								12
 
								13
 
								14
 
								15
 
								16
 
								17
 
								18
 
								19
 
								20
 
								21
 
								22
 
								23
 
								24
 
								25
						  | 
public class user {
private string firstname;
private string lastname;
省略set ,get 
}
public class usersetform {
private set<user> users = new hashset<user>();
public usersetform() {
users.add(new user());
users.add(new user());
users.add(new user());
}
public set<user> getusers() {
return users;
}
public void setusers(set<user> users) {
this.users = users;
}
}
 | 
controller代码:
| 
 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
						  | 
@requestmapping("saysth.do")
public void test(usersetform userform) {
for (user user : userform.getusers()) {
system.out.println(user.getfirstname() + " - " + user.getlastname());
}
}
 | 
表单代码:
| 
 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
 
								8
 
								9
 
								10
 
								11
 
								12
 
								13
 
								14
 
								15
 
								16
 
								17
 
								18
						  | 
<form action="saysth.do" method="post">
<input type="submit" value="save" /></td>
</tr>
</tfoot>
<tbody>
<tr>
<td><input name="users[0].firstname" value="aaa" /></td>
<td><input name="users[0].lastname" value="bbb" /></td>
</tr>
<tr>
<td><input name="users[1].firstname" value="ccc" /></td>
<td><input name="users[1].lastname" value="ddd" /></td>
</tr>
<tr>
<td><input name="users[2].firstname" value="eee" /></td>
<td><input name="users[2].lastname" value="fff" /></td>
</form>
 | 
基本和list绑定类似。
需要特别提醒的是,如果最大下标值大于set的size,则会抛出org.springframework.beans.invalidpropertyexception异常。所以,在使用时有些不便。
7. map绑定:
map最为灵活,它也需要绑定在对象上,而不能直接写在controller方法的参数中。
model代码:
| 
 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
 
								8
 
								9
 
								10
 
								11
 
								12
 
								13
 
								14
 
								15
 
								16
 
								17
 
								18
 
								19
						  | 
public class user {
private string firstname;
private string lastname;
省略set ,get 
}
public class usermapform {
private map<string, user> users;
public map<string, user> getusers() {
return users;
}
public void setusers(map<string, user> users) {
this.users = users;
}
}
 | 
controller代码:
| 
 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
						  | 
@requestmapping("saysth.do")
public void test(usermapform userform) {
for (map.entry<string, user> entry : userform.getusers().entryset()) {
system.out.println(entry.getkey() + ": " + entry.getvalue().getfirstname() + " - " +
entry.getvalue().getlastname());
}
}
 | 
表单代码:
| 
 
								1
 
								2
 
								3
 
								4
 
								5
 
								6
 
								7
 
								8
 
								9
 
								10
 
								11
 
								12
 
								13
 
								14
 
								15
 
								16
 
								17
 
								18
						  | 
<form action="saysth.do" method="post">
<input type="submit" value="save" /></td>
</tr>
</tfoot>
<tbody>
<tr>
<td><input name="users['x'].firstname" value="aaa" /></td>
<td><input name="users['x'].lastname" value="bbb" /></td>
</tr>
<tr>
<td><input name="users['y'].firstname" value="ccc" /></td>
<td><input name="users['y'].lastname" value="ddd" /></td>
</tr>
<tr>
<td><input name="users['z'].firstname" value="eee" /></td>
<td><input name="users['z'].lastname" value="fff" /></td>
</form>
 | 
以上这篇springmvc前台向后台传值几种方式总结(从简单到复杂)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。
原文链接:https://blog.csdn.net/revent/article/details/64125965
相关文章
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
 - 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
 - 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
 - ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
 - ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
 
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
 - 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
 - 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
 - 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
 - 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
 
快网idc优惠网
QQ交流群
- 
            2025-06-04 94
 - 
            2025-05-27 65
 - 
            2025-05-27 22
 - 
            2025-05-29 74
 - 
            2025-05-27 72
 
        
    		
            	
        
        