這篇文章主要介紹springmvc使用POJO作為參數(shù)的方法,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供龍山網(wǎng)站建設、龍山做網(wǎng)站、龍山網(wǎng)站設計、龍山網(wǎng)站制作等企業(yè)網(wǎng)站建設、網(wǎng)頁設計與制作、龍山企業(yè)網(wǎng)站模板建站服務,10年龍山做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡服務。
這篇文章主要介紹了springmvc如何使用POJO作為參數(shù),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
springmvc會按請求參數(shù)名和POJO屬性名進行匹配,自動為該對象填充屬性值,支持級聯(lián)屬性。
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")@Controllerpublic 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賦值,包括級聯(lián)屬性address。
以上是“springmvc使用POJO作為參數(shù)的方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道!