react 前端框架如何驱动企业数字化转型与创新发展
408
2023-06-28
Spring MVC接受表单自动封装特性实例解析
这篇文章主要介绍了Spring MVC接受表单自动封装特性实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Spring MVC中的Controller可以以实体类接受来自客户端的form表单,表单的字段自动构成实体类对象
客户端的表单
实体类
public class User {
private String name;
private Integer age;
private String hobby;
public User() {
this.name = "未初始化";
this.age = 10;
this.hobby = "coding";
}
public User(String name) {
this.name = name;
this.age = 10;
this.hobby = "coding";
}
public User(String name, Integer age) {
this.name = name;
this.age =http:// age;
this.hobby = "coding";
}
public User(String name, Integer age, String hobby) {
this.name = name;
this.age = age;
ttCJeehis.hobby = hobby;
}
public Integer getAge() {
return age;
}
public String getHobby() {
return hobby;
}
public String getName() {
return name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", hobby='" + hobby + '\'' +
'}';
}
}
服务端接收
@http://Controller
@RequestMapping("/test")
public class TestController {
@RequestMapping(value = "/user", method = RequestMethod.POST)
// 控制器会自动实例化参数
public String user(User user) {
System.out.println(user);
return "redirect:/test/user";
}
@RequestMapping(value = "/user", method = RequestMethod.GET)
public String user() {
return "form";
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~