这篇文章主要介绍了springmvc怎样使用POJO作为参数,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
目前累计服务客户数千家,积累了丰富的产品开发及服务经验。以网站设计水平和技术实力,树立企业形象,为客户提供成都做网站、网站制作、成都外贸网站建设、网站策划、网页设计、网络营销、VI设计、网站改版、漏洞修补等服务。创新互联公司始终以务实、诚信为根本,不断创新和提高建站品质,通过对领先技术的掌握、对创意设计的研究、对客户形象的视觉传递、对应用系统的结合,为客户提供更好的一站式互联网解决方案,携手广大客户,共同发展进步。
springmvc会按请求参数名和POJO属性名进行匹配,自动为该对象填充属性值,支持级联属性。
User.java
package com.gong.springmvc.entities; public class User { private String username; private String password; private String email; private Integer age; Address address; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } @Override public String toString() { return "User [username=" + username + ", password=" + password + ", email=" + email + ", age=" + age + ", address=" + address + "]"; } }
Address.java
package com.gong.springmvc.entities; public class Address { private String city; private String street; public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } @Override public String toString() { return "Address [city=" + city + ", street=" + street + "]"; } }
SpringmvcTest.java
import com.gong.springmvc.entities.User; @RequestMapping("/springmvc") @Controller public class SpringmvcTest { private static final String SUCCESS = "success"; @RequestMapping(value="/testPojo") public String testPojo(User user) { System.out.println("testPojo:"+user); return SUCCESS; } }
index.jsp
启动tomcat服务器之后:
输入相应信息点击提交:
成功为pojo赋值,包括级联属性address。
感谢你能够认真阅读完这篇文章,希望小编分享的“springmvc怎样使用POJO作为参数”这篇文章对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,更多相关知识等着你来学习!