您好,登錄后才能下訂單哦!
這篇文章主要介紹了springmvc怎樣使用POJO作為參數,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
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
<form action="springmvc/testPojo" method="POST"><br> <span>username:</span><input type="text" name="username"><br> <span>password:</span><input type="password" name="password"><br> <span>email:</span><input type="text" name="password"><br> <span>age:</span><input type="text" name="password"><br> <span>city:</span><input type="text" name="address.city"><br> <span>street:</span><input type="text" name="address.street"><br> <input type="submit" value="submit"><br> </form>
啟動tomcat服務器之后:
輸入相應信息點擊提交:
成功為pojo賦值,包括級聯屬性address。
感謝你能夠認真閱讀完這篇文章,希望小編分享的“springmvc怎樣使用POJO作為參數”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。