form表单数据绑定实例
第一个:使用requestbody
前端:
<form action="testrequestbody" method="post">
用户名:<input type="text" name="username"/><br/>
密码: <input type="text" name="password"/><br/>
<input type="submit" value="提交"/>
</form>
后端
@RequestMapping(value = "/testrequestbody", produces="text/html;charset=UTF-8")
public String testrequestbody(@RequestBody String body) throws UnsupportedEncodingException {
String encode = URLDecoder.decode(body,"utf-8");
System.out.println("requestBody测试");
System.out.println(encode);
return "success";
}
第二个:直接使用实体类
实体类:自行 实现getter和setter和tostring方法
public class User {
private String username;
private String password;
private Date birthday;
}
前端
<form action="test" method="post">
用户名:<input type="text" name="username"/><br/>
密码: <input type="text" name="password"/><br/>
<input type="submit" value="提交"/>
</form>
后端
@RequestMapping("/test")
public String test(User user){
System.out.println(user);
return "success";
}
第三个:请求参数绑定,封装到类,list,map中
实体类
- Account类:自行 实现getter和setter和tostring方法
public class Account {
private String username;
private String password;
private double money;
private User user;
private

本文详细介绍了五种表单数据绑定的方法,包括使用requestbody、直接使用实体类、请求参数绑定到类、list、map中、前端提交同类多条数据放在list中、Set绑定和Map绑定。每种方法都提供了前端和后端的代码示例。
最低0.47元/天 解锁文章
177

被折叠的 条评论
为什么被折叠?



